View Full Version : send HTML email with styling

01-21-2010, 10:04 PM

I am trying to send an HTML email with styling but however I apply styling nothing appears in the sent email. I have tried to use external stylesheets but that didnt work and also inline styling i.e

<p style='background-color: #000000;'>

but this results in an email being sent but with no text in the body. Without any style the message comes through fine with unstyled text.

How do you apply style to a PHP email? My code is below if you need to see it.

Many thanks


if (isset($_POST['linkid'])) {

$rows = explode("%", $_POST['linkid']);
$upperLimit = $rows[16] + 17;
//$to = $rows[2];

$to = "rtwpricetag@googlemail.com";
$from = "email test";

$subject = $name . ' recommends rtwpricetag.com';

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

$message = "<html>


<h1>" . $rows[0] . "'s Pricetag</h1>

<h2>Grand Total: " . $rows[$upperLimit+24] . "</h2>

<p>Hi " . $rows[1] . "</p>
<p>" . $rows[0] . " has sent you the following report of a round the world trip he/she calculated today on <a

href='http://www.rtwpricetag.com/round-the-world-trip-budget-cost-calculator.php'>rtwpricetag.com</a>. If you want to create one of your own and send it

back, please use the hyperlink above to do so.</p></td></tr></table>



<tr><td><b>Leaving on:</b> " . date('jS F Y',$rows[3]) . "</td></tr>
<tr><td><b>Returning on:</b> " . $rows[4] . "</td></tr>
<tr><td><b>Length of trip:</b> " . $rows[5] . "</td></tr>


<tr><th colspan=2>Pre Costs</th></tr>
<tr><td colspan=4></td></tr>
<tr><td><b>Jabs</b></td><td>" . $rows[7] . "</td></tr>
<tr><td><b>AirFares</b></td><td>" . $rows[8] . "</td></tr>
<tr><td><b>Visas</b></td><td>" . $rows[9] . "</td></tr>
<tr><td><b>Insurance</b></td><td>" . $rows[10] . "</td></tr>
<tr><td><b>Tours</b></td><td>" . $rows[11] . "</td></tr>";

$currency = substr($rows[12], -3);
$cost = strrev(strstr(strrev($rows[13]), ' '));

if ($cost + ' ' + $currency != '0.00 GBP')
$message = $message . "<tr><td>" . $rows[12] . "</td><td>" . $rows[13] . "</td></tr>";

$currency = substr($rows[13], -3);
$cost = strrev(strstr(strrev($rows[15]), ' '));

if ($cost + ' ' + $currency != '0.00 GBP')
$message = $message . "<tr><td><b>" . $rows[14] . "</b></td><td><b>" . $rows[15] . "</b></td></tr>";

$message = $message . "<tr><td colspan=4></td></tr><tr><th colspan=4>Destinations</th></tr><tr><td colspan=4></td></tr>";

if ($rows[16] > 1)

$message = $message . "<tr><td><strong>Destination</strong></td><td><strong>Length Of Stay</strong></td><td><strong>Avg. Daily

Budget</strong></td><td><strong>Sub Total</strong></td></tr>";

for ($x=17; $x<intval($upperLimit); $x++)

$cells = explode("|", $rows[intval($x)]);
$message = $message . "<tr><td>" .$cells[0] . "</td><td>" . $cells[2] . " Day(s)</td><td>" . $cells[1] . "</td><td>" . $cells[3] . "</tr>";


$gTotal = explode("|", $rows[intval($upperLimit)]);

$message = $message . "<tr><td colspan=3</td><td style='border-top: solid 1px; font-weight: bold;'>" . $gTotal[0] . "</td></tr>";

$message = $message . "<tr><td colspan=4>no destinations added</td></tr>";

$message = $message . "<tr><td colspan=4></td></tr>
<tr><th colspan=2>Lifestyle</th></tr>
<tr><td colspan=4></td></tr>

<tr><td><b>Accommodation</b></td><td>" . $rows[$upperLimit+1] . "</td></tr>
<tr><td><b>Drinker</b></td><td>" . $rows[$upperLimit+2] . "</td></tr>
<tr><td><b>Eater</b></td><td>" . $rows[$upperLimit+3] . "</td></tr>
<tr><td><b>Shopper</b></td><td>" . $rows[$upperLimit+4] . "</td></tr>

<tr><td colspan=4></td></tr>
<tr><th colspan=2>Activities</th></tr>
<tr><td colspan=4></td></tr>

<tr><td><b>Zorbing</b></td><td>" . $rows[$upperLimit+5] . "</td></tr>
<tr><td><b>Bungee Jumping</b></td><td>" . $rows[$upperLimit+6] . "</td></tr>
<tr><td><b>Sky Diving</b></td><td>" . $rows[$upperLimit+7] . "</td></tr>
<tr><td><b>Canoeing</b></td><td>" . $rows[$upperLimit+8] . "</td></tr>
<tr><td><b>Rafting</b></td><td>" . $rows[$upperLimit+9] . "</td></tr>
<tr><td><b>Cycling</b></td><td>" . $rows[$upperLimit+10] . "</td></tr>
<tr><td><b>Whale Watching</b></td><td>" . $rows[$upperLimit+12] . "</td></tr>
<tr><td><b>Canyoning</b></td><td>" . $rows[$upperLimit+13] . "</td></tr>
<tr><td><b>Kayaking</b></td><td>" . $rows[$upperLimit+14] . "</td></tr>
<tr><td><b>Surfing</b></td><td>" . $rows[$upperLimit+15] . "</td></tr>
<tr><td><b>Rocking Climbing</b></td><td>" . $rows[$upperLimit+16] . "</td></tr>
<tr><td><b>Scuba Diving</b></td><td>" . $rows[$upperLimit+17] . "</td></tr>
<tr><td><b>" . $rows[$upperLimit+18] . "</b></td><td>" . $rows[$upperLimit+19] . "</td></tr>
<tr><td><b>" . $rows[$upperLimit+20] . "</b></td><td>" . $rows[$upperLimit+21] . "</td></tr>
<tr><td><b>" . $rows[$upperLimit+22] . "</b></td><td>" . $rows[$upperLimit+23] . "</td></tr>
<tr><td colspan=4></td></tr>
<tr><th>GRAND TOTAL</th><td style='background-color: #D2433F; color: #FFFFFF; font-size: 18px;'>" . $rows[$upperLimit+24] . "</td></tr>

<a href='http://www.rtwpricetag.com/round-the-world-trip-budget-cost-calculator.php'>Create your own pricetag</a>


// Send
mail($to, $subject, $message, $headers);

echo $message;



01-22-2010, 01:56 AM
you need to MIME version, from what I was told. for example, the following example enables me to send a formatted email to someone:
$send_header = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: adam@domain.com' . "\r\n" . 'Reply-To: adam@domain.com';
$send_body = '


etcc, etc...'

also be aware that tables are not reliable, because they come through differently with every mail server. they are even less reliable if you try to format them from left and right. in general, they will come through one on top of the other when you actually "see" the email on the recipient's end.

01-22-2010, 03:41 AM
You should use something like PHPMailer to get this done. It will do it pretty easily and won't require all the messing around with large scripts

01-23-2010, 06:32 PM
is PHP mailer a library of mailing functions? Do I download them and upload to my webserver?

01-23-2010, 06:34 PM
It's a class. It's very easy to use
check out the tutorial here

01-25-2010, 09:11 PM

I am trying a simple example with phpmailer but nothing it coming through. Phpmailer is working fine because I have sent the same message fine with no styling. The majority is standard code, just the HTML table that is mine.


if (isset($_POST['linkid'])) {

$rows = explode("%", $_POST['linkid']);
$upperLimit = $rows[16] + 17;


$message = '<table>
<tr><th colspan=2 style="font-family: Verdana, arial, sans; font-size: 18px; background-color: #333333; color: #FFFFFF;">Lifestyle</th></tr>
<tr><td colspan=4></td></tr>

<tr><td style="font-family: Verdana, arial, sans; font-size: 18px; background-color: #333333; color: #FFFFFF;">Accommodation</td></tr>
<tr><td colspan=4></td></tr>
<tr><th colspan=2>Activities</th></tr>
<tr><td colspan=4></td></tr>

<a href="http://www.rtwpricetag.com/round-the-world-trip-budget-cost-calculator.php">Create your own pricetag</a>';

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport

try {
$mail->AddAddress('rtwpricetag@googlemail.com', 'John Doe');
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically

echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!

echo $message;


01-26-2010, 12:12 AM
surely sending an HTML email should not be this hard. Is the error in the apostrophes or double quotes aroudn the HTML? I have tried many combinations of this.

Even sending very basic messages works with a bit of styling but can then stop working if I add some more inline styling.

I am viewing in gmail if that makes a difference. I can't even confidently style a table with a background color.

01-26-2010, 12:26 PM
gmail has nothing to do with it. i have used the mail function with plenty of incomming mail servers, and all of them support HTML formatting. i would go back to using the mail function out of simplicity. but that's just this humble man's opinion. it is really not that hard...

01-26-2010, 03:15 PM
php mailer is not complicated, its maybe the syntax around the styling that is wrong.

01-26-2010, 10:44 PM
the gmail client was bring all my styling and html through but it was not being displayed to me because i had to toggle a link to actually see it. For some reason i had to click on a "show quoted text" link (the only thing that would actually show) and then it all appeared. No idea why i had to do this but it cost me about 2 days of development.

08-10-2011, 04:30 PM
I keep getting the same thing!!! Very annoying. I've even listened in on the
socket level communications to see what is actually being sent to the SMTP server and there is NOTHING to indicate that the text is quoted,
there are no tabs, no indentation or special characters.

I'm getting this with PhpMailer 5.1. Did you ever find out why it happens?


08-10-2011, 04:42 PM
Why is it that after you spend 1/2 a day trying to find something and then
desperately post a message to a forum, that you find the answer 10 seconds later? !!

It turns out that there was nothing special about these message, and nothing wrong with them, and no "quoted text" in them.

Instead, what is happening is that the Gmail email client is looking at a series of "test" messages that look very similar, and grouping them into a thread. It hides the "similar" parts of each message with is "- show quoted text-" link to
make it easier to read a group of messages in a long thread, where everyone keeps quoting each other.

I've been banging my head against the wall, sending test email messages and the whole problem was that I had sent too many similar messages -- oh the irony.