PHP Code for Email with Attachments
Moderator: General Moderators
PHP Code for Email with Attachments
How does one code emails with attachment? I once viewed a code for this but it got really confusing for me. Please help.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
To be brief, if you can't follow the code in that snippet it goes like this:
The email must be a multipart/mixed content type. This needs to define some sort of a boundary where one part starts and the next part begins.
The headers for the overall mail define this like so:
Now you need to patch together a sub-email for each part which you do like this:
You leave a blank line after the headers, which should immediately follow the MIME boundary you defined prepended with hypen, hypen " -- ".
Do this for all the parts you want.
Attachments are no different, they are just another part using that boundary, which are usually base64 encoded.
You do need to define the filename and specify the content-disposition to be an attachment though. To get the data for the attachment, just use:
So that would be another part looking like this:
Stick all those parts together and end the email with the boundary prepended AND appended with hyphen, hyphen.
That's it really
You could of course just use an already developed mailer though 
The email must be a multipart/mixed content type. This needs to define some sort of a boundary where one part starts and the next part begins.
The headers for the overall mail define this like so:
Code: Select all
Subject: Your subject
To: "Someone" <someone@address.com>
From: "You" <your@name.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed;
boundary="some_randrom_string"Code: Select all
--some_random_string
Content-Type: text/html
Content-Transfer-Encoding: 7bit
<strong>This is a HTML email all rather annoyingly in bold!</strong>Do this for all the parts you want.
Attachments are no different, they are just another part using that boundary, which are usually base64 encoded.
You do need to define the filename and specify the content-disposition to be an attachment though. To get the data for the attachment, just use:
Code: Select all
base64_encode(file_get_contents($filename));Code: Select all
--some_random_string
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Dispostion: attachment;
filename="Your filename.jpg"
Content-Description: Your filename.jpg
<base 64 encoded data here>Code: Select all
Subject: Your subject
To: "Someone" <someone@address.com>
From: "You" <your@name.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Type: multipart/mixed;
boundary="some_randrom_string"
--some_random_string
Content-Type: text/html
Content-Transfer-Encoding: 7bit
<strong>This is a HTML email all rather annoyingly in bold!</strong>
--some_random_string
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Dispostion: attachment;
filename="Your filename.jpg"
Content-Description: Your filename.jpg
<base 64 encoded data here>
--some_random_string--