PHP Mail: Send Text and HTML Mail Through PHP Script

In this article, I will show you how to use PHP mail to send a text/html message through a PHP script.

A client of mine today wanted to begin an email campaign to his subscriber list that included 10,000 people. He wanted the email sent to be HTML for those who view mail with HTML enabled and text mail for those who don’t.

I have done this before but I had to brush up on how to send mail via PHP in both HTML and text format. For you (and so I don’t have to brush up on this the next time), I will show you how to do this.

Step 1: Download PHPMailer

PHPMailer is the best way to send mail using PHP. Of course, you could code everything yourself. But why? Download PHPMailer here.

After you have downloaded and extracted PHPMailer, install PHPMailer by copying the following files to your server as it states on the install page: class.phpmailer.php, languages/phpmailer.lang-en.php. If you put these files in the same directory as the file below, that is the easiest approach.

Step 2: Configure Your PHP Mail File

PHP is an incredible scripting language but I am never quite satisfied with the examples that I am given. This holds true with PHPMailer as well. Thus, I am going to give you the exact PHP file that you can use and, hopefully, I will do a passable job adding comments and making this file pretty clear.

You can find the exact PHP file to use directly below, but first, let me give you a couple of notes. Copy the information below into your favorite text editor and change the variables in the file (email names, addresses, text and html messages) . Then, upload the file to your server and access it. Voila–your mails will be sent. Those who can read HTML messages will receive the HTML version and text-based email users will receive the text-based version.

PHPMailer Sample File: HTML and Text Message Sent Within Email



<?

$recipientfirstname="Jim";
$recipientfirstlastname="Jim Truckee";
$recipientemailaddress="jimtruckee@yahoo.com";  // dont't email this guy...I just made up the address
$thesubject="$recipientfirstname -- I Have Great Information For You!";
$senderaddress="you@yourdomainname.com";
$sendername="YourFirstName YourLastName";



// html text message...change this to your liking
$myhtmlmessage="Hi $recipientfirstname, <b>I have great news</b>!<br /><br />

I have just come across a website that shows me <b>how to send an email in HTML and text format</b>.  You must check it out.<br /><br />You will learn amazing things, like how to send an HTML or Text message through a PHP program!  Read more here >>> http://richardcummings.info/php-mail-send-text-html-mail-multipart/ <br />";
// end of html text message...

// simple text messsage...change this to your liking
$mytextmessage="Hi $recipientfirstname, I have great news!<br /><br />

I have just come across a website called Richard Cummings:  http://richardcummings.info/ .  You must check it out.  You will learn amazing things, like how to send an HTML or Text message through a PHP program!";
$mytextmessage= str_replace("<br />",'',$mytextmessage);  // this removes the <br />'s or you can take them out manually

require_once 'class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch



try {
  $mail->AddReplyTo($senderaddress, $sendername);  // you can change this to a "do not reply address"
  $mail->AddAddress($recipientemailaddress, $recipientfirstlastname);
  $mail->SetFrom($senderaddress, $sendername);
  $mail->Subject = $thesubject;
  $mail->AltBody = $mytextmessage; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML($myhtmlmessage);
  $mail->Send();
  echo "Message Sent OK\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
?>

Conclusion: Send Text and HTML Mail Through PHP Script

By changing the variables in the file above, you should be able to successfully send a message using this script as long as your web host supports PHP.

If you know a bit of MySql, you see how I will make this file a part of a loop and email everybody on the 10,000 person mailing list. However, I will be doing just 200 per day at the clients request and I will accomplish this via a Cron Job…but that is for another discussion.

I hope that you are able to use the template file above to send text and html messages via the PHPMailer script.

Cheers,
Richard

Appendix: Text/HTML Email Web Pages You May Want To Visit

Here are some pages that you may want to review regarding PHPMailer if you have any questions:

PHP Mailer Example
Sending Email with PHP and PHPMailer Tutorial

And a good discussion of text/HTML email can be found here:
Should I Use Text or HTML Email?

Richard CummingsPHP Mail: Send Text and HTML Mail Through PHP Script

Leave a Reply

Your email address will not be published. Required fields are marked *