Send Email via SMTP from PHP

Aaron Poffenberger

Here's some code I put together to send email via smtp. I hadn't done it in a while and a colleague was having problems sending email to a large ISP using the standard the PHP mail function. A lot of large (and small) ISPs won't accept email from hosts that aren't either a registered MX record or have a valid RARP. Sending the email from a known SMTP server (in this case we used Yahoo) solved the problem.

The code isn't production ready in the sense that it's not a drop in class. It's organized and well commented and is meant to be an example to use in when sending mail through an SMTP server. It doesn't implement every possible SMTP option but has the key values: To, From, Subject, Reply-To, Subject and Body.

The code has simple error handling and displays a success message or the reason for failure.

The code uses Mail.php from the PEAR library. As the noted in the comments, you'll have dependencies on Mail and Net_SMTP from PEAR.

Happy emailing.

Git Repository: git clone

File: send_smtp_email.php License: ISC (BSD)