Hassle-Free Laravel Mail Sending via SendMail

Every developer is familiar with the significant challenge of setting up emails for their web applications. It often takes a considerable amount of time to configure, or it's guaranteed to cost you some cash to outsource it to a third-party, especially when you just want to set it up as an MVP to gauge market interest...

Fortunately, Ubuntu Linux web servers are easily configured with SendMail to send emails for free through your own server.

With this guide, we assume you have basic knowledge of the following:

  • How to connect to your server via SSH

  • How to log in as a root user on your server

  • How to navigate within a Ubuntu Linux environment

  • How to use Vim or another editor in Linux

Furthermore, we recommend always backing up before following a guide like this, so that in case of unexpected errors, you can quickly revert to a state where your server was still functioning.

Install Sendmail

In many cases, Sendmail is already installed by default on Ubuntu environments. However, if that is not the case, you can execute the following command:

sudo apt install sendmail

Configure Sendmail

Once SendMail is installed, we need to configure it to connect to our SMTP relay server, which we want to apply our email traffic to. In this example, we are using Zorgeloos Mailen from TransIP.

TransIP offers a convenient option to use their SMTP relay server for sending email traffic. For this, TransIP provides you with a set of DNS records that you can easily configure on the project's domain name.

If you prefer not to use this option and instead opt for another provider such as Google Gmail, it is also possible, but you will need to adjust the settings accordingly to your chosen provider.

Applying a few rules

We need to add a few rules to an existing SendMail file. Open the file by executing the following command:

sudo vim /etc/mail/sendmail.mc

Next, add the following lines to the file:

define(`SMART_HOST', `vps.transip.email')dnl
define(`confAUTH_MECHANISMS', `LOGIN PLAIN')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
FEATURE(`authinfo`)dnl

We intentionally choose to only allow the SMTP relay server to communicate over port 587, as this is necessary to establish the correct connection. Furthermore, you can choose to adjust the SMART_HOST to an address like smtp.google.com.

Since we often deal with an authentication protocol, we need to ensure that an authentication file is also created, where we will input our login credentials. This will allow Sendmail to connect to our SMTP relay server.

Let's start by creating a new file by executing the following command:

sudo vim /etc/mail/authinfo

We add a simple text line here, as shown below:

AuthInfo:vps.transip.email "U:USERNAME" "P:PASSWORD" "M:PLAIN"

Once we have placed this in the file, we can execute a series of commands to ensure that everything is registered correctly and that Sendmail will henceforth use the newly configured SMTP relay server!

sudo makemap hash authinfo < authinfo
sudo /etc/mail/Makefile
service sendmail restart