How to send email using phpmailer
How to send email using phpmailer
I have seen a lot of users having issues using their own hosting account to relay emails from the basic mail() function. I’m writing this to show you a way to send email using phpmailer library using a SMTP server like gmail or any other SMTP server. This library can still be used to send mails using the mail() function, and most scripts out there, even WordPress, Drupal, SugarCRM, Yii and Joomla! if I’m not wrong, uses phpmailer. This is a really good library to be honest and has grown over the years a lot.
The features offered by this library are:
- It has integrated SMTP support
- You can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
- Multipart/alternative emails for mail clients that do not read HTML email
- You have support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
- It provides SMTP authentication with LOGIN, PLAIN, NTLM and CRAM-MD5 mechanisms over SSL and TLS transports
- Native language support
- DKIM and S/MIME signing support
- It is compatible with PHP 5.0 and later
So as you can see you get a pretty good amount of features here.
You can download the latest version of phpmailer from here:
Using phpmailer
Using this is not really that hard, rather is really easy. What you do need to do is after you downloaded the zip archive to your computer, unzip it to your document root of your site. Then include the library into your own application using something similar to:
1 2 3 |
<?php require '/path/to/PHPMailerAutoload.php'; ?> |
Once you’ve done this, you are all set to go and start using the library.
A simple example, based on phpmailer snippet is bellow:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php require '/path/to/PHPMailerAutoload.php'; $mail = new PHPMailer; //$mail->SMTPDebug = 3; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'user@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to $mail->From = 'from@example.com'; $mail->FromName = 'Mailer'; $mail->addAddress('contact@exmple.com', 'Contact email'); // Add a recipient $mail->addAddress('someuser@example.com'); // Name is optional $mail->addReplyTo('replyback@example.com', 'Reply Back'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); $mail->WordWrap = 50; // Set word wrap to 50 characters $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?> |
When testing and working with this, is best to make sure you use the debug option so that you receive an output back from the script when it finished sending the message or if it failed sending the message.
Its also best to know that if you are using SMTP, make sure to enable Authentication by specifying $mail->SMTPAuth to true
For localization you can also use something like:
1 2 |
// To load the French version $mail->setLanguage('fr', '/optional/path/to/language/directory/'); |
You can download the languages from:
https://github.com/PHPMailer/PHPMailer/tree/master/language
That’s it for now, will see you again in our next tutorial.