In case you have a VPS and you don’t want to create an email server or your hosting panel doesn’t have an email hosting solution then you will need to add a relay email server so the VPS to use that for sending your emails.
It’s a good practice to use an SMTP relay anyway as if the email server is not configured OK or port 25 is closed by the VPS provider emails will not be sent.
I have installed CloudPanel to host my websites on a Hetzner VPS and that panel is not having any email-sending options that are why I want to add an SMTP relay in Postfix so all emails from WordPress websites or other apps to go thru it.
For more details about these 2 services, you can check CloudPanel Install and Hetzner Review.
Also, WordPress can be configured to send emails from the Admin area with the help of a plugin easily, so you can follow the Send Emails With Zoho or Send Emails with Gmail to configure WordPress directly if this tutorial is too hard.
As an SMTP server, I am using the Zoho Zeptomail, you can buy 2.5$ – 10.000 transactional emails that are valid for 6 months. You have also some free SMTP alternatives out there but I prefer Zeptomail as the price is not high and I am already using Zoho.
In this step you need to add your domain to zeptomail and verify it, you sign up, buy 1 credit and add the domain in the Domain area. After they will ask you to verify your domain by adding 3 entries in the DNS (SPF,DKIM,CNAME). This should be straight forward as any other SMTP provider. In the Configuration section, you will have the details that you will need next, server, user, and password for SMTP.
If Postfix is not installed you will need to install it first, to do that you run:
sudo apt-get update
sudo apt install -y mailutils
Here you choose Internet Site and enter the email domain, in my case bitdoze.com for this test.
Hetzner comes with postfix installed to you don’t need to install it. Next, we will need to configure it.
To configure it manually you should first edit the /etc/postfix/main.cf and add the below lines with the relayhost = that is the SMTP details from Zoho Zeptomail
Edit:
sudo nano /etc/postfix/main.cf
At the end add the below:
# outbound relay configurations
relayhost = [smtp.zeptomail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
header_size_limit = 4096000
sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps = regexp:/etc/postfix/sender_canonical
smtp_header_checks = regexp:/etc/postfix/smtp_header_checks
Create the sasl_passwd
file which will store our credentials.
sudo nano /etc/postfix/sasl_passwd
In here you need to insert the SMTP details in the below format:
[smtp.zeptomail.com]:587 username:password
You should use your own username and password from the Zoho account.
Next we need to create the hash database that will be used by postfix:
sudo postmap /etc/postfix/sasl_passwd
For added security, we will only allow the root user to read and write to sasl_passwd
and sasl_passwd.db
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
In Zeptomail if you let the config like this and you want WordPress to send an email you will get the below error message:
553 Relaying disallowed as wordpress@domain (in reply to end of DATA command))
This is happening because the server tries to use the WordPress domain to send emails and this is not allowed in Zeptomail, also to reduce the risk of email reaching the spam folder you should send the email with the domain you have configured.
We can force postfix to use the email with want with the below files:
In the /etc/postfix/sender_canonical we add a regexp that will replace our email with what we want:
/.+/ [email protected]
[email protected] should be an email belonging to the approved domain like: [email protected] in my case, or [email protected]
The second file that needs to be edited and changed in the email header details is: /etc/postfix/smtp_header_checks
/From:.*/ REPLACE From: [email protected]
And we are using the email address that we have used in smtp_header_checks.
You can even customize the name with the help of the below:
/From:.*/ REPLACE From: NameYouWant <[email protected]>
In these 2 files, you can create more advanced regexp if you want.
I my case I have created my VPS with cloud.bitdoze.com and when the email was sent it was trying to use this one, all the emails were failing as it was using the subdomain. To fix this you need to edit the /etc/mailname and be sure is pointing to the TLD domain
sudo nano /etc/mailname
yourdomain.com
For all this to work we need to restart/reload postfix:
sudo postfix reload
Now what remained to do is to send a test email and see if the email goes thru:
echo "test message" | mail -s "test subject" [email protected]
If you have WordPress websites installed on your CloudPanel you can try resetting the password to one and see if the mail goes thru, you can check the /var/log/mail.log for details, you should see:
Aug 12 08:09:24 cloud postfix/smtp[18711]: 0C0E63EE01: replace: header From: WordPress <[email protected]>: From: [email protected]
Aug 12 08:09:24 cloud postfix/smtp[18711]: 0C0E63EE01: to=<[email protected]>, relay=smtp.zeptomail.com[165.173.182.10]:587, delay=1.5, delays=0.01/0.02/0.83/0.61, dsn=2.0.0, status=sent (250 Message received)
Aug 12 08:09:24 cloud postfix/qmgr[18508]: 0C0E63EE01: removed