I installed the Postfix mail transfer agent using the command sudo apt install mailutils.

I am using the following command in my terminal.

echo "This is the body of the email" | mail -s "This is the subject line" 

It runs fine but I want to use my gmail account. How can I do this?

Note that my Ubuntu version is 18.10.

1 Answer

1.configure Postfix

Edit the Postfix configuration file.

sudo nano /etc/postfix/main.cf 

Find the following line relayhost = about 6 lines up from the bottom of the file and delete it.

Add the following to the end of the file.

relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/postfix/cacert.pem smtp_use_tls = yes 

Save file and exit. (Press CTRL + X, press Y and then press ENTER)

2. Create Password and DB Files

Create the sasl_passwd file which will store our credentials.

sudo nano /etc/postfix/sasl_passwd 

Insert the following:

[smtp.gmail.com]:587 :password 

Save file and exit. (Press CTRL + X, press Y and then press ENTER)

Create a hash database file for Postfix with the postmap command.

sudo postmap /etc/postfix/sasl_passwd 

There should now be a file called sasl_passwd.db in the /etc/postfix/ directory.

For added security, we will only allow root user to read and write to sasl_passwd and sasl_passwd.db

sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db 

3.Send a Test Mail

We’ll now send a test email message. Make sure to replace with your own email address.

echo "Test Email message body" | mail -s "Email test subject" 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy