Sending Email from PHP

I remember one of my first paid programming gigs, it was to build a contact form for a friend’s website. Not one of those janky mailto: hacks that attempted to open the local email client. I’m talking about a form that would send mail from the server, you know, in case they didn’t have Outlook Express properly configured 😉 The first time I had to accomplish the task it was to be done in Perl but we can just pretend it was in PHP for the sake of the post 😉

Please keep in mind that in order to send email from your server you will need to have some sort of mail server installed and running. This can be simple as running sudo apt-get install exim4 (adjust accordingly for your distro and preferred mail server). I won’t be covering the installation, just the PHP portion of sending email (perhaps in another post?)

In PHP the simplest way to send an email is to call the mail() function:

mail('recipient@example.com', 'Subject Line!!~!', 'Hey, this is a message to you'

In it’s simplest form PHP will send an email from the user account that ran the script (www-data in most cases) with no formatting or anything.

Want to set the from address? You will need to use the optional 4th argument of mail() to set the message’s header:

$header = 'From: Example Sender <sender@example.com>'

mail('recipient@example.com', 'Subject Line!!~!', 'Hey, this is a message to you', $header

Definitely coming along, how about being able to use HTML to add some style to the message? To do that well need to set some more headers for the message’s content type:

$header  = 'From: Example Sender <sender@example.com>' . "rn"
		 . 'MIME-Version: 1.0' . "rn"
		 . 'Content-Tye: text/html; charset=ISO-8859-1'

$message = '<html><body>Hey, this is <strong>an HTML message</strong> to you</body></html>'

mail('recipient@example.com', 'Subject Line!!~!', $message, $header

Well lookie there, now we can use HTML in our message! Keep in mind that images typically aren’t displayed by default so you should not rely on them to convey your message.

A few other header’s that you may find handy are Reply-To, CC and BCC. They are set in the same way as the aforementioned headers. It’s worth noting that if the Suhosin patch is installed you may run into some headers being disallowed (based on the configuration). Specifically, CC and BCC would be disallowed.

One trick that I like to do when sending the information from a form vis email is to cut some corners and simply use print_r() to generate the message. It saves a bit of time since you don’t need to worry about formatting, works great when presentation is a nice to have but not mandatory and it is future proof from new fields being added to the form down the road.

mail('recipient@example.com', 'Subject Line!!~!', print_r($_POST, true

BOOM! Depending on what you have on your form you will end up with a message something like this:

Array ( [from] => sender@example.com [country] => US [age] => 25-34 [message] => Hey there! )

You can tidy it up a bit by using HTML and wrapping the print_r in &lt;pre&gt; tags.

Sending email from your server should be approached with a bit of caution, specifically in regard to either being a spammer or being mistaken for a spammer. First off, don’t spam from your server. Worst case scenario your hosting company could can you for being a deviant. Secondly, just because you’re not spamming doesn’t mean your message will make it to the recipient’s inbox. When sending mail from your server there are additional steps that should be taken to configure the mail server to send well-formed emails. This includes setting up SPF and DKIM and also setting up processes for handling bounce back emails. Simply put, if you need to send email that is guaranteed to make it to the inbox, you may want to look into a third party Email Service Provider but even then it’s not guaranteed.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.