How to send e-mail newsletters using PHP and PHPMailer
Without going into the intricacies of HTML or plain text e-mails this tutorial gets stuck into the ‘nitty gritty’ code required to successfully send e-mail newsletters. While there are ready made solutions on the market to manage mailing lists and send out bulk e-mail, there’s nothing like building your own - with the ability to customise the whole process! Using PHP and MySQL this tutorial explains how to send a multi-part e-mail (one that contains both a HTML and plain text version) using the popular PHPMailer; a fully featured e-mail transfer class.
We are not going to explain how to best compose your e-mails, however, for an interesting article on coding HTML e-mail why not read “How to Code HTML Email Newsletters” by Tim Slavin. We assume that you already have both a plain text and HTML version of your newsletter ready to send out to a list of subscribers.
Let’s get started
Firstly, you will need to download the PHPMailer class. This is a class and therefore does not require any modification of the server. Simply ‘include’ the appropriate files into your PHP script. Download the latest version of PHPMailer fromphpmailer.sourceforge.net. For a full explanation and user manual refer to the PHPMailer website.
The code
The PHPMailer class contains 2 files; ‘class.phpmailer.php’ and ‘class.smtp.php’. Include the ‘class.phpmailer.php’ file at the start of your script.
<?php
#include PHPMailer class and database connection
include("class.phpmailer.php");
include("db.php");
?>
We’ll assume the HTML and plain text versions of your e-mail will be coming from a form. We must make sure we remove all the slashes which may have been added in PHP.
#remove slashes from content $html = stripslashes($_POST["html"]); $plain = stripslashes($_POST["plain"]);
Initiate the PHPMailer class and set some e-mail values, including from name and email address.
#initiate PHPMailer class $mail = new PHPMailer(); #set the from e-mail address $mail->From = "email@domain.co.uk"; #set the from name $mail->FromName = "Company Name"; #set the e-mail type to HTML $mail->IsHTML(true);
In this example we have a list of e-mail addresses stored in a MySQL database. The next step is to loop through all e-mail addresses and send out the e-newsletter.
#loop through e-mail addresses
$query = "SELECT * FROM table_email";
$result = mysql_db_query ("$databasename", $query);
while ($myrow = mysql_fetch_array($result)){
Now we send out the newsletter to each subscriber. We must add the plain text version for e-mail clients which don’t support HTML or for subscribers who choose to not receive HTML email.
#add subscribers address as the recipient $mail->AddAddress($myrow["fldEmail"]); #the subject of the email $mail->Subject = "Our Newsletter"; #the HTML content of the email $mail->Body = $html; #the plain text version $mail->AltBody = $plain; #sends the newsletter $mail->Send(); #clears the recipient address $mail->ClearAddresses(); }
Complete code
<?php
#include PHPMailer class and database connection
include("class.phpmailer.php");
include("db.php");
#remove slashes from content
$html = stripslashes($_POST["html"]);
$plain = stripslashes($_POST["plain"]);
#initiate PHPMailer class
$mail = new PHPMailer();
#set the from e-mail address
$mail->From = "email@domain.co.uk";
#set the from name
$mail->FromName = "Company Name";
#set the e-mail type to HTML
$mail->IsHTML(true);
#the subject of the email
$mail->Subject = "Our Newsletter";
#the HTML content of the email
$mail->Body = $html;
#the plain text version
$mail->AltBody = $plain;
#loop through e-mail addresses
$query = "SELECT * FROM table_email";
$result = mysql_db_query ("$databasename", $query);
while ($myrow = mysql_fetch_array($result)){
#add subscribers address as the recipient
$mail->AddAddress($myrow["fldEmail"]);
#sends the newsletter
$mail->Send();
#clears the recipient address
$mail->ClearAddresses();
}
?>
All done!
You will now have sent out a multipart e-mail to all of your subscribers. View the full PHPMailer online manual and tutorials for further information on how to customise this script.
Issues
Some shared hosting companies prevent mass mailings or cap the number of mailings being sent in a given period of time. It’s important to contact you hosting company to see what (if any) restrictions are placed upon bulk e-mail sending.
When sending out bulk e-mails it’s important to adhere to the privacy of your subscribers. Clear unsubscribe instructions should be given and if you’re sending out HTML newsletters, give the subscriber the option to receive plain text e-mails only.
For more information on any of the code featured above visit www.php.net for the online manual.

How to attach files?
by
Sathiya
To add an attachment to an e-mail using PHPMailer you can use the following code (place this after the HTML declaration):
$mail->AddAttachment(”../folder/file.ext”, “file.ext”);
Warning: If you are planning to send out a lot of e-mails then I would recommend that you upload the file onto the Internet and show the URL of the file, rather than attaching the file in the e-mail itself. This would save on bandwidth and the processing time to send out the e-mails.
Just some Questions about this, If i follow you, I must make a form that calls this file to send the email?
Lets say I have a page where I show the records from a table, can I make a link like..send to newsletter to each one that calls this file and send the info?
Thanks in advance
Thank you for your question. Yes - a form is used to capture the HTML and plain text to send to the list of recipients. The form contains the fields for the HTML and plain text versions to send out.
Did you mean a page showing a list of e-mails? If so, it is possible to list the email addresses with a link for each one to send out a newsletter. How would you actually compose the e-mail? This can be from a form or hard-coded into your script.
The principles of sending out the e-mail are the same. Instead of looping through the subscribers, you would add the e-mail address from the URL instead, to send to just the one subscriber.
I hope this helps - if you require further help please let me know.
First thanks for answer.
I was talking about something like this:
I have a page from where you can add, edit , delete registers from a table ( news ) and have another table ( emails ) with user data.
So I was thinking, if in the page from where I manage the news I can make a link to send any of this news to the emails from the table emails.
Hope you can understand me.
Can you please code this form so I can check the code and use on mine ?
Thanks!!
Hi, I was just playing with the code, the mail is sent but no content in the body is mail out.
I use a form that calls a file with the php code, in the form have to textareas call $html and $plain, son in the php page I use this in body and altbody , body = “$html”;… what I’m I doing wrong?
Thanks in advance
The best way to debug this is to break the problem down - try replacing the $html with just the text “Testing”, so…
$mail->Body = “Testing”;
$mail->AltBody = “Testing”;
If this works then you know it’s a problem with your $html value. Also, try printing out the $html variable, for example,
die(”Contents of HTML: “.$html);
This will stop the script and print out the HTML - if you see nothing then you know it’s a problem with the variable, otherwise it’s a problem with the way your sending out the email. Make sure your script is set to HTML.
Hi, sorry to “spam” with my comments, first let me Thanks for your support, and now, I have to say I GOT IT
I just use this
$mail->Body= ($_POST['html']);
So it takes the value of the textarea calls “html”
Excellent - glad to hear you got it sorted! Sometimes the trial and error approach works best!
Thanks a lot… have been looking for this coding for some time, after a few trial and errors, it seems to flick just fine..
Thank you very much. I was searching for it every where. Once again thanks alot.
hi i would like to know.you said that you extracted the e-mal addresses to be sent from mysql db,ok i understand that, but what if you have e-mails from another source like text file, how do you send them, ho do you extract them
a super basic question: how i call the send, or how use the function to start the sending?
Hi, i would like to know how can i use SMTP to send the emails using phpmailer…
Thank you very much Andy for this handy article.
Glad i dropped by, added you to my bookmarks.
Cheers! Happy Holidays!
Hi,
IS there any configure on pop3 or smtp server for sending mail
I got Message When run script But Message not see in Server MailBox.
So Please Help me……….
Anyone have any info on using the latest ver.
of phpmailer with tinymce? I have been messing with the code,
can’t get it to work. I am thinking, replacing getfile might pull the data from the body of the form text area. $body = $mail->getFile(’contents.html’);
Veigh
Let say I got thousand of recipient, and there is limitation of our hosting company that I can only send out 50 email each time, how to config to send those email batch by batch in a period of time ?
Thx for this, I just wasted couple of hours for sending pure html email.
This helped me , thx for the author.
Anand kumar.
Does above script will work if there are more than 2000 emails ?
If no then what will be the solution for it ? thanks in advance
i have 1 doubt in this i tried to do that but it didnt work to me,my question is while sending the HTML format of mail or plain text we will have to add “Hi,Uday” this Uday comes from the database how do i add this to the body of the mail….pls help me in this part.
Thanks in advance
Thanks for the great tutorial! Can you personalize the body of the email; i.e. “Dear $Name,” if this info is also available in the database queried?
Thanks!
Hi - thanks for laying this out so easily.
I understand that the mysql connection string needs to be in the db.php file, however what is the syntax for this file ?
Need some help over here
i need a rate of 200mail per hours
where are the config to limit this?