logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Local-mail-server.md (5217B)


  1. ---
  2. date: 2018-08-05
  3. layout: post
  4. title: Setting up a local dev mail server
  5. tags: [mail, instructional]
  6. ---
  7. As part of my work on [lists.sr.ht](https://meta.sr.ht), it was necessary for
  8. me to configure a self-contained mail system on localhost that I could test
  9. with. I hope that others will go through a similar process in the future when
  10. they set up [the code](https://git.sr.ht/~sircmpwn/lists.sr.ht) for hacking on
  11. locally or when working on other email related software, so here's a guide on
  12. how you can set it up.
  13. There are lots of things you can set up on a mail server, like virtual mail
  14. accounts backed by a relational database, IMAP access, spam filtering, and so
  15. on. We're not going to do any of that in this article - we're just interested in
  16. something we can test our email code with. To start, install your distribution
  17. of `postfix` and pop open that `/etc/postfix/main.cf` file.
  18. Let's quickly touch on the less interesting config keys to change. If you want
  19. the details about how these work, consult the postfix manual.
  20. - *myhostname* should be your local hostname
  21. - *mydomain* should also be your local hostname
  22. - *mydestination* should be `$myhostname, localhost.$mydomain, localhost`
  23. - *mynetworks* should be `127.0.0.0/8`
  24. - *home_mailbox* should be `Maildir/`
  25. Also ensure your hostname is set up right in `/etc/hosts`, something like this:
  26. ```
  27. 127.0.0.1 homura.localdomain homura
  28. ```
  29. Okay, those are the easy ones. That just makes it so that your mail server
  30. oversees mail delivery for the `127.0.0.0/8` network (localhost) and delivers
  31. mail to local Unix user mailboxes. It will store incoming email in each user's
  32. home directory at `~/Maildir`, and will deliver email to other Unix users. Let's
  33. set up an email client for reading these emails with. Here's my development
  34. [mutt](http://mutt.org) config:
  35. ```
  36. set edit_headers=yes
  37. set realname="Drew DeVault"
  38. set from="sircmpwn@homura"
  39. set editor=vim
  40. set spoolfile="~/Maildir/"
  41. set folder="~/Maildir/"
  42. set timeout=5
  43. color index blue default ~P
  44. ```
  45. Make any necessary edits. If you use mutt to read your normal mail, I suggest
  46. also setting up an alias which runs `mutt -C path/to/dev/config`. Now, you
  47. should be able to send an email to yourself or other Unix accounts with
  48. mutt[^1]. Hooray!
  49. [^1]: Mutt crash course: run `mutt`, press `m` to compose a new email, enter the recipient (`$USER@$HOSTNAME` to send to yourself) and the subject, then compose your email, exit the editor, and press `y` to send. A few moments later the email should arrive.
  50. To accept email over SMTP, mozy on over to `/etc/postfix/master.cf` and
  51. uncomment the submission service. You're looking for something like this:
  52. ```
  53. 127.0.0.1:submission inet n - n - - smtpd
  54. # -o syslog_name=postfix/submission
  55. # -o smtpd_tls_security_level=encrypt
  56. # -o smtpd_sasl_auth_enable=yes
  57. # -o smtpd_tls_auth_only=yes
  58. # -o smtpd_reject_unlisted_recipient=no
  59. # -o smtpd_client_restrictions=$mua_client_restrictions
  60. # -o smtpd_helo_restrictions=$mua_helo_restrictions
  61. # -o smtpd_sender_restrictions=$mua_sender_restrictions
  62. # -o smtpd_recipient_restrictions=
  63. # -o smtpd_relay_restrictions=
  64. # -o milter_macro_daemon_name=ORIGINATING
  65. ```
  66. This will permit delivery via localhost on the submission port (587) to anyone
  67. whose hostname is in `$mydestination`. A good old `postfix reload` later and you
  68. should be able to send yourself an email with SMTP:
  69. ```
  70. $ telnet 127.0.0.1 587
  71. Trying 127.0.0.1...
  72. Connected to 127.0.0.1.
  73. Escape character is '^]'.
  74. 220 homura ESMTP Postfix
  75. EHLO example.org
  76. 250-homura
  77. 250-PIPELINING
  78. 250-SIZE 10240000
  79. 250-VRFY
  80. 250-ETRN
  81. 250-ENHANCEDSTATUSCODES
  82. 250-8BITMIME
  83. 250-DSN
  84. 250 SMTPUTF8
  85. MAIL FROM:<sircmpwn@homura>
  86. 250 2.1.0 Ok
  87. RCPT TO:<sircmpwn@homura>
  88. 250 2.1.5 Ok
  89. DATA
  90. 354 End data with <CR><LF>.<CR><LF>
  91. From: Drew DeVault <sircmpwn@homura>
  92. To: Drew DeVault <sircmpwn@homura>
  93. Subject: Hello world
  94. Hey there
  95. .
  96. 250 2.0.0 Ok: queued as 8267416366B
  97. QUIT
  98. 221 2.0.0 Bye
  99. Connection closed by foreign host.
  100. ```
  101. Pull up mutt again to read this. Any software which will be sending out mail and
  102. speaks SMTP (for example, sr.ht) can be configured now. Last step is to set up
  103. LTMP delivery to lists.sr.ht or any other software you want to process incoming
  104. emails. I want most mail to deliver normally - I only want LTMP configured for
  105. my lists.sr.ht test domain. I'll set up some transport maps for this purpose. In
  106. `main.cf`:
  107. ```
  108. local_transport = local:$myhostname
  109. transport_maps = lmdb:/etc/postfix/transport
  110. ```
  111. Then I'll edit `/etc/postfix/transport` and add these lines:
  112. ```
  113. lists.homura lmtp:unix:/tmp/lists.sr.ht-lmtp.sock
  114. homura local:homura
  115. ```
  116. This will deliver mail normally to `$user@homura` (my hostname), but will
  117. forward mail sent to `$user@lists.homura` to the Unix socket where the
  118. [lists.sr.ht LMTP
  119. server](https://git.sr.ht/~sircmpwn/lists.sr.ht/tree/master/listssrht-lmtp) lives.
  120. Add the subdomain to `/etc/hosts`:
  121. ```
  122. 127.0.0.1 lists.homura.localdomain lists.homura
  123. ```
  124. Run `postmap /etc/postfix/transport` and `postfix reload` and you're good to go.
  125. If you have the lists.sr.ht daemon working, send some emails to
  126. `~someone/example-list@lists.$hostname` and you should see them get picked up.