Log in

View Full Version : Sending HTML email



letom
05-31-2013, 09:46 AM
I have following code in header for sending HTML emails.


.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "Content-Transfer-Encoding: 8bit . "\r\n";


My Issues are following

I can't send html email with the above coding, all mails are getting in plain text..
If the browser is not supporting HTML what is the alternate solution to display the message by stripping the HTML tags.

developerhusain
05-31-2013, 10:56 AM
You do like this to achieve what you want

$headers = "MIME-Version: 1.0" . "\r\n";
$to =$_POST['to'];
$subject ='Job Opportunity @ m`brace';
$message ='<head>
<title>Untitled Document</title>
</head>

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

// More headers
$headers .= 'From:'.$_POST['from']."\r\n";

mail($to,$subject,$message,$headers);
Do revert to me if there is any problem
developerhusain

traq
05-31-2013, 03:05 PM
I have following code in header for sending HTML emails.

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "Content-Transfer-Encoding: 8bit . "\r\n";


You've got a few syntax errors in that snippet:
// missing single quote after "8bit"
// also, you're assigning instead of concatenating (so your $headers will contain *only* this last line)
$headers = "Content-Transfer-Encoding: 8bit . "\r\n";
// should be:
$headers .= "Content-Transfer-Encoding: 8bit" . "\r\n";

Also, I would highly recommend considering switching your encoding to UTF-8 (http://joelonsoftware.com/articles/Unicode.html).

*****

If the browser is not supporting HTML what is the alternate solution to display the message by stripping the HTML tags. I assume you mean the email client, not browsers.

What I do is compose the email in plain text first, then, when necessary, add an HTML version )markdown (http://daringfireball.net/projects/markdown/) is a nice way to do this). mail() is not a very good choice to send it, since it doesn't handle multipart messages conveniently. You might look at using Pear's Mail class (http://pear.php.net/mail) instead.

Keep in mind that email clients are generally "behind the times" when it comes to HTML support. Also, security settings and/or user preferences prohibit much of HTML - no scripts, usually no images, and a large subset of css won't work either. HTML emails are the one area where you still have to "design like 1999" - tables, inline styles, and so forth.

letom
06-01-2013, 06:18 AM
You do like this to achieve what you want

$headers = "MIME-Version: 1.0" . "\r\n";
$to =$_POST['to'];
$subject ='Job Opportunity @ m`brace';
$message ='<head>
<title>Untitled Document</title>
</head>

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

// More headers
$headers .= 'From:'.$_POST['from']."\r\n";

mail($to,$subject,$message,$headers);
Do revert to me if there is any problem
developerhusain

Hi Husain

Thanks for your kind advice.
Your coding seems fine.., i have also some what related to this, but it not comes to the real solution of the problem but related to that..

Regards
Tom

letom
06-01-2013, 06:29 AM
You've got a few syntax errors in that snippet:
// missing single quote after "8bit"
// also, you're assigning instead of concatenating (so your $headers will contain *only* this last line)
$headers = "Content-Transfer-Encoding: 8bit . "\r\n";
// should be:
$headers .= "Content-Transfer-Encoding: 8bit' . "\r\n";

Also, I would highly recommend considering switching your encoding to UTF-8 (http://joelonsoftware.com/articles/Unicode.html).

*****
I assume you mean the email client, not browsers.

What I do is compose the email in plain text first, then, when necessary, add an HTML version )markdown (http://daringfireball.net/projects/markdown/) is a nice way to do this). mail() is not a very good choice to send it, since it doesn't handle multipart messages conveniently. You might look at using Pear's Mail class (http://pear.php.net/mail) instead.

Keep in mind that email clients are generally "behind the times" when it comes to HTML support. Also, security settings and/or user preferences prohibit much of HTML - no scripts, usually no images, and a large subset of css won't work either. HTML emails are the one area where you still have to "design like 1999" - tables, inline styles, and so forth.

Dear Adrain

Thanks for your response and support.
Iam not using any complicated html stuffs in messages neither images nor java scripts.
only styling the layer as follows only.

<div style="border:1px solid #000000;font-family:arial">

The mail is sending when a user register a account, so we have no option to choose html/text message before sending, it should identify itself and display HTML message or text message properly according to the working environment.

Any solution some what related to <noscript> as noscript displays the content, if javscript is not supported, same way if html is not supported any solution to display the message properly

letom
06-01-2013, 07:41 AM
HTML emails are the one area where you still have to "design like 1999" - tables, inline styles, and so forth.
Why can't we use div instead of table and use inline styling in div as in the code of previous post?

Beverleyh
06-01-2013, 09:21 AM
Adrian didn't mean that you can't use divs at all (in fact, a simple, one column layout for the sake of adding a border and font, like you have, should be perfectly ok, although you shouldn't rely on the border or font rendering 100% in all cases as it comes down to the settings/capabilities of the email client) but his advice is correct in a broader sense. For multi-column layouts and more advanced email layouts with logo/header/footer positioning, tables are just more predictable and offer a more 'stable' structure. Web-based email clients tend to fair a little better with div/CSS layouts, but desktop applications, such as MS Outlook and Lotus Notes, are less forgiving, especially with multi-column div floats (they'll just end up stacking on top of each other). Since you can't predict how your customers are accessing emails, this is one area where you should probably play it safe with the rigid, grid-like placement of (nested) tables.

letom
06-01-2013, 10:03 AM
Hi Beverley

Thanks for your advice.


Adrian didn't mean that you can't use divs at all
YES, Just for understand the difference between using div and table.

But i need a solution for displaying text version of same HTML message if the working environment is not supported HTML.

Regards
Tom

Beverleyh
06-01-2013, 10:19 AM
That's fine - you can achieve that by using a "multipart/alternative" email which allows you to set a "boundary" that the email client uses to select from a plain text or HTML email (depending on how its setup and/or its capabilities).

Here is a tutorial that should help with that: http://webcheatsheet.com/PHP/send_email_text_html_attachment.php

Beverleyh
06-01-2013, 10:32 AM
And here are some tutorials/tips/best practices for setting up HTML emails:

http://24ways.org/2009/rock-solid-html-emails/

http://kb.mailchimp.com/article/how-to-code-html-emails

letom
06-01-2013, 10:35 AM
Hi Beverley,

Thanks..

Nice tutorial in first look.

My next question is for testing the output..
all modern browsers are supporting html mails, allows me to test the HTML mail, howbeit how can i test the text version of mail.

Is Lynx browser is applicable for that ?then i cannot use lynx for testing it from my desktop mail client
OR
Web developer (https://addons.mozilla.org/en-US/firefox/addon/web-developer/) plugin of mozilla is sufficient or any other suggestions you have ?

Moreover i will post the result of the output here.

Regards
Tom

Beverleyh
06-01-2013, 10:53 AM
all modern browsers are supporting html mails, allows me to test the HTML mail, howbeit how can i test the text version of mail. I'm not really sure about that. My own web host only provides a really old, text-based email client on my oldest account, although my newer accounts have much prettier html email clients, so I'm lucky enough to be able to send out test emails and see both results.

I would imagine that there'd be a setting somewhere in most email clients that allows you to turn-off HTML emails and just display plain text though.

Here's how to do it in gmail so maybe you could just sign up for a gmail account and test there? http://webapps.stackexchange.com/questions/9585/toggle-text-plain-and-text-html-in-email-client-such-as-yahoo-gmail-or-hotmail/43911#43911

traq
06-01-2013, 07:56 PM
My next question is for testing the output..
all modern browsers are supporting html mails, allows me to test the HTML mail. howbeit how can i test the text version of mail.
Browsers allow you to view your HTML, yes - but again, most emails are not viewed in browsers. If you want to test your emails, it would be advisable to test them in email clients (e.g., Thunderbird or Outlook), not in web browsers.

Testing text-only emails is easy: the message will look the same as it does in your plain text editor.

letom
06-02-2013, 03:57 AM
Browsers allow you to view your HTML, yes - but again, most emails are not viewed in browsers. If you want to test your emails, it would be advisable to test them in email clients (e.g., Thunderbird or Outlook), not in web browsers.

Testing text-only emails is easy: the message will look the same as it does in your plain text editor.

Adrain,

Thanks.. under what circumstances the text email contains the coding of html as follows


<table><tr><td>Name</td><td>Jo</td></table>

I think keeping two separate versions of mail under

Content-Type: text/plain; charset=UTF-8\n
Content-Type: text/html; charset=UTF-8\n

will give an solution for that and it guarantees the result will produced correctly in all environments by stripping unwanted tags as table ?

traq
06-02-2013, 05:09 AM
I think keeping two separate versions of mail under

Content-Type: text/plain; charset=UTF-8\n
Content-Type: text/html; charset=UTF-8\n

will give an solution for that and it guarantees the result will produced correctly in all environments

Assuming that your messages are correctly formatted and you use the multipart/alternative boundaries properly, the email client will choose which part to display (HTML or plain text, based on its default and/or user settings), and ignore/set aside the other.

Whether any particular email client will display your HTML "correctly" (i.e., as you expect it to) is a separate issue, however. It will almost certainly look different than it does in the browser (or even any other email client). Getting this "right" is a matter of using the most basic HTML you can, with the most outdated CSS, followed by lots of testing.


under what circumstances the text email contains the coding of html as follows

<table><tr><td>Name</td><td>Jo</td></table>

I'm not sure what you mean by that. Your text email should not contain HTML markup at all.


by stripping unwanted tags as table ?

This will not happen - neither the mail() function nor any email client will rewrite your message.

letom
06-05-2013, 07:18 PM
Hi Adrain

Thanks for your advice.. I suggest the phpmailer or swift mailer is the right solution for this.

under what circumstances the text email contains the coding of html as follows
Code:

<table><tr><td>Name</td><td>Jo</td></table>

I'm not sure what you mean by that. Your text email should not contain HTML markup at all.

Iam putting both plaintext and html in message ,
My doubt is whether some email clients will display broken HTML part / HTML coding instead of the HTML design ?
FYI iam using inline and internal style sheets only with tables..

traq
06-05-2013, 08:05 PM
My doubt is whether some email clients will display broken HTML part / HTML coding instead of the HTML design ?

Unfortunately, that's not very easy to answer. There are many, many email clients, as well as different versions of each, and almost all of them handle *something* differently. Designing html emails is actually a very tedious task because of this; you simply need to decide which clients are important to you to support and then go test your email in all of them.

In general, you'll be "okay" in most clients as long as you stick to markup that's been around for the last ten years.

letom
06-05-2013, 08:39 PM
You have suggestions for libraries other than phpmailer