Send Email With Postfix via Gmail

Maciej
2 min readFeb 21, 2022

--

Photo by Liam Truong on Unsplash

Introduction

CentOS uses Postfix as the default MTA. Even if you install it with the minimal configuration, Postfix is ​​installed and started, which is a waste, so relay it to another place with GMail. For example, all emails sent from the host ozyrys.local are sent from the same GMail user as shown below. This makes it basically unusable in a multi-user environment. I feel that it may be good for my local development environment.

From: root <user@gmail.com>
X-Google-Original-From: root@hoge.local (root)

Let’s start

Environment and Setup

  • CentOS 7

⚠️ Note that you can not authenticate with GMail without cyrus-sasl-plain

Relay settings

We need edit /etc/postfix/main.cf And add the following settings to the end, set GMail as a relay destination and use TLS for SMTP authentication (PLAIN).

Setup authentication user settings

Edit file /etc/postfix/sasl_passwd add the following settings

[smtp.gmail.com]:587 <gmail_address>:<password>

Now we can change permissions and create db

chmod 600 /etc/postfix/sasl_passwd
postmap hash:/etc/postfix/sasl_passwd

At the end, we only have to restart the website with the command.

systemctl restart postfix

Testing

If you have the mailx package installed, send an email to your own address (other than GMail is also possible) as shown below, and you should receive the email from the GMail address registered as a relay.

$ mail test@example.com
Subject: test-mail
TEST-MAIL
EOT

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.