Jump to content

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


Alexhoward

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:

 

<?php

// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

$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 <[email protected]>, Someone Else <[email protected]>' . "\r\n";
$headers .= 'From: Add Some Music <[email protected]>' . "\r\n";

// message
$message = '

<html>
<head>
  <title>Test an HTML email</title>
</head>
<body>

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

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

</body>
</html>

';

// 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

http://www.addsomemusic.co.uk

save yourself the time and use PHPMailer:

http://phpmailer.codeworxtech.com

here is an example...it's more complicated then what you are doing, but it has the HTML/Text stuff you want:

http://phpmailer.codeworxtech.com/index.php?pg=examples

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

 

Thanks!

 

Add Some Music

http://www.addsomemusic.co.uk

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:

http://us2.php.net/manual/en/function.mail.php#85772

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...

 

cheers,

 

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

http://www.addsomemusic.co.uk

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.