How to send mail from a Linux server using Gmail

Introduction

This Linux mail tutorial will show you how to send mail from your Linux server using Gmail. DMA (Dragonfly Mail Agent) will be used to setup outgoing mail from a Linux server using a Gmail account to provide great deliverability. By using DMA you won’t have to deal with having bigger software like Exim, Postfix, etc installed. DMA is a small mail transport agent that’s perfect for servers (or even desktop computers) that only need to send outgoing e-mail like log files, notifications, security notifications from fail2ban, etc and it doesn’t need to listen on network ports. I use DMA on almost all of my servers and I absolutely love it.

Note: This tutorial uses Debian Linux but it should also work with Ubuntu Linux too!

Install DMA and bsd-mailx

First you need to install DMA and bsd-mailx

sudo apt install dma bsd-mailx

and with DMA and bsd-mailx installed, you can now move on to setting up the DMA configuration files.

Configure DMA to use Gmail

Let’s start with setting up /etc/dma/auth.conf first, which will contain your Gmail account information, so open it up with nano or your favorite editor:

sudo nano /etc/dma/auth.conf

and add the following (making sure to change the highlighted text to your Gmail e-mail address and your Gmail password):

yourgmailaddress@gmail.com|smtp.gmail.com:yourgmailpassword

Now save /etc/dma/auth.conf and exit

With the authentication settings setup, you can move on to setting up the actual dma configuration file by opening up /etc/dma/dma.conf

sudo nano /etc/dma/dma.conf

and change the following settings:

SMARTHOST: smtp.gmail.com
PORT: 587
AUTHPATH: /etc/dma/auth.conf
SECURETRANSFER
STARTTLS

Save /etc/dma/dma.conf and exit

Setup mail aliases in /etc/aliases

You’ll probably want to setup some aliases for your mail like root or your user so you can do that by creating/opening /etc/aliases like so:

sudo nano /etc/aliases

and add the aliases you want mail to be sent to. For instance, in the example below I setup a root alias that sends mail to my e-mail address and also a wildcard to catch any other mail.

root: youremailaddress@example.com
*: root

Save the /etc/aliases file and exit, then refresh the aliases with:

sudo newaliases

You’re now ready to move on to the last step of this tutorial!

Turn on Allow less secure app access in your Google account

When you try to send mail via your Gmail account, Google will likely block the mail until you turn on the “Allow less secure app access” in your Google account settings.

To allow access, you’ll need to open up your Google account settings by visiting https://myaccount.google.com/ and clicking on the “Security” tab

Google Account security tab
Click on the Security tab in your Google Account settings

Once you’re in the Security settings of your Google account, find the “Allow less secure app access” tab and click on it to turn it on:

To send outgoing mail with Gmail on your Linux server you need to enable Less secure app access
Click on Turn on access to enable the “Allow less secure app access”

Once you’ve enabled the “Allow less secure app access” in your Google Account settings, you’re now ready to finally test out sending mail with your new DMA outgoing mail setup!

Note: If you’re using two-factor authentication on your Google account then you will also need to create an app specific password at https://myaccount.google.com/apppasswords.

Send a test e-mail

You can now send a test e-mail by using the follow command:

echo "Testing out DMA on my linux server" | mail -s "Testing DMA" root

Note: The above command sends the test e-mail to root so the e-mail will be sent to the address you set for root in /etc/aliases. If you would like to send the test email to another address, just switch out “root” to an e-mail address of your choosing at the end of the above command.

Completion

You should hopefully have successfully setup outgoing e-mail on your Linux server using Gmail and DMA by following this tutorial! Want to use something besides Gmail? You could also use this tutorial with Sendgrid, Mailgun, or even just use DMA by itself. If you have any questions or need help, please let me know!

Image Credit: The featured image for this article was created using a photo by Julie Marsh on Unsplash.

Leave a Reply

Your email address will not be published. Required fields are marked *