Compose and dispatch an email directly from the command line.

Postfix configuration to enable sending of emails to a specified Gmail account.

·

2 min read

Compose and dispatch an email directly from the command line.

First, we'll need to make sure our system is up-to-date by running this command.

Next, we'll install Postfix, a mail transfer agent (MTA) specifically designed to route and deliver emails on Unix-like operating systems.

Configure Postfix with the "Internet Site" option, then specify a system mail name of your choice.

We will then generate a Google app password to configure it for our Postfix setup. To generate app password click the link below. 2FA well be needed to enable this operation.

generate app password

Create a SASL password in the SASL folder of our postfix config files. SASL, or Simple Authentication and Security Layer, is a framework used for authentication and data security in network protocols. The sasl_passwd will be created in this directory /etc/postfix/sasl

The configuration sasl_passwd should look like this, where by smtp.gmail.com is the address of the Gmail SMTP server and 587 is the SMTP port. Then your gmail address and the app password respectively

Create a hash database using postmap for the password. The command essentially converts the source file into a format that Postfix can efficiently use for mail routing and configuration.

You will possess a database file, and for added security, it is necessary to enhance the protection of plain text passwords by modifying ownership and permissions to be assigned to the root user. The below commands will enable us do so.

Now we will go back and configure postfix in the main.cf file to use the gmail server in order to relay the emails.

Add relay host to [smtp.gmail.com]:587

The following config files #SASL authentication should also be added at the bottom of the main.cf file to enable sasl authentication to run.

Then restart postfix to update the newly added configurations.

Test it using the below command.

To send the email successfully to the specified Gmail account, press Ctrl+D after composing the message to exit and initiate the sending process.