Derek Sivers

Entrepreneur, programmer, avid student of life. I make useful things, and share what I learn.

How to send personalized emails

I think the single most common question I've been asked over the years is “What do you use to send out your personalized emails?

It's a short little PHP script, run on the command-line, directly on your webserver. The script and simple instructions are below.

Sorry I can't play tech-support for everyone, so for anything here you don't understand yet, please ask your web-hosting company how to do this for your site. (This page is at sivers.org/emailer.)

1. Keep your list in a spreadsheet

It's important that you do it with email address in column A and name in column B like this:
spreadsheet

2. Save as tab-delimited text

Under “Save As...”, every spreadsheet program has an option to save as plain text, with the columns separated with a “tab” character. For this example, name it list.txt. Upload it to your server.

3. Here's the PHP script that does it

<?php
$from = "Willy Wonka <willy@wonka.com>";
foreach(file('list.txt') as $line)
    {
    list($email, $name) = explode("\t", $line);
    list($firstname) = explode(' ', $name);
    $subject = "Hi $firstname! The chocolate is all yours.";
    $body = "Hi $firstname - 

Those other kids are awful. You deserve it all. Come and get it.

--
Willy Wonka - willy@wonka.com - http://wonka.com
";
    mail($email, $subject, $body, $from);
    print "$email sent\n";
    }
?>

Look at it slowly, even if you don't know PHP. It's pretty self-explanatory.

  1. It goes through each line of your list.txt file
  2. Breaks each line into $email and $name, separated by tab (\t)
  3. Breaks the name into words, taking the first one as $firstname
  4. Merges $firstname into the subject and body of the mail
  5. Emails it, using the customized $email, $subject and $body

Obviously, replace the $from, $subject, and $body for your own needs. Save it as mailer.php. Then upload it to your server.

4. Log in to the command-line on your server

This is the part your web-hosting company will have to tell you how to do. Using Terminal on Mac, or PuTTY on Windows, SSH into your server's command-line.

5. Run it!

On the command-line, where the mailer.php and list.txt files are, just type:

php mailer.php
... and everyone will be sent a customized email.