Gmail: How to send email from linux server?

Gmail: How to send email from linux server?  Gmail is the first email choice for personal uses. Very convenient, easy to use, but you have to do some configuration if gmail is used to send system notice from your linux server.

01 Turn on less security in Gmail

  1. Login in your Gmail account from web browser
  2. Click Google Apps and select “Account”
  3. Click “Security”
  4. Find “Less secure app access”
  5. Turn on it

Gmail: How to send email from  linux server?

 

02 Configuration

CentOS:

Login in with root via SSH

  • Install POSTFIX and mailx

yum install postfix

systemctl restart postfix

yum install cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain

yum install mailx

 

  • Edit main.cf

nano /etc/postfix/main.cf

  • Add blow lines to the end of main.cf

#postfix send email via gmail on Aug 09 2020

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_use_tls = yes

 

  • Create the file /etc/postfix/sasl_passwd with the following contents:

nano /etc/postfix/sasl_passwd

  • Add your Gmail information
[smtp.gmail.com]:587 user.name@gmail.com:password
  • Restrict permissions on the postfix file:

chmod 400 /etc/postfix/sasl_passwd

chown root.root /etc/postfix/sasl_passwd

  • Hash the file into a .db that Postfix will read:
postmap /etc/postfix/sasl_passwd
  • Change the owner of the sasl_passwd to the postfix user:

chown postfix /etc/postfix/sasl_passwd

  • Reload postfix:

service postfix restart

  • Test email

echo “Test mail from postfix” | mail -s “Test Postfix” youremail@gmail.com

 

Debian or Ubuntu Server:

The process is almost same as CentOS, the only difference is to install mailutils instead of mailx.

sudo apt install mailutils