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:

  1. You have a Gmail account with 2-Step Verification (2SV) enabled.
  2. You have a Linux server with a mail client installed (e.g., sendmail, postfix, or exim).
  3. 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:

  1. Log in to your Gmail account.
  2. Click on the gear icon in the upper right corner and select "See all settings."
  3. Go to the "Accounts and Import" tab.
  4. Click on "Other (Less secure apps)" and toggle the switch to "On."
  5. 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:

  1. Edit the /etc/sendmail.cf file using a text editor (e.g., sudo nano /etc/sendmail.cf).
  2. Add the following lines at the end of the file:
    define(`SMART_HOST', `smtp.gmail.com')
    define(`RELAYHOST', `smtp.gmail.com')
  3. Save the file and restart the sendmail service (e.g., sudo service sendmail restart).

Postfix:

  1. Edit the /etc/postfix/main.cf file using a text editor (e.g., sudo nano /etc/postfix/main.cf).
  2. 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
  3. Create a new file /etc/postfix/sasl_passwd with the following contents:
    [smtp.gmail.com]:587 your_email_address:your_email_password
  4. Save the file and restart the postfix service (e.g., sudo service postfix restart).

Exim:

  1. Edit the /etc/exim.conf file using a text editor (e.g., sudo nano /etc/exim.conf).
  2. 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
  3. 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:

By following these steps, you should be able to send mail from your Linux server to Gmail.