Skip to main content

Command Palette

Search for a command to run...

Setting the Postfix mail in Ubuntu

Updated
2 min read
B

I am DevOps Engineer who works on DevOps tools like Docker, Kubernetes, Terraform, Git, GitHub, Jenkins and AWS services.

  1. Install postfix

    apt install postfix

  2. install mailx

    apt install mailutils

  3. In the /etc/postfix/main.cf file, add -

     relayhost = [smtp.gmail.com]:587
    
     # Location of sasl_passwd we saved
     smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
    
     # Enable SASL authentication for postfix
     smtp_sasl_auth_enable = yes
     smtp_tls_security_level = encrypt
    
     # Disallow methods that allow anonymous authentication
     smtp_sasl_security_options = noanonymous
    

    1. Create file under /etc/postfix/sasl/

      Filename: sasl_passwd

      Add the below line -

      [smtp.gmail.com]:587 <your email>:<your password>

      Ex- [smtp.gmail.com]:587 bhaskarmehta422@gmail.com:aasajrjiszawigyw

      Steps to set the Password -

      1. Go to this location in your browser and click on "Manage your Google Account"

        1. Click On Security -

And make sure that 2 Steps Verification is ON -

3. Search App passwords in the search bar and click on this -

4. Click on Select App and Click on (Other)Custom Name. Give a name example - smtp and click on Generate.

-> Once you click on Generate, it will generate a password for you and you can use this here.

  1. Convert the sasl_passwd file into db file from /etc/postfix/sasl location

    postmap sasl_passwd

  2. change the owenership of sasl_passwd

    chmod 600 *

  3. Start the Postfix service

    systemctl start/enable postfix

    systemctl stop/disable postfix

    systemctl restart postfix

    systemctl status postfix

  4. Send a test email

     echo "Test Mail" | mail -s "Postfix TEST" -A testfile.txt bhaskarmehta422@gmail.com
    
  5. Output -