Jul 10, 2016

f Comment

[SOLVED] How to Configure for a Third Party SMTP Host to Send Emails from Your Program?

Amazon If you want to send emails from your program or website, you should use a third party SMTP host so that your emails won't end up in the spam folder of your recipients. However, when you use a third party SMTP service to send emails, you must configure your program's email-related settings so that when your program calls the email-sending function, the email-sending function will know where to relay the email message to.

First, you must gather the necessary information about your third party SMTP host. You need the following information:

- SMTP host
- SMTP host's port
- SMTP username
- SMTP password

In my case, I use Mailrelay. And their SMTP-related information is posted in my account like the following:

SMTP access

This option allows you to activate the SMTP for your Mailrelay account.

Before activating the service, you will have to add the correct SPF record for your domain, you can find more information here.

Once the spf record is correct, click on "Activate".

If you need to send using SSL/TLS connection, use port 465. For more information, please contact us.

The SMTP service is active for your account.

SMTP Host my-domain.ip-zone.com
Port 465 (with SSL encryption) or 25/2525 (without SSL encryption)
SMTP Username my-domain

Like what it says above, you must add the correct SPF record for your domain. Each third party SMTP host is different, and they will tell you how to add the correct SPF record. In my case, my SPF record looks like this:

SPF my-domain.com v=spf1 include:my-domain.ip-zone.com a mx -all
SPF my-domain.com v=spf2.0/pra include: my-domain.ip-zone.com a mx -all
TXT my-domain.com v=spf1 include:my-domain.ip-zone.com a mx -all

And there's a section below that allows me to set my SMTP password. Therefore, my information is the following:

- SMTP host = my-domain.ip-zone.com
- SMTP host's port = 25 (because I don't use SSL)
- SMTP username = my-domain
- SMTP password = ...this is a secret...

There's one more step. You need to create a file with your username and password in it. Here are the steps:

1. Edit the /etc/postfix/sasl_passwd file. If the file does not exist, create it. Add the following lines to the file, replacing USERNAME and PASSWORD with your SMTP username and password.

[my-domain.ip-zone.com]:25 USERNAME:PASSWORD

Save and close the sasl_passwd file.

2. At a command prompt, issue the following command to create a hashmap database file containing your SMTP credentials.

sudo postmap hash:/etc/postfix/sasl_passwd

Now you are ready to configure your program's email-related settings. You need to know which email program your code is using. In my case, I use PHP, and the email program I use is Postfix. Therefore I must configure Postfix to use the third party SMTP host by editing /etc/postfix/main.cf. The main lines in /etc/postfix/main.cf are as follows:

relayhost = [my-domain.ip-zone.com]:25
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd_mailrelay

Save the changes and we are done! Try sending out an email from your PHP code and see if your recipient gets it! If the third party SMTP host is reliable, your email should NOT end up in the spam folder of the recipient's email box.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael