views:

45

answers:

3

I am using MAMP (LAMP stack development environment) to send emails using PHP.

The code is below:

$out_message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>You have requested an appointment with the Smile Zone</title>
</head>

<body>
<p style="font-family:Arial, Helvetica, sans-serif;"><strong>Dear '.$details_name.'</strong></p>
<p style="font-family:Arial, Helvetica, sans-serif">You have requested an appointment!</p>
<p style="padding:10px;"><strong>Please note that this appointment is currently unconfirmed</strong></p>
</body>
</html>
';

//now for the client side:
$out_to = '[email protected]';
$out_subject = 'My Subject';

// 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: John <[email protected]> \r\n";
$headers .= 'From: The Smile Zone <[email protected]>' . "\r\n";

$success2 = mail($out_to, $out_subject, $out_message, $headers);

When I send this email and view in Apple Mail, it appears as it should, however in GMail, in the browser all I get is the raw source.

Why is this?

A: 

Remove these tags

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>You have requested an appointment with the Smile Zone</title>
</head>

<body>

Some use full links

Use some standard library like

  • zend_mail
  • pear-mail
JapanPro
Also, a handy reference for formatting: http://www.campaignmonitor.com/downloads/documents-tools/Campaign_Monitor_Guide_to_CSS_Support_in_Email_27_Aug_2009.pdf Luckily, on this one it doesn't look like any compatibility issues apply, but it's a great resource for the next time.
bpeterson76
+1  A: 

You have conflicting charsets:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

and:

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Change them to match.

webbiedave
how foolish of me...
Ashley Ward
A: 

Gmail is much less forgiving about the formatting of your emails than other web clients. You'll need to conform to RFC2822. My guess is that the mail function in PHP does not create messages that conform to that RFC for multi-part messages. (That is, messages with both a text and an HTML part.) Per the documentation for mail it is better to use PEAR::Mail_Mime.

Sean Vieira