472,328 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 9246
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,658 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,658 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,658 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

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

Similar topics

4
by: Max | last post by:
I am looking for some simple example code on generating SMTP e-mail messages in Python. I have a script that sends system reports to me at a...
0
by: Philippe C. Martin | last post by:
Hi, I have the following problem: 1) I can use smtplib to send text messages 2) I can generate html 3) I want to email the html and want it to...
0
by: cooldv | last post by:
i have an *access database + ASP newsletter* that is working fine and it sends newsletter to ALL the email addresses in the DB. i want to send this...
1
by: Fahad | last post by:
I want to build a system that sends formated HTML newsletter. Like the one we recevive from microsoft. How do we include the whole page with the...
4
by: VB Programmer | last post by:
I have an HTML newsletter that I want to email to a bunch of people found in a database. 1. I want some parts of the newsletter to filled in...
2
by: VB Programmer | last post by:
My client wants to make an HTML newsletter (using Word) then upload it to my website. I know how to send emails using system.web.mail. How do I...
0
by: ja | last post by:
To explain: I want to send HTML email from windows.forms app. I need to have textual templates which could be used for changing email bodies. ...
1
by: rahia307 | last post by:
hi everybody i want to send html email using php. please help me if any body have idea about it.
2
by: anu b | last post by:
Now i am sending email to my friend using session variable... but my code is as below private bool SendEmail(string email) { try
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.