Web Design Forum: Send HTML email via php mail - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Send HTML email via php mail Rate Topic: -----

#1 User is offline   HogIT 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 283
  • Joined: 09-September 09
  • Reputation: 14
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Designer

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?
0

#2 User is offline   Geeks 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 321
  • Joined: 07-October 09
  • Reputation: 11
  • Gender:Male
  • Location:South Africa
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 12 March 2010 - 02:13 PM

try phpmailer
0

#3 User is offline   HogIT 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 283
  • Joined: 09-September 09
  • Reputation: 14
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Designer

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?
0

#4 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 March 2010 - 04:11 PM

View PostHogIT, 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?


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 :)
1

#5 User is offline   HogIT 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 283
  • Joined: 09-September 09
  • Reputation: 14
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 12 March 2010 - 04:22 PM

View Postwebdesigner93, on 12 March 2010 - 04:11 PM, said:

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 :)


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

#6 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 March 2010 - 04:33 PM

View PostHogIT, on 12 March 2010 - 04:22 PM, said:

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.


Well the example i gave u is using the charset UTF-8 so that might be one reason insted of the charset u were using :)
0

#7 User is offline   Wickham 

  • Web Guru
  • View gallery
  • Group: Moderators
  • Posts: 2,875
  • Joined: 11-June 09
  • Reputation: 257
  • Gender:Male
  • Location:Salisbury UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 12 March 2010 - 08:22 PM

Just for info, I've been sending html emails using:-
$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?
1

#8 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 March 2010 - 09:44 PM

View PostWickham, on 12 March 2010 - 08:22 PM, said:

Just for info, I've been sending html emails using:-
$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 :)
0

#9 User is offline   TopShopper 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 878
  • Joined: 21-July 09
  • Reputation: 14
  • Gender:Male
  • Location:Cardiff, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

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);

1

#10 User is offline   HogIT 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 283
  • Joined: 09-September 09
  • Reputation: 14
  • Gender:Male
  • Experience:Nothing
  • Area of Expertise:Designer

Posted 13 March 2010 - 09:17 AM

Thanks all.

Your input on this is much appreciated.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users