Jump to content

Sending Text and HTML emails at the same time...


Recommended Posts

Hi Guys,


I'm having some issues with this as, to be honest, i don't get it.....  :D


what i'd like to do is send an HTML email to the customer, when the do certain things, but as some mail boxes don't like HTML emails i realised that i'd also have to send a text version..


rather than having a tick box or something to choose, i've read that you can send them both at once using a multipart form...


however, i can't get it to work...


here's what i have so far:



// multiple recipients
$to  = 'me@mymail.com' . ', '; // note the comma
$to .= 'someone@theirmail.com';

$boundary = "nextPart";

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

// subject
$subject = 'Testing an HTML/Text email';

//Text Version

$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
//$headers .= "Text only..., will it work...?";

$message = 'Text only..., will it work...?' ;

//HTML Version

// To send HTML mail, the Content-type header must be set
$headers .= "\n--$boundary--\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Me <me@mymail.com>, Someone Else <someone@theirmail.com>' . "\r\n";
$headers .= 'From: Add Some Music <no-reply@addsomemusic.co.uk>' . "\r\n";

// message
$message = '

  <title>Test an HTML email</title>

<p><img src="http://www.addsomemusic.co.uk/images/ASM_Logo.jpg" width="150"></p>

<p>Some Text.....</p>



// Mail it
$mail_sent = mail($to, $subject, $message, $headers);

//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";



could anyone please tell me what i'm doing wrong...?


Thanks in advance!


Add Some Music


Link to comment
Share on other sites

Thanks for the reply rhodesa,


however this is just me testing...


Everything is already setup, sending text emails from the site, with mail forms and all that,


I just want to alter the code to send HTML, but of course, I also need to send Text


Any pointers would be much apprecaited




Add Some Music


Link to comment
Share on other sites

at first glance, you have a couple problems...the multipart/alternative header is good, but text/plain and text/html headers should actually be in $message. Also, when you add the HTML message part, you replace the value of $message instead of appending to it. here is an example i found you may want to look at:


Link to comment
Share on other sites

Thanks for your help...


This is gonna take some looking in to.....


i'll report back when i got my head round it a bit better...




oh, quick question.


If you send a text and an html email at the same time, how does the receivers email choose which to display....? or is that a silly question... :D


Thanks Again


Add Some music


Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.