How to send mail from linux server to gmail
To send mail from a Linux server to Gmail, you'll need to configure your Linux server to use Gmail's SMTP server. Here's a step-by-step guide to help you do so:
Prerequisites:
- You have a Gmail account with 2-Step Verification (2SV) enabled.
- You have a Linux server with a mail client installed (e.g., sendmail, postfix, or exim).
- You have a basic understanding of Linux and mail server configuration.
Step 1: Enable Less Secure Apps in Gmail
To allow your Linux server to send mail through Gmail, you need to enable "Less Secure Apps" in your Gmail account settings. To do this:
- Log in to your Gmail account.
- Click on the gear icon in the upper right corner and select "See all settings."
- Go to the "Accounts and Import" tab.
- Click on "Other (Less secure apps)" and toggle the switch to "On."
- Click "Save Changes."
Step 2: Configure your Linux server's mail client
The specific steps to configure your Linux server's mail client will vary depending on the mail client you're using. Here are some general steps for popular mail clients:
Sendmail:
- Edit the
/etc/sendmail.cf
file using a text editor (e.g.,sudo nano /etc/sendmail.cf
). - Add the following lines at the end of the file:
define(`SMART_HOST', `smtp.gmail.com') define(`RELAYHOST', `smtp.gmail.com')
- Save the file and restart the sendmail service (e.g.,
sudo service sendmail restart
).
Postfix:
- Edit the
/etc/postfix/main.cf
file using a text editor (e.g.,sudo nano /etc/postfix/main.cf
). - Add the following lines at the end of the file:
smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_use_tls = yes smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
- Create a new file
/etc/postfix/sasl_passwd
with the following contents:[smtp.gmail.com]:587 your_email_address:your_email_password
- Save the file and restart the postfix service (e.g.,
sudo service postfix restart
).
Exim:
- Edit the
/etc/exim.conf
file using a text editor (e.g.,sudo nano /etc/exim.conf
). - Add the following lines at the end of the file:
driver = smtp hosts = smtp.gmail.com port = 587 username = your_email_address password = your_email_password
- Save the file and restart the exim service (e.g.,
sudo service exim restart
).
Step 3: Test your mail client
Once you've configured your Linux server's mail client, test it by sending a mail to a recipient using the mail
command (e.g., mail -s "Test mail" [email protected]
).
Troubleshooting tips:
- Make sure you've enabled 2-Step Verification (2SV) in your Gmail account and have generated an App Password.
- Verify that your Linux server's mail client is configured correctly by checking the mail logs (e.g.,
/var/log/mail.log
). - If you're still having issues, try disabling the "Less Secure Apps" setting in your Gmail account and reconfiguring your Linux server's mail client.
By following these steps, you should be able to send mail from your Linux server to Gmail.