469,301 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,301 developers. It's quick & easy.

Script to send html newsletter using PHP

rahulephp
Hi there,

I tried a lot to send HTML code using php mail fuction, but when email sent, it showing all html tags (including style property as well).

Can you please let me know any script to send HTML formatted newsletter using PHP. (It should show only formatted text with all html property and not html tags)
Sep 30 '09 #1
19 9052
code green
1,726 Expert 1GB
You have to set Content-type in the header to be 'text/html'.
But I recommend using an email class to save this headache.

Are you sure the receiving email client is allowing HTML as such and not converting to plain text?
Sep 30 '09 #2
Dormilich
8,651 Expert Mod 8TB
do yourself a favour and use SwiftMailer. it comes with an extensive documentation so there should be no problem setting up a HTML Newsletter.
Sep 30 '09 #3
Thanks for reply from you.
Here is my coding, have a look. I have already included 'text/html'.
But still it didn't work, Can you let me know other such script?

Expand|Select|Wrap|Line Numbers
  1. $to=$data[2];
  2. $subject ="New Newsletter from couponguru.com";
  3.     $headers = 'From: couponguru@gmail.com' . "\r\n" .
  4.   'Reply-To: couponguru@gmail.com' . "\r\n" .
  5.   'X-Mailer: PHP/' . phpversion();
  6.  
  7. $headers .= "MIME-Version: 1.0\r\n";
  8. $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
  9. $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  10.  
  11. $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  12.  
  13.  
  14.  
  15.  $message = "This is a multi-part message in MIME format.\n\n" .
  16.       "--{$mime_boundary}\n" .
  17.       "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  18.       "Content-Transfer-Encoding: 7bit\n\n" .
  19.       "HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" .
  20.       "--{$mime_boundary}\n" .
  21.       "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
  22.       "Content-Transfer-Encoding: 7bit\n\n" .
  23.       "<h1>HTML E-mail</h1>" .
  24.       "<p>This is an <b>HTML</b> e-mail.</p>";
  25.  
  26.  
  27. mail($to, $subject, $message, $headers);
Sep 30 '09 #4
Dormilich
8,651 Expert Mod 8TB
@rahulephp
although Iím repeating myself: SwiftMailer
Sep 30 '09 #5
TheServant
1,168 Expert 1GB
@Dormilich
Just incase some clarification is needed, SwiftMailer and PHPMailer are libraries which do a lot of the mail stuff for you. It makes all your mail code smaller, and it automatically does some things that you might not even think about (like change content type). It's worth looking at if you have anything more than a simple, single mail script, and even if that's all you have, you should still look at it to help you now and in the future. Both are good and have their own advantages, but honestly they will both ultimately do the same thing.
Sep 30 '09 #6
Yeah.....It works, Thanks a lot buddy
Oct 1 '09 #7
TheServant
1,168 Expert 1GB
Please can you post how you corrected it, so that others who have a similar problem can learn from it. Glad it's solved.
Oct 1 '09 #8
Yes of-course,
Actually, i found there was some problem in $headers, & also it need to include <html><body> tag as well. Not all receiving email client do allow all HTML tags,
Here is list of CSS support in email clients

Have a look on simple script below, & believe me it works as i checked it on my own.

Expand|Select|Wrap|Line Numbers
  1.                 $to='receiver@gmail.com';
  2.         $subject ="New Newsletter from example.com";
  3.  
  4.         $headers = 'From: example@gmail.com' . "\r\n" .
  5.         'Reply-To: example@gmail.com' . "\r\n" .
  6.         "X-Mailer: PHP/" . phpversion()."\r\n";
  7.  
  8.         $headers .= "MIME-Version: 1.0\r\n";
  9.         $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  10.  
  11.         $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  12.  
  13.         $message ="
  14.         <html><body>
  15.         Dear subscriber,
  16.  
  17.         <h3 style='color: RED;'>
  18.         This email confirms your purchase of a 30 day
  19.         email support subscription. Please direct all 
  20.         requests to support@example.com.</h3>
  21.  
  22.         Thank you,
  23.         The Example.com support staff
  24.         </body></html>";
  25.  
  26. echo $message;
  27. mail($to, $subject, $message, $headers);
You can customize the $message as per your need.


Thanks again, I appreciate you.
Oct 1 '09 #9
TheServant
1,168 Expert 1GB
Just some more information if you are not going to go with a library like one suggested:
Have a look into Content-Type: multipart/alternative which will mean you can send a plain-text and html email in the same email. This has problems of it's own, but you can read about them.

Another interesting idea is wrapping your plain text message in <!-- --> to hide it from html reading clients, while showing a bit messy but better email to plain-text people. This also increases email size because youa re sending more text.

Yet another way you can set it up, which is a little more involved, is to make two lists: one for html people and one for plain-texts, and send each list a different email.

Again, using a PHP library reduces the work you have to do about this and can just set an alternative mail for plain-text like:
Expand|Select|Wrap|Line Numbers
  1. $email->alternative = "My plain-text email";
Which is much quicker.
Oct 1 '09 #10
Markus
6,050 Expert 4TB
btw, PHPMailer is awful. SwiftMailer beats it many times over.
Oct 2 '09 #11
zorgi
431 Expert 256MB
@Markus
Used PHPMailer quite a bit. Must say never had any problems. Easy to use and never failed. Mind you... I never used SwiftMailer so I'll keep open mind for it.
Oct 2 '09 #12
code green
1,726 Expert 1GB
Hi Markus
PHPMailer is awful
Could you expand on that please?
I found PHPMailer seriously lacking in error trapping and poorly scripted in places.
But this was an old version that I have largely re-written.
But it always works well with HTML, attachments, cc, bcc, muliple addresses.
How is SwiftMailer superior?
Oct 2 '09 #13
Markus
6,050 Expert 4TB
@code green
Documentation, for one. Extensibility, for another. Code quality, for another - last time I checked PHPMailer was just one huge class, and just a class for sake of being a class with no real OO principles/designs used; I could even suggest it was written by a sub-par developer (only joking), whereas SwiftMailer was written by Chris Corbyn, someone who I know to be a respected PHP developer.
Oct 2 '09 #14
code green
1,726 Expert 1GB
I recently downloaded PHPMailer_v5.0.2 which I planned to "upgrade" to.
Yes there is still only two classes, A phpmailer class and a smtp class.
(Also a small inherited Exception Handler class).

An upgrade to me means simply re-writing a class that inherits from phpmailer.
You obviously believe it should be re-written for SwiftMailer due to better quality.

But is there any difference between performance?
Oct 2 '09 #15
Markus
6,050 Expert 4TB
@code green
Huh? What upgrade? I never said anything about an 'upgrade'.

But is there any difference between performance?
Maybe not, but my aforementioned arguments remain.
Oct 2 '09 #16
code green
1,726 Expert 1GB
You worry me sometimes Markus.
Huh? What upgrade? I never said anything about an 'upgrade'.
It was me that mentioned upgrade
I recently downloaded PHPMailer_v5.0.2 which I planned to "upgrade" to
I apologise if hi-jacking this post to discuss Email classes is causing confusion.
Dormillich has kindly sent me a class wrapper for Swiftmailer.
That seems to be two moderators recommending Swiftmailer.
Oct 2 '09 #17
Markus
6,050 Expert 4TB
I apologise :P Now I see what you're talking about. Well, I wouldn't have used PHPMailer in the first place, but if it's what you're using now then by all means carry on. :)
Oct 2 '09 #18
Dormilich
8,651 Expert Mod 8TB
@code green
tell me what you think of it.

@code green
yo

I would have sent the batch mail wrapper too, but it contained to much private code.
Oct 2 '09 #19
TheServant
1,168 Expert 1GB
@Markus
Thanks for your analysis, I have only really used SwiftMailer, so wasn't really qualified to make the statement I did, but they do ultimately achieve the same thing, even if one is a bit more bulky and rigid.
Oct 4 '09 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Max | last post: by
reply views Thread by Philippe C. Martin | last post: by
4 posts views Thread by VB Programmer | last post: by
2 posts views Thread by VB Programmer | last post: by
1 post views Thread by rahia307 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.