Pages

Sunday, November 30, 2014

PHPMailer

I've been a stubborn hold out for just using the PHP mail() function. It was what I learned early on, and it worked wherever I needed it.

Well, this weekend I needed SMTP, and I decided to use an existing project instead of trying to roll my own. (It would have taken me forever, and probably would have come out pretty shabby too.)

I chose PHPMailer from some recommendations online. Another alternative is SwiftMailer. I couldn't tell you why one is better than the other, just that when I did my searches, PHPMailer seemed like the best choice for me at the time.

I was using this for a form submission, so the first thing I did was create an email address just for this project to use. Then, I put that account's  username and password into the appropriate properties in the script.

You can download the entire project from github.com/PHPMailer/PHPMailer

I only needed four files to make it work for me,

  • PHPMailerAutoload.php (Just to simplify the require statement)
  • class.phpmailer.php
  • class.pop3.php
  • class.smtp.php
Then, in the "examples" folder, I found the pop_before_smtp.phps file. I used this to create a test. I got that working and then tweaked it for my site in production.

For whatever reason, even with debugging set to zero, I was getting success messages echoed on screen. I'll probably go back later and figure out the real fix, but the quick work around was to wrap the whole thing in output buffering and throw away the buffer:

ob_start();
[PHPMailer code here]
ob_end_clean(); 
So, if you're looking for an alternative to the mail() function in PHP, you might want to try PHPMailer or SwiftMailer


No comments: