Click to See Complete Forum and Search --> : Sending Email Attachments Via PHP


Baby Jai
08-16-2003, 10:22 PM
I dont know what I should name the PHP's. Also I have the one that is an application and it looks great, whats the html to add a file upload box? Once that is done I need to create another PHP like a miler.php, but im type confused. So all I have to do is edit the mailer php? So then what is the actual code for it then?

This is it, but its really confusing.

$mime_boundary = "<<<--==+X[".md5(time())."]";

Sort out your basic headers as usual, by piling them into a $headers string.

$headers .= "From: Automatic <an.e.mail@domain.net>\r\n";
$headers .= "To: SomeName <an.e.mail@domain.net>\r\n";
Then you've got to specify the type of content you're dealing with. This is also put into the $headers string. In this case, it will be mixed between text, and some kind of attachment, hence the Content-Type bit. The boundary bit uses the boundary variable you made earlier:

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
Next, get on with piling the message into a $message string. You need quite a bit of header looking stuff in here. The first bit is what is displayed if the client receiveing the email doesn't support MIME, followed by the boundary string (preceded by "--", not sure why).

$message .= "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
Next comes the actual content of the email, each part needs it's own mini header describing what it is.

$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "Email content and what not: \r\n";
$message .= "This is the file you asked for! \r\n";
$message .= "--".$mime_boundary."\r\n";
Finally, a plaintext attachment. In this case I already put your contents into $fileContent.

$message .= "Content-Type: application/octet-stream;\r\n";
$message .= " name=\"filename.extn\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"filename.extn\"\r\n";
$message .= "\r\n";
$message .= $fileContent;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
Then send the thing, using the mail function as described in the manual. You will probably have to have a few goes at this before you get it right. The linbreaks need to be exactly right for this to work, and if it doesn't, that's probably why.


$ok = mail("an.e.mail@domain.net", "file by email", $message, $headers);

Baby Jai
08-18-2003, 05:55 PM
can someone please help me with this

brendandonhue
08-18-2003, 06:03 PM
HTML for an upload box is
<input type="file">

I need some more detail before I can help ya....
I dont know what I should name the PHP's.
What do you mean? Are you talking about the filenames of the PHP scripts?
Also I have the one that is an application
Have one what thats an application?
What is miler.php? What do you want miler.php to do?
Whats the actual code for what?

Baby Jai
08-18-2003, 06:10 PM
Ok here is my issue, if you go here (http://www.babyjai.com/application.php) , i need to send a email attachment with this. Its going to be a picture. I just need to know what I need to add to it. Any help on this would be great

brendandonhue
08-18-2003, 06:15 PM
Why don't you just use the code in your first post?
Or are you having problems with it?

Baby Jai
08-18-2003, 06:17 PM
all i want to do is add another box in my form with a browse button, and it will automatically send the file attachment once they hit submit. I just dont know hwo about fdoing it. Also that one above has got me lost, there is stuff all over the place.

brendandonhue
08-18-2003, 06:19 PM
To add the upload box to your form use
<input type="file">

Where did you get that script from? It probably has instructions on how to use it.

Baby Jai
08-18-2003, 06:25 PM
That is not going to send it though, although i see where your att but it doesnt send it

Baby Jai
08-18-2003, 06:29 PM
see I added it but it doesnt work

brendandonhue
08-18-2003, 07:47 PM
You still have to submit it to the script you posted in your first post.
You might want to check for instructions that came with the script though.

Baby Jai
08-18-2003, 08:17 PM
ahh forget it, its useless

pyro
08-18-2003, 10:05 PM
Sending attachments in PHP is a complicated task, as you need to use MIME headers, and they are complicated, in themselves. I've never had to send an attachment with PHP, so I can give you much direct help. Just to answer brendandonhue's question, the script can be found at http://www.hollowearth.co.uk/tech/php/email_attachments.php and is used to send a plaintext attachment.

Baby Jai
08-18-2003, 10:09 PM
yeah thats the one i was trying to play with and got absolutely no where. I was hoping someone could help me out and help me fix that PHP. No other thread onthe web knows, and I figured you would know pyro :( .

pyro
08-18-2003, 10:19 PM
When it comes to MIME headers, it seems to just take a lot of playing around. One other possiblity would be to sent the mail using the IMAP mail protocol, perhaps? Though even with that, you may need to play around with the MIME headers....

Baby Jai
08-18-2003, 10:32 PM
any help bro is good help, can you do just this last piece of coding for me, anyway that I cant send a jpeg no bigger than 1 Meg would be great:D

cleromancer
08-20-2003, 12:06 AM
This is a working example for you.

http://www.sitepoint.com/article/679/5

Baby Jai
08-20-2003, 04:33 AM
anymore confusing than that and i would get lost