Inbucket - Self Hosted Disposable Email Server

27 Januari 2021 • 3 menit untuk membaca artikel ini

Latar Belakang

Jadi gini, aku pengen punya satu email server dengan kriteria:

  1. Inbound only, email server cuma bisa nerima tanpa bisa ngirim. Demi mencegah terjadinya open relay dan nyebarin spam.
  2. Catch all, email server akan menerima semua email dengan alamat apapun yang ditujukan kepadanya tanpa harus aku bikin akun emailnya.
  3. Web UI, email server didukung oleh web ui untuk melihat email-email yang masuk .
  4. REST API, kalau bisa ada ini untuk integrasi ke mainan lain.
  5. Instalasi sederhana, aku tidak mau overkill untuk hal semacam ini. Maunya yg sederhana, run single binary dan beres.

Pernah dengar temp-mail? atau Mailinator? atau mailsac? nah kurang lebih fungsinya sama seperti itu. Nama hits-nya adalah “Disposable email”, ada beberapa manfaat:

  1. Mendaftar layanan dengan email sembarangan, jadinya gk ngotorin inbox email pribadi/utama.
  2. Melakukan testing aplikasi tanpa harus kirim email ke dunia luar, tetap private.

Setelah menyelam, aku dapat dua kandidat:

  1. MailHog, Web and API based SMTP testing.
  2. Inbucket, Disposable webmail server with built in SMTP, POP3, RESTful servers; no DB required.

Setelah lihat-lihat, pilihan jatuh ke Inbucket dengan alasan development masih aktif.

Setup Inbucket Disposable email server di Debian 10

Pertama, aku generate config untuk inbucket dengan configurator yang mereka sediakan, silakan buka Inbucket Configurator

Hasil akhirnya adalah sebagai berikut, aku ambil yg versi systemd:

Environment=INBUCKET_LOGLEVEL=error
Environment=INBUCKET_MAILBOXNAMING=full
Environment=INBUCKET_SMTP_ADDR=0.0.0.0:25
Environment=INBUCKET_SMTP_DOMAIN=emx.sumarsono.com
Environment=INBUCKET_SMTP_MAXRECIPIENTS=100
Environment=INBUCKET_SMTP_MAXMESSAGEBYTES=10240000
Environment=INBUCKET_SMTP_DEFAULTACCEPT=false
Environment=INBUCKET_SMTP_ACCEPTDOMAINS=sumarsono.com,nalakawula.com
Environment=INBUCKET_SMTP_DEFAULTSTORE=false
Environment=INBUCKET_SMTP_STOREDOMAINS=sumarsono.com,nalakawula.com
Environment=INBUCKET_SMTP_TIMEOUT=300s
Environment=INBUCKET_POP3_ADDR=127.0.0.1:1100
Environment=INBUCKET_POP3_DOMAIN=sumarsono.com
Environment=INBUCKET_POP3_TIMEOUT=600s
Environment=INBUCKET_WEB_ADDR=127.0.0.1:9000
Environment=INBUCKET_WEB_UIDIR=/usr/local/share/inbucket/ui
Environment=INBUCKET_WEB_GREETINGFILE=/etc/inbucket/greeting.html
Environment=INBUCKET_WEB_TEMPLATECACHE=true
Environment=INBUCKET_WEB_MAILBOXPROMPT=@sumarsono.com
Environment=INBUCKET_WEB_COOKIEAUTHKEY=tKa8mGD7tqwCuYezdbE4chkQRxSotpCfs4ucB
Environment=INBUCKET_WEB_MONITORVISIBLE=true
Environment=INBUCKET_WEB_MONITORHISTORY=30
Environment=INBUCKET_STORAGE_TYPE=file
Environment=INBUCKET_STORAGE_PARAMS=path:/var/local/inbucket
Environment=INBUCKET_STORAGE_RETENTIONPERIOD=720h
Environment=INBUCKET_STORAGE_RETENTIONSLEEP=86400s
Environment=INBUCKET_STORAGE_MAILBOXMSGCAP=100

Catatan:

  1. Aku gak butuh POP3, makanya bind ke 127.0.0.1
  2. Email server akan menerima alamat apapun@sumarsono.com dan apapun@nalakawula.com
  3. Email yang masuk akan aku keep 30 hari
  4. Alamat web aku bind ke 127.0.0.1:9000 karena aku mau pakai reverse proxy untuk terminate SSL dan BasicAuth

Kedua, aku unduh paket inbucket dari halaman rilis inbucket

root@emx:~# wget https://github.com/inbucket/inbucket/releases/download/v3.0.0-rc1/inbucket_3.0.0-rc1_linux_amd64.deb

Ketiga, tentu saja aku install

root@emx:~# dpkg -i inbucket_3.0.0-rc1_linux_amd64.deb 

Keempat, aku edit inbucket.service

root@emx:~# nano /lib/systemd/system/inbucket.service

# isinya sesuaikan dengan hasil generate di tahap 1
[Unit]
Description=Inbucket Disposable Email Service
After=network.target

[Service]
Type=simple
User=daemon
Group=daemon
PermissionsStartOnly=true

# default conf
#Environment=INBUCKET_LOGLEVEL=warn
#Environment=INBUCKET_SMTP_ADDR=0.0.0.0:2500
#Environment=INBUCKET_POP3_ADDR=0.0.0.0:1100
#Environment=INBUCKET_WEB_ADDR=0.0.0.0:9000
#Environment=INBUCKET_WEB_UIDIR=/usr/local/share/inbucket/ui
#Environment=INBUCKET_WEB_GREETINGFILE=/etc/inbucket/greeting.html
#Environment=INBUCKET_STORAGE_TYPE=file
#Environment=INBUCKET_STORAGE_PARAMS=path:/var/local/inbucket

# Hasil generate tahap 1
Environment=INBUCKET_LOGLEVEL=error
Environment=INBUCKET_MAILBOXNAMING=full
Environment=INBUCKET_SMTP_ADDR=0.0.0.0:25
Environment=INBUCKET_SMTP_DOMAIN=sumarsono.com
Environment=INBUCKET_SMTP_MAXRECIPIENTS=100
Environment=INBUCKET_SMTP_MAXMESSAGEBYTES=10240000
Environment=INBUCKET_SMTP_DEFAULTACCEPT=false
Environment=INBUCKET_SMTP_ACCEPTDOMAINS=sumarsono.com,nalakawula.com
Environment=INBUCKET_SMTP_DEFAULTSTORE=false
Environment=INBUCKET_SMTP_STOREDOMAINS=sumarsono.com,nalakawula.com
Environment=INBUCKET_SMTP_TIMEOUT=300s
Environment=INBUCKET_POP3_ADDR=127.0.0.1:1100
Environment=INBUCKET_POP3_DOMAIN=sumarsono.com
Environment=INBUCKET_POP3_TIMEOUT=600s
Environment=INBUCKET_WEB_ADDR=0.0.0.0:9000
Environment=INBUCKET_WEB_UIDIR=/usr/local/share/inbucket/ui
Environment=INBUCKET_WEB_GREETINGFILE=/etc/inbucket/greeting.html
Environment=INBUCKET_WEB_TEMPLATECACHE=true
Environment=INBUCKET_WEB_MAILBOXPROMPT=@sumarsono.com
Environment=INBUCKET_WEB_COOKIEAUTHKEY=tKa8mGD7tqwCuYezdbE4chkQRxSotpCfs4ucB
Environment=INBUCKET_WEB_MONITORVISIBLE=true
Environment=INBUCKET_WEB_MONITORHISTORY=30
Environment=INBUCKET_STORAGE_TYPE=file
Environment=INBUCKET_STORAGE_PARAMS=path:/var/local/inbucket
Environment=INBUCKET_STORAGE_RETENTIONPERIOD=720h
Environment=INBUCKET_STORAGE_RETENTIONSLEEP=86400s
Environment=INBUCKET_STORAGE_MAILBOXMSGCAP=100 

# Uncomment line below to use low numbered ports
ExecStartPre=/sbin/setcap 'cap_net_bind_service=+ep' /usr/local/bin/inbucket

ExecStartPre=/bin/mkdir -p /var/local/inbucket
ExecStartPre=/bin/chown daemon:daemon /var/local/inbucket

ExecStart=/usr/local/bin/inbucket

# Give SMTP connections time to drain
TimeoutStopSec=20
KillMode=mixed

[Install]
WantedBy=multi-user.target

Kelima, enable dan start inbucket.service

root@emx:~# systemctl daemon-reload
root@emx:~# systemctl enable --now inbucket.service

# verifikasi port-nya
root@emx:~# ss -tulpn | grep inbucket
tcp     LISTEN   0        128            127.0.0.1:1100          0.0.0.0:*       users:(("inbucket",pid=13101,fd=8))                                            
tcp     LISTEN   0        128              0.0.0.0:25            0.0.0.0:*       users:(("inbucket",pid=13101,fd=6))                                            
tcp     LISTEN   0        128            127.0.0.1:9000          0.0.0.0:*       users:(("inbucket",pid=13101,fd=7))                                            

Keenam, bikin dns record, aku cuma butuh value ini

# a record emx.sumarsono.com
emx.sumarsono.com.		20 IN A	1xx.2xx.1xx.xx

# webui inbucket
anuan.sumarsono.com.	300 IN CNAME emx.sumarsono.com.

# mx record sumarsono.com
sumarsono.com.		300 IN MX 10 emx.sumarsono.com.

# mx record nalakawula.com
nalakawula.com.		300 IN MX 10 emx.sumarsono.com.

Ketujuh, bikin vhost (kebetulan pakai apache httpd) untuk reverse proxy webui inbucket dan websocketnya:

VirtualHost *:80>
	ServerName anuan.sumarsono.com
	
	<Proxy *>
		AuthType Basic
		AuthName "Restricted Content"
		AuthUserFile /etc/apache2/.htpasswd-inbucket
		Require valid-user
	</Proxy>

	ProxyPreserveHost On
	ProxyPass / http://127.0.0.1:9000/
	ProxyPassReverse / http://127.0.0.1:9000/
	
	RewriteEngine on

	RewriteCond %{REQUEST_URI} ^/api/v1/monitor/messages    [NC]
	RewriteCond %{HTTP:Upgrade}  =websocket                 [NC]
	RewriteRule /(.*) ws://127.0.0.1:9000/$1                [P,L]
	RequestHeader unset Origin  
</VirtualHost>

Kedelapan, test kirim email ke sembarang@sumarsono.com dan sembarang@nalakawula.com, dalam hal ini aku coba ngirim dari gmail, emailnya masuk ke inbucket.

Uji coba inbucket

dan jika aku klik salah satu email tersebut maka akan tampil:

Uji coba inbucket 2

Selesai.

Cool~

Techlinuxemail serverinbucket

Sumarsono

System Administrator
Kembali ke atas

Melindungi Debian 10 Server Pakai Sshguard Dan Firewalld>>

<<Firewalld Nftables Debian 10