WP-Mix

A fresh mix of code snippets and tutorials

Minimal, optimal email headers

I recently had a conversation with an email guru concerning the ideal headers to use when sending plain-text email messages. Here is the punchline of that insightful exchange..

Best headers

The best minimal set of headers to use when sending email:

From:, Reply-To:, Content-Type:

I also like to add X-Mailer:, but it’s not required.

Here is an example showing how these headers might be used when sending email via a PHP script:

$headers  = 'X-Mailer: Simple Basic Contact Form'. "\n";
$headers .= 'From: '. $name .' <'. $email .'>'. "\n";
$headers .= 'Reply-To: '. $name .' <'. $email .'>'. "\n";
$headers .= 'Content-Type: text/plain; charset='. get_option('blog_charset', 'UTF-8') . "\n";

This snippet is written in the context of my free WordPress plugin, Simple Basic Contact Form, but the logic applies to any email that you want to send. Just change the WP stuff (i.e., get_option()) to whatever you’re working with.

Note: The Reply-To header is not technically required if it has the same value as the From header. You don’t need both, but it’s fine to include both if desired. Totally up to you :)

Notes

Some related infos:

  1. You can have only one email address in a From header, so don’t do this: From: Path Smith <test1@example.com, test2@example.com>
  2. If your Content-Type is plain, you shouldn’t be setting the MIME-Version header.
  3. Setting the Return-Path header is an RFC violation, don’t set it.
  4. Always set the Reply-To header of a contact form to be the visitor’s email address; that will enable the recipient to reply regardless of what shows up as the From address (e.g., Reply-To: [visitor-name] <[visitor-email]>

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress