§2024-09-24

Creating Your Own E-Mail Service With Haraka, PostgreSQL, and AWS S3

試作機器: hc4Noble.yushei.net,

$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 24.04.1 LTS"
$ which node
/usr/bin/node
haraka@hc4Noble:~/build$ node --version
v18.19.1
haraka@hc4Noble:~/build$ which npm 
/usr/bin/npm
haraka@hc4Noble:~/build$ npm --version
9.2.0
  1. installing haraka
$ sudo npm install -g haraka
$ which haraka
/usr/local/bin/haraka
$ haraka --version
Haraka.js — Version: 3.0.4
  1. create a configuration for it
$ pwd
/home/haraka/build

$ haraka -i hc4NobleMailServerPostgres
create: /home/haraka/build/hc4NobleMailServerPostgres
create: /home/haraka/build/hc4NobleMailServerPostgres/plugins
create: /home/haraka/build/hc4NobleMailServerPostgres/docs
create: /home/haraka/build/hc4NobleMailServerPostgres/config
create: /home/haraka/build/hc4NobleMailServerPostgres/config/smtp.ini
create: /home/haraka/build/hc4NobleMailServerPostgres/config/log.ini
create: /home/haraka/build/hc4NobleMailServerPostgres/config/plugins

one important file to note right now is the config/smtp.ini file. you can change server listening details like the port, ip, etc. from that file. the default port for haraka is 25 , which requires root/admin privileges depending on the system you are using.

  1. DNS setup
$ dig hc4Noble.yushei.net mx

; <<>> DiG 9.18.28-0ubuntu0.24.04.1-Ubuntu <<>> hc4Noble.yushei.net mx
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29216
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;hc4Noble.yushei.net.		IN	MX

;; ANSWER SECTION:
hc4Noble.yushei.net.	7136	IN	MX	1 mail.hc4Noble.yushei.net.

;; Query time: 4 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Wed Sep 25 07:09:20 CST 2024
;; MSG SIZE  rcvd: 69

alexlai@hc4Noble:~$ dig mail.hc4Noble.yushei.net

; <<>> DiG 9.18.28-0ubuntu0.24.04.1-Ubuntu <<>> mail.hc4Noble.yushei.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58557
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;mail.hc4Noble.yushei.net.	IN	A

;; ANSWER SECTION:
mail.hc4Noble.yushei.net. 7135	IN	A	192.168.48.239

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Wed Sep 25 07:09:29 CST 2024
;; MSG SIZE  rcvd: 69
  1. config/host_list

when you want haraka to accept emails for your domain, you need to add it to the /path/to/haraka_test/config/host_list file. for example, to accept emails for thihara.com (i.e., thihara@thihara.com, tj@thihara.com) you need to add thihara.com to the host_list file as a single line.

$ cat  config/host_list 
hc4Noble.yushei.net
  1. smtp

it’s worth noting that the responsibility of an smtp server like haraka is two-fold.

receiving and accepting emails and then forwarding them to their destinations. sending outgoing emails. in no way will the smtp server itself store the emails, nor does it have any concept of an inbox or mailbox (where emails specific to a single user will be stored).

storing emails and maintaining inboxes is generally done by separate servers like imap or pop3.

  1. start it up
$ pwd
/home/haraka/build/hc4NobleMailServerPostgres
haraka@hc4Noble:~/build/hc4NobleMailServerPostgres$ sudo haraka -c ./
loaded TLD files:
  1=1448
  2=8320
  3=3571
loaded 9672 Public Suffixes
loglevel: INFO
log format: DEFAULT
[WARN] [-] [server] smtp.ini.nodes unset, using 1, see https://github.com/haraka/Haraka/wiki/Performance-Tuning
Starting up Haraka version 3.0.4
[INFO] [-] [plugins] loading mail_from.is_resolvable
[INFO] [-] [plugins] loading rcpt_to.in_host_list
[INFO] [-] [plugins] loading queue/smtp_forward
[NOTICE] [-] [server] worker started worker=1 pid=36583
loaded TLD files:
  1=1448
  2=8320
  3=3571
loaded 9672 Public Suffixes
loglevel: INFO
log format: DEFAULT
[WARN] [-] [server] smtp.ini.nodes unset, using 1, see https://github.com/haraka/Haraka/wiki/Performance-Tuning
Starting up Haraka version 3.0.4
[INFO] [-] [plugins] loading mail_from.is_resolvable
[INFO] [-] [plugins] loading rcpt_to.in_host_list
[INFO] [-] [plugins] loading queue/smtp_forward
[INFO] [-] [core] loading tls.ini
[ERROR] [-] [core] tls key /home/haraka/build/hc4NobleMailServerPostgres/config/tls_key.pem could not be loaded.
[ERROR] [-] [core] tls cert /home/haraka/build/hc4NobleMailServerPostgres/config/tls_cert.pem could not be loaded.
[NOTICE] [-] [server] Listening on [::0]:25
[NOTICE] [-] [server] worker 1 listening on [::0]:25
  1. test from other host
alexlai@hc4Bookworm:~$ telnet hc4Noble.yushei.net 25
Trying 192.168.48.239...
Connected to hc4Noble.yushei.net.
Escape character is '^]'.
220 hc4Noble ESMTP Haraka/3.0.4 ready
EHLO yushei .net
250-hc4Noble Hello [192.168.48.136], Haraka is at your service.
250-PIPELINING
250-8BITMIME
250-SMTPUTF8
250 SIZE 0
MALI FROM: alexlai@hc4Noble.yushei.net
500 Unrecognized command
MAIL FROM: alexlai@hc4Noble.yushei.net
450 No valid MX for your FROM address
MAIL FROM: alexlai@h2Jammy.yushei.net
250 sender <alexlai@h2Jammy.yushei.net> OK
RCPT TO: alexlai@h2Jammy.yushei.net,rai.sousuke@mac.com,rai.sousuke@gmail.com
501 Invalid RCPT TO address
RCPT TO: alexlai@h2Jammy.yushei.net
550 I cannot deliver mail for <alexlai@h2Jammy.yushei.net>
RCPT TO: rai.sousuke@mac.com
550 I cannot deliver mail for <rai.sousuke@mac.com>
RCPT TO: alexlai@hc4Noble.yushei.net                        
550 I cannot deliver mail for <alexlai@hc4Noble.yushei.net>
.
500 Unrecognized command
quit
221 hc4Noble closing connection. Have a jolly good day.
Connection closed by foreign host.
$ telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 hc4Noble ESMTP Haraka/3.0.4 ready
EHLO yushei.net
250-hc4Noble Hello ip6-localhost [::1], Haraka is at your service.
250-PIPELINING
250-8BITMIME
250-SMTPUTF8
250 SIZE 0
MAIL FROM: alexlai@yushei.net
250 sender <alexlai@yushei.net> OK
RCPT TO: alexlai@hc4Noble.yushei.net
250 recipient <alexlai@hc4Noble.yushei.net> OK
DATA
354 go ahead, make my day
Subject: 2024-09-25 07:23
This is a test
.
quit
450 plugin timeout (F3085D2F-EAB1-4B26-A94C-7A4B35DD2691.1)
221 hc4Noble closing connection. Have a jolly good day.
Connection closed by foreign host.

以下 kept for record onlt. Debian 12 Bookworm can nto install npm

  1. adduser haraka
$ sudo adduser  haraka
Adding user `haraka' ...
Adding new group `haraka' (1000) ...
Adding new user `haraka' (1000) with group `haraka (1000)' ...
Creating home directory `/home/haraka' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for haraka
Enter the new value, or press ENTER for the default
        Full Name []: haraka 
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] y
Adding new user `haraka' to supplemental / extra groups `users' ...
Adding user `haraka' to group `users' ...
alexlai@n2Jitsi:~$ sudo usermod -aG sudo haraka
alexlai@n2Jitsi:~$ id haraka
uid=1000(haraka) gid=1000(haraka) groups=1000(haraka),27(sudo),100(users)