How to Send Gmail Email With Postfix on Ubuntu
In this guide, we will walk you through the steps to configure Postfix on an Ubuntu system to send emails through your Gmail account. This can be useful for various purposes, such as sending automated emails from your server. Follow these steps:
Step 1: Install Required Packages
First, you need to install the necessary packages. Open a terminal and run the following command:
|
|
During the installation process, you will be prompted to configure Postfix. Select “Internet Site” and press Enter. Enter your domain name (or set your localhost name if you don’t have a domain).
Step 2: Create the SASL Password File
Now, you need to create a file to store your Gmail account credentials securely. Open the SASL password file for editing with your preferred text editor. In this example, we’ll use Vim:
|
|
Add the following line to the file, replacing [email protected]
with your Gmail email address and password
with your Gmail password:
|
|
Save and close the file.
Next, restrict the permissions of this file to ensure it’s not accessible to unauthorized users:
|
|
Then, create a hash database from this file:
|
|
Step 3: Configure Postfix
Open the Postfix configuration file for editing:
|
|
Find the line that starts with relayhost =
and modify it as follows:
|
|
Add the following lines to enable SASL authentication, disallow anonymous authentication, specify the location of the SASL password file, enable STARTTLS encryption, and specify the location of CA certificates:
|
|
Save and close the file.
Step 4: Restart Postfix
To apply the changes you made to the Postfix configuration, restart the Postfix service:
|
|
Sending Gmail Emails with Postfix
You have now configured Postfix to send emails through your Gmail account. You can use the mail
command or any other email-sending application on your Ubuntu server to send emails via your Gmail account through the Postfix configuration you set up.
Remember to use this configuration responsibly, and do not share your Gmail credentials or leave them exposed on your server.