In this tutorial, we’ll see how the PHP server-side scripting language can be used to send email, and explore how to send complex message types such as HTML email or emails with file attachments.
For convenience we will be using PHPMailer in this tutorial.
PHPMailer is a class for PHP that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments. PHPMailer is an efficient way to send e-mail within PHP.
Step 1: Downloading PHP mailer
- You can download the latest release from the PHPMailer website: PHPMailer
Before continuing, please be sure that PHPMailer is installed correctly. If you feel uncertain, please read the installion instructions that accompany the package.
Step 2: Sending our first email
In this first example we’ll be sending out a simple email, covering the basics of the PHPMailer package.
We start off by including the PHPMailer libary in our script:
1 | require("class.phpmailer.php"); // First we require the PHPMailer libary in our script |
Next we create a new object of this libary (a new object of the class PHPMailer) called $mail.
1 | $mail = new PHPMailer(); // Next we create a new object of the PHPMailer called $mail |
Now we enter the address that the e-mail should appear to come from. You can use any address that the SMTP server will accept as valid.
1 | $mail->From = "from@example.com"; |
Now we give the address an associated name. This will add ‘Your Name’ to the from address, so that the recipient will know the name of the person who sent the e-mail.
1 | $mail->FromName = "Your Name"; |
Now we add the to address, the address to which the e-mail will be sent. You must use a valid e-mail here, of course, if only so that you can verify that your PHPMailer test worked. It’s best to use your own e-mail address here for this inintial test.
1 | $mail->AddAddress("myfriend@example.net"); // This is the adress to witch the email has to be send. |
Now we set the subject of the email.
1 | $mail->Subject = "First PHP Email message"; // This is the subject of the email message. |
Next up is the actual message in the email, this is done by the $mail->body. You can use ‘\n’ for a new line.
1 | $mail->Body = "Hi! \n\n This is my first e-mail sent through PHP."; // This is the actual email message |
Finally, we send out the e-mail, once all necessary information has been provided. This is done with $mail->Send();. In this example script, it’s combined with an error message; if Send() fails, it’ll return false and you can catch it and display an error message.
1 2 3 4 5 6 7 8 9 | if(!$mail->Send()) // Now we send the email and check if it was send or not. { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } |
Here is the complete example:
The complete first example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php require("class.phpmailer.php"); // First we require the PHPMailer libary in our script $mail = new PHPMailer(); // Next we create a new object of the PHPMailer called $mail $mail->From = "from@example.com"; // this is the From adress (the adress the email came from) $mail->FromName = "Your Name"; $mail->AddAddress("myfriend@example.net"); // This is the adress to witch the email has to be send. $mail->Subject = "First PHP Email message"; // This is the subject of the email message. $mail->Body = "Hi! \n\n This is my first e-mail sent through PHP."; // This is the actual email message if(!$mail->Send()) // Now we send the email and check if it was send or not. { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?> |
Step 3: Sending your first HTML email
Sending out HTML e-mail is a easy task with PHPMailer, but it requires knowledge of HTML. Mail clients vary greatly in their support of HTML email, with some refusing to show it. For those mail clients that are not able to display HTML, you can provide an alternate email body containing the message as text.
We start the same way as example 1, but now we use the following changes:
We use the $mail->IsHTML(true) to tell the script that the email that will be sent is an HTML message.
1 | $mail->IsHTML(true); // This tell's the PhPMailer that the messages uses HTML. |
Then we create the HTML message the same way we did for the normal text one, using the $mail->Body. But this time we add HTML codes to it.
1 | $mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML !"; |
Now we make sure that people who can’t read HTML emails can also read the message, this is done by providing an Alternative body:
1 | $mail->AltBody = "Hello, my friend! \n\n This message uses HTML, but your email client did not support it !"; |
Next we send the email on the same way as example 1.
The complete HTML example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->From = "from@example.com"; $mail->FromName = "Your Name"; $mail->AddAddress("myfriend@example.net"); // This is the adress to witch the email has to be send. $mail->Subject = "An HTML Message"; $mail->IsHTML(true); // This tell's the PhPMailer that the messages uses HTML. $mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML !"; $mail->AltBody = "Hello, my friend! \n\n This message uses HTML, but your email client did not support it !"; if(!$mail->Send()) // Now we send the email and check if it was send or not. { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?> |
Step 4: Sending an email with attachements
Perhaps you need to attach something to your email, such as an image or an word document. Or perhaps you need to attach multiple files.
There are two ways of attaching something to your mail:
- You can simply attach a file from the filesystem
- or you can attach (binary) data stored in a variable.
The last one is called a Stringattachment. This makes it possible put extract data from a database and attach it to an e-mail, without having to save it as a file.
The command to attach a file can be placed anywhere between $mail = new PHPMailer() and $mail->Send() and it’s called AddAttachment($path);. This line will add an attachment to your email.
$path is the path of the filename. It can be a relative one or a full path to the file you want to attach.
If you want more options or you want to specify encoding and the MIME type of the file, then you can use 3 more parameters, all of which are optional:
1 | AddAttachment($path,$name,$encoding,$type); |
$name is an optional parameter, used to set the name of the file that will be embedded within the e-mail. The person who recieved your email will only see this name, rather than the original filename.
$encoding with this parameter you can set the type of encoding of the attachment. The default is base64.
$type is the MIME type of the attached file. This parameter makes it possible change the MIME type (MIME = Multipurpose Internet Mail Extensions) of an attachment from the default value of application/octet-stream (works with every kind of file) to a more specific MIME type, such as image/jpeg for a .jpg image.
String attachments work much like AddAttachment(), and is called with AddStringAttachment($string,$filename,$encoding,$type). The string data is passed to the method with the first parameter, $string. Because the string will become a standard file (which is what the attachment will be when received via email), the $filename parameter is required, it’s used to provide the filename for the string data.
So, why use AddStringAttachment instead of AddAttachment? Is it for text-only files? No, not at all. It’s primarily for databases. Data stored in a database is always stored as a string (or as a BLOB: Binary Large OBject). You could query your database for an image stored as a BLOG and pass the result to the AddStringAttachment.
If you want to attach multiple files (or strings), just call AddAttachment() or AddStringAttachment() multiple times.
Here is an example of howto attach a file to an email (using the HTML example above)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->From = "from@example.com"; $mail->FromName = "Your Name"; $mail->AddAddress("myfriend@example.net"); // This is the adress to witch the email has to be send. $mail->Subject = "An HTML Message"; $mail->IsHTML(true); // This tell's the PhPMailer that the messages uses HTML. $mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML !"; $mail->AltBody = "Hello, my friend! \n\n This message uses HTML, but your email client did not support it !"; $mail->AddAttachment("c:/temp/projectresults.zip", "results-october.zip"); // attaching a file to the email if(!$mail->Send()) // Now we send the email and check if it was send or not. { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?> |
useful!
great!
[...] entry was written by admin. Bookmark the permalink. Follow any comments here with the RSS feed for this post.Content related [...]
Hi Admin, im really new with php as in 0% knowledge.Do I have to save every php document with php file extension?
I have a ftp and smtp server but I dont know how to install the php file I downloaded from http://phpmailer.codeworxtech.com/ can you please enlighten me with this one. Thank you!
@ADMIN
How about one of your tutorials about sending multiple emails with phpmailer.
I am confy with linear PHP, but OOP is something I have steered cleared of thinking it will slow down my progession as a php programmer.
How wrong was I!
It would seem that OOP is the way of the future!
So, hows about helping an old dog learn another new practical OOP imlementation?
Des
I do not believe this
good one. Very usefull
your code doesn’t work on my pc
the screen display ‘Message has been sent.’
but the email never arrive on my account.
is there any other way?
I relly need to learn how to make my own phpmailer please i need you tutorial
Add A Comment