Text/html email sending

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

Text/html email sending

Post by Tubby »

Hi,
I'm creating a newsletter system that I want to send emails in html/text format so most people can read it, I'm currently using:

Code: Select all

$headers = "From: our company <newsletter@ourcompany.tld>\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$boundary = uniqid("==companyname"); 

$headers .= "Content-Type: multipart/alternative" . 
   ";\r\n boundary="$boundary"\r\n\r\n"; 

// text version
$headers .= "--$boundary\r\n" .
	"Content-Type: text/plain; charset="ISO-8859-1"\r\n" .
	"Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= chunk_split("This is the text based version");

// html version
$headers .= "--$boundary\r\n" .
	"Content-Type: text/html; charset="ISO-8859-1"\r\n" .
	"Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$headers .= chunk_split("<html><head><title>test</title></head><body><font color="#0066ee">this is the html version</font></body></html>");

$headers .= "--$boundary";

// send message 
mail("customer@e.mail", "Newsletter...", "", $headers);
Which works, but is it the correct way of doing it in your opinions? How would you do it differently?

Also doing it like this, when it goes to an email address that is protected by spamassassin, spamassassin complains of "MIME_MISSING_BOUNDARY,MSGID_FROM_MTA_SHORT" what does this mean? and how can I get it not complain of this (to shave a couple of spam points off the score) - the protected email address receives html email from others and it dosen't complain of those errors on theirs.
I've checked the source of their emails and can't see any difference between theirs and mine.

Thanks :)
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Are you gonna send attachment with your email ?
if not you don't need boundary

Code: Select all

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
that's all the ehaders you need for the HTML email
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

Post by Tubby »

I'll probably be sending inline images with the email at some point so I'd need the boundarys then won't I?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

yes boundary are for text and attachment.
User avatar
Tubby
Forum Commoner
Posts: 28
Joined: Thu Oct 17, 2002 5:55 pm

Post by Tubby »

Hi again,
I've done a bit of re-writing as per a zend tutorial, however it dosen't work very well.

Here is the php:

Code: Select all

$headers = "From: test <test@whatever.bleh>\r\n"; 

//specify MIME version 1.0 
$headers .= "MIME-Version: 1.0\r\n"; 

//boundary
$boundary1 = uniqid("==infinity");

// content type
$headers .= "Content-Type: multipart/alternative;\r\n" .
			"boundary="$boundary1"\r\n";

$headers .= "Content-Transfer-Encoding: 7bit\r\n";

$headers .= "This is a MIME encoded message.\r\n";

$headers .= "--$boundary1\r\n"; 

$headers .= "Content-Type: multipart/related;\r\n";

$boundary2 = uniqid("==infinity");
$headers .= "boundary="$boundary2";\r\n";  

$headers .= "--$boundary2\r\n";  

$headers .= "Content-Type: multipart/alternative;\r\n";

$boundary3 = uniqid("==infinity");
$headers .= "boundary="$boundary3"\r\n"; 

$headers .= "--$boundary3\r\n"; 

$headers .= "Content-Type: text/plain\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";

// text version
$headers .= chunk_split("Hey this is the text version!");

$headers .= "--$boundary3\r\n";  

$headers .= "Content-Type: text/html\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";

//html version
$headers .= chunk_split("<html><head><title>moo</title></head><body><b>Hey</b> html <i>version</i><br><img src="cid:headerimage" border=0 alt=""></body></html>");

$headers .= "--$boundary3--\r\n"; 

$headers .= "--$boundary2\r\n";  

$headers .= "Content-Type: image/jpeg\r\n";

$headers .= "Content-Transfer-Encoding: base64\r\n";

$headers .= "Content-ID: headerimage\r\n";

$headers .= chunk_split(encode_img('/home/test/public_html/images/emailhead.jpg'));

$headers .= "--$boundary2--\r\n"; 

$headers .= "--$boundary1--\r\n"; 

//send message 
mail("moo@moo.moo", "Hello", "", $headers);
(encode_img(); is a function of my own to encode said image into to base64)

When this is script is run, the email that is send displays as this in the email client:
boundary="==infinity413f69456dde3"
Content-Transfer-Encoding: 7bit
This is a MIME encoded message.
--==infinity413f69456dde3
Content-Type: multipart/related;
boundary="==infinity413f694572c01";
--==infinity413f694572c01
Content-Type: multipart/alternative;
boundary="==infinity413f694577a20"
--==infinity413f694577a20
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hey this is the text version!
--==infinity413f694577a20
Content-Type: text/html
Content-Transfer-Encoding: 7bit
<html><head><title>moo</title></head><body><b>Hey</b> html <i>version</i><br
><img src="cid:headerimage" border=0 alt=""></body></html>
--==infinity413f694577a20--
--==infinity413f694572c01
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: headerimage
<<<<<base64'd image>>>>>
--==infinity413f694572c01--
--==infinity413f69456dde3--
Instead of displaying the text or html version.
Any clues as to why that is?

Thanks for reading :)
Post Reply