The magic of PHPMailer

November 10, 2013 - 2 minutes

Recently I updated the website for my club, the Association for Women in Computing at WWU, to send e-mails via PHPMailer. We have a contact form with the usual subject, content, name, etc. and want to send all that to our e-mail address. The previous e-mail system went through the school’s network  and was very much broken, so it was in need of a good fix.

It was easier than I thought it would be, mostly just building some basic PHPMailer code. Add the script to your site, create a new instance of PHPMailer, and set the various properties on that instance. For example, if your mailer is $mail, you set the subject of the email like so:

$mail->Subject ="E-mail subject here";

Then once you’re done adding all the properties, you do $mail->Send, and you’re good to go!

For the server, I ended up just creating a new Gmail e-mail address so I could use Gmail as a host. So our host property looked something like this:

$mail->Host = "";

And then you use the Gmail address and login for the Username and Password properties. Simple and easy!

PHP does already have a built-in mail function, conveniently called mail(), which is fine for super simple e-mails but PHPMailer makes it easy to do more complicated things, like adding an attachment or using SMTP to send the e-mail. So unless you only want to do very basic things with mail in PHP, I’d stick with a library.

Official instructions and examples can be found on their Github.