Send Email With Postfix via Gmail

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

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store