Send an Email Later

10/06/2008

A friend came to me with a small problem. She wanted to email her boss, but she didn’t want him to know she had been up late; she wanted to send it first thing in the morning, when she wouldn’t be awake, and she’s on a Mac.

Apparently, Outlook and Entourage can do this, and I hunted around for a way to do it in either iCal or Mail.app, but I couldn’t find anything. An Automator script would be great, but she was going to bed soon and needed something quick.

I made two quick-and-dirty solutions to this, using the at daemon. First you have to turn it on. From the terminal, run the following (without the leading ‘#’):

# sudo launchctl \
  load -w \
  /System/Library/LaunchDaemons/com.apple.atrun.plist

That just tells Apple’s launchd program to run the at daemon, atrun.

Next, we’ll make a job for at to run later. Here’s the quickest-and-dirtiest way:

# echo \
  "echo \
  'This is the message body.' \
  | mail -s \
    'Email Subject' 'you@example.com'" \
  | at 7:30am

(Note that at takes the command to run on stdin; thus the redundant echos.)

It may seem like a jumbled mess, but, assuming you don’t use any quotes in the email body or subject, you can just replace them (and the email recipient) with exactly what you want. (If your subject or body do have quotes, you’ll have to escape them properly, which I’m not going to cover.)

The next method, which is probably easier if you want to send an email with more than one line in it, starts by opening a text file and writing your email. You can use “TextEdit” or any plain text editor. The trick here is to put some headers up top:

From: me@example.com
Reply-To: me@example.com
To: you@example.com
Subject: Yay! Email!

Here is your email.

Yay, email!

Yours truly,
Chewbacca

PS - Rawwwwrrrr!

Save this in your home folder as email.txt. You need those headers on top for the sendmail program to be able to tell where to send it.

Then, in the terminal, we’ll make an at job that uses this file:

# echo \
  "sendmail -t < ~/email.txt" \
  | at 7:30am"

And you’re done! If you want to test the at command first, replace “7:30am” with “now + 1 minute” (which means “1 minute from now”; how about that?), and change the recipient’s email address to your email address.

2 Comments