Page 1 of 1
Send HTML email via php mail
#1
Posted 12 March 2010 - 01:06 PM
I have been investigating sending html email via php and have the following test code:-
<?php
$to = "admin@test.com";
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table border="1">
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
// $headers = 'MIME-Version: 1.0' . "\r\n";
// $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
// $headers .= 'To: Mary <admin@hogit.com>, Kelly <admin@hogit.com>' . "\r\n";
// $headers .= 'From: Birthday Reminder <sep06@uk2.net>' . "\r\n";
$headers = "From: test@uk2.net\r\n";
$headers .= "Reply-To: test@uk2.net\r\n";
$headers .= "CC: test@uk2.net\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
It sends successfully if I include the CC: header param, if I omit it it sends the email as text ... see below:
----------------------------------------------------------
Content-Type: text/html; charset=ISO-8859-1
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table border="1">
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
--------------------------------------------------
Any thoughts or better alternatives?
<?php
$to = "admin@test.com";
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table border="1">
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
// $headers = 'MIME-Version: 1.0' . "\r\n";
// $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
// $headers .= 'To: Mary <admin@hogit.com>, Kelly <admin@hogit.com>' . "\r\n";
// $headers .= 'From: Birthday Reminder <sep06@uk2.net>' . "\r\n";
$headers = "From: test@uk2.net\r\n";
$headers .= "Reply-To: test@uk2.net\r\n";
$headers .= "CC: test@uk2.net\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
It sends successfully if I include the CC: header param, if I omit it it sends the email as text ... see below:
----------------------------------------------------------
Content-Type: text/html; charset=ISO-8859-1
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table border="1">
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
--------------------------------------------------
Any thoughts or better alternatives?
#3
Posted 12 March 2010 - 03:58 PM
After much experimenting I seem to have narrowed the problem to
$headers .= "MIME-Version: 1.0\r\n";
If this appears anywhere before the end the following headers are ignored.
If I put it at the end the HTML email displays OK but with the MIME-Version displayed as text.
If I leave it out the html email displays.
I can't help feeling the MIME header must be important so is leaving it out wise?
$headers .= "MIME-Version: 1.0\r\n";
If this appears anywhere before the end the following headers are ignored.
If I put it at the end the HTML email displays OK but with the MIME-Version displayed as text.
If I leave it out the html email displays.
I can't help feeling the MIME header must be important so is leaving it out wise?
#4
Posted 12 March 2010 - 04:11 PM
HogIT, on 12 March 2010 - 03:58 PM, said:
After much experimenting I seem to have narrowed the problem to
$headers .= "MIME-Version: 1.0\r\n";
If this appears anywhere before the end the following headers are ignored.
If I put it at the end the HTML email displays OK but with the MIME-Version displayed as text.
If I leave it out the html email displays.
I can't help feeling the MIME header must be important so is leaving it out wise?
$headers .= "MIME-Version: 1.0\r\n";
If this appears anywhere before the end the following headers are ignored.
If I put it at the end the HTML email displays OK but with the MIME-Version displayed as text.
If I leave it out the html email displays.
I can't help feeling the MIME header must be important so is leaving it out wise?
Use these 3 headers to send your message as html
$headers .= "MIME-Version: 1.0\n"; $headers.="Content-Transfer-Encoding: 8bit\n"; $headers .="Content-Type: text/html; charset=UTF-8\n";
Please +1 me if this helps
#5
Posted 12 March 2010 - 04:22 PM
webdesigner93, on 12 March 2010 - 04:11 PM, said:
Use these 3 headers to send your message as html
Please +1 me if this helps
$headers .= "MIME-Version: 1.0\n"; $headers.="Content-Transfer-Encoding: 8bit\n"; $headers .="Content-Type: text/html; charset=UTF-8\n";
Please +1 me if this helps
That did the trick, thanks.
Any idea why the other solution didn't work, the example I used is used in several examples on the web.
#7
Posted 12 March 2010 - 08:22 PM
Just for info, I've been sending html emails using:-
so perhaps it was the charset=ISO-8859-1 causing the trouble.
My emails work without
so what does that do? Is it anything to do with computer specifications? Most computers are 32bit, so why is 8bit required?
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
so perhaps it was the charset=ISO-8859-1 causing the trouble.
My emails work without
$headers.="Content-Transfer-Encoding: 8bit\n";
so what does that do? Is it anything to do with computer specifications? Most computers are 32bit, so why is 8bit required?
#8
Posted 12 March 2010 - 09:44 PM
Wickham, on 12 March 2010 - 08:22 PM, said:
Just for info, I've been sending html emails using:-
so perhaps it was the charset=ISO-8859-1 causing the trouble.
My emails work without
so what does that do? Is it anything to do with computer specifications? Most computers are 32bit, so why is 8bit required?
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
so perhaps it was the charset=ISO-8859-1 causing the trouble.
My emails work without
$headers.="Content-Transfer-Encoding: 8bit\n";
so what does that do? Is it anything to do with computer specifications? Most computers are 32bit, so why is 8bit required?
Your right its not required for it to work but i still think its important cause it shows the type of encoding performed on the data and you can only pick from. 7bit, 8bit, binary, quoted-printable, base64 and custom so i just pickes 8bit encoding i use that most all the time
#9
Posted 13 March 2010 - 09:01 AM
You should make sure that your html is in lines of no more than 70 characters or funny things may happen with some email clients:
$HtmlBody = wordwrap($HtmlBody, 70);
- ← Timesheet logging...
- Server Side (PHP, Databases, ASP.NET, etc)
- Found this code for drop down lists, can some help tweak it for me? →
Share this topic:
Page 1 of 1
Help



















