tajidyakub
11/10/2017 - 11:39 PM

Menggunakan mailcatcher sebagai dummy smtp server di localhost untuk local development.

Menggunakan mailcatcher sebagai dummy smtp server di localhost untuk local development.

Local Development SMTP Server menggunakan Mailcatcher

  • Melihat apakah request yang terkait dengan pengiriman email dapat dilakukan dengan baik pada pengembangan di localhost
  • Minim konfigurasi, berjalan sebagai service namun dapat dimatikan dengan mudah.

Instalasi di macOS

$ sudo gem install mailcatcher
...
$ mailcatcher
Starting MailCatcher
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.

Start mailcather dengan menjalankan command mailcatcher di cli

Konfigurasi PHP melalui php.ini

$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/7.1
Loaded Configuration File:         /usr/local/etc/php/7.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.1/conf.d/ext-mcrypt.ini,
/usr/local/etc/php/7.1/conf.d/php-memory-limits.ini

# Edit file php.ini menggunakan nano / vim
# Edit direktif mengenail SMTP Port dan sendmail_path

$ vim /usr/local/etc/php/7.1/php.ini
; http://php.net/smtp-port
smtp_port = 1025
smtp_porsendmail_path = /usr/bin/env catchmail -f mail@localhost.devt = 1025
# Save dan Exit file
# Check config
$ php -i | grep "sendmail"
..
Path to sendmail => /usr/bin/env catchmail -f mail@localhost.dev

Kirim test email menggunakan curl

$ curl -v --insecure "smtp://localhost:1025" --mail-from "localmail@localhost.dev" --mail-rcpt "tajid.yakub@gmail.com" -T email-contents.txt
...
< 220 EventMachine SMTP Server
> EHLO email-contents.txt
< 250-Ok EventMachine SMTP Server
< 250-NO-SOLICITING
< 250 SIZE 20000000
> MAIL FROM:<localmail@localhost.dev> SIZE=167
< 250 Ok
> RCPT TO:<tajid.yakub@gmail.com>
< 250 Ok
> DATA
< 354 Send it
} [167 bytes data]
* We are completely uploaded and fine
< 250 Message accepted
100   167    0     0  100   167      0    766 --:--:-- --:--:-- --:--:--   766
* Connection #0 to host localhost left intact

Kemudian periksa di browser http://localhost:1080