Solution to send mails from development systems/servers - Strictly for developers


I faced this issue many times when I had to test the mail feature in my projects. I then had to add SMTP library separately to check in different web based mails like Google Mail, Yahoo Mail, Outlook, etc. or need to upload the code to production servers where mails worked.


Mandrill (sends to any really account - preferred for UAT and Production systems) and Mailtrap (sends to single account for any email address - preferred for Development systems) provides a very good way to do this. With small changes in configuration of the server we can easily use this to send mails to any account without having SPAM issues.


Mandrill can also be a very useful tool to send mails in production if Client has budget or the number of mails are very limited. It provides tracking tools, graph and we can even configure BCC on all mails without changing code for debugging purpose.

Today, I will cover how to configure it in Postfix (Linux Ubuntu/CentOS/Amazon Linux) and XAMPP using Sendmail.


Installing Postfix


Installing Postfix is easy, just run this command as root:
yum install postfix mailx cyrus-sasl-plain


Configuring in Postfix


Basically, you need to create a password file so that Postfix can authenticate to Gmail's servers. You do this by creating a file named sasl_passwd in /etc/postfix. Replace smtp_user and smtp_passwd with their respective values.


Step 1A (Mandrill):
echo "smtp.mandrillapp.com    smtp_user:smtp_passwd" > /etc/postfix/sasl_passwd

Step 1B (Mailtrap):
echo "smtp.mandrillapp.com    smtp_user:smtp_passwd" > /etc/postfix/sasl_passwd



You then hash that file so that the password is not stored in clear text. This command will create a file named sasl_passwd.db in the /etc/postfix/ directory.

Step 2: postmap hash:/etc/postfix/sasl_passwd


After that is done, add these to the bottom of /etc/postfix/main.cf. This is assuming that your root certificates installed from openssl are located in /etc/pki/tls/certs/ca-bundle.crt.

Step 3A (Mandrill):

smtp_sasl_auth_enable = yes

smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
# Secure channel TLS with exact nexthop name match.
smtp_tls_security_level = secure
smtp_tls_mandatory_protocols = TLSv1
smtp_tls_mandatory_ciphers = high
smtp_tls_secure_cert_match = nexthop
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
relayhost = smtp.mandrillapp.com:587


Step 3B (Mailtrap):

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

relayhost = mailtrap.io:2525


After that is done, restart postfix:

Step 4: service postfix restart


Now test it to make sure it is working. Run this:

Step 5: mail email@domain

Fill in the subject, put something in the body and then type a . and hit enter.

If all went well, you should get an email at the email address you entered. If you do, you can delete the file that has the password.

Step 6: rm /etc/postfix/sasl_passwd

If it did not work, check the log to see what happened.

Step 7: tail /var/log/maillog

Everything should be good after you get everything set up, so enjoy your new SMTP relay with Mandrill or Mailtrap!






Configuring in XAMPP


To do the same in XAMPP, we need to add sendmail. We can get sandmail from http://www.glob.com.au/sendmail/sendmail.zip

Extract the zip inside XAMPP directory.

Open sendmail.ini and change the settings inside as below:

[sendmail]
#smtp_server=smtp.mandrillapp.com

smtp_server=mailtrap.io
#smtp_port=587
smtp_port=2525
error_logfile=error.log
debug_logfile=debug.log
auth_username=[Email address for Mandrillapp or Mailtrap username here]
auth_password=[API key for Mandrill or password for Mailtrap.io]
force_sender=
[Email address]


Open php.ini, uncomment sendmail_path and change the path to where you have extracted sendmail:
[mail function]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"


Hope this helps you to test your mails that you develop.

Comments

Popular posts from this blog

Few tips to speed up your system - Strictly for developers