Tag Archive for 'exim'

cygwin+exim: setting up a local SMTP server for testing email applications

This is mainly a ref­er­ence for myself in case I forget how to do it later, as it took me a good deal of time to figure out.

Situation

I want to unit test appli­ca­tions that send emails. Obvi­ously I don’t want to be open­ing a dozen of real email accounts just to test this thing out. Nat­u­rally I would want to install an SMTP/POP3/IMAP server on Win­dows, and there is one free (open source) option that is quite promis­ing – hMailServer. How­ever, it still involves 2 (quite major) incon­ve­niences for my use case:

  • I have to go through the con­fig­u­ra­tion screens to create new users. i.e. if I want to send email to alice@localhost I’d have to create the user account alice manually
  • I have to use an email client (like Out­look Express) to con­nect to the local POP3 server to view the emails. This seems redun­dant to me, since the files are already stored in my disks, why can’t I just view them?

The Solution

Then I came across exim on cygwin. exim is (as it claims to be) a vast improve­ment over the good ol’ send­mail. So I gave it a shot – man, that was awe­some! I can now direct my appli­ca­tion to send to arbi­trary address like [email protected], [email protected] and I can view the results imme­di­ately in a text file, solv­ing the two prob­lems men­tioned above. Here’s what I did:

  • Install exim using cygwin installer
  • Run exim-config
  • Edit /etc/exim.conf
  • Uncom­ment primary_hostname and set it to mail.local
  • Edit the router localuser, com­ment out the line check_local_user (If you don’t know what that means, just search for the line localuser:)
  • Edit %WINDIR%\system32\drivers\etc\hosts, add the line 127.0.0.1 mail.local
  • Finally, reboot the exim service

And there you go! You can now send mail to *@mail.local. To view the mail, go to /var/spool/mail which con­tains all the mail in text files. A simple tail -f /var/spool/mail/foobar can mon­i­tor the mail sent to [email protected]

A simple test to make sure everything’s working:

$ exim -bt [email protected]
[email protected]
  router = localuser, transport = local_delivery
# If something is wrong in your config, it would say "Unrouteable address"
 
# Now let's try sending an email
$ exim -v -odf [email protected]
Hello World!
.
LOG: MAIN
  <= [email protected] U=Chris P=local S=296
delivering KEQKED-0002RS-A7
LOG: MAIN
  => foobar <[email protected]> R=localuser T=local_delivery
LOG: MAIN
  Completed
 
# Cool, let's see if it really delivered
$ tail /var/spool/mail/foobar
Received: from Chris by mail.local with local (Exim 4.69)
        (envelope-from <[email protected]>)
        id KEQKED-0002RS-A7
        for [email protected]; Sun, 08 Feb 2009 15:20:39 +0800
Message-Id: <[email protected]>
From: Chris <[email protected]>
Date: Sun, 08 Feb 2009 15:20:39 +0800
 
Hello World