How to Configure a Postfix Server to Send Email through Gmail | Linode

postfix is ​​a mail transfer agent (mta) that can act as an smtp server or client to send or receive email. there are many reasons why you would want to configure postfix to send email using google workspace (previously called g suite and google apps) and gmail. one reason is to prevent your mail from being marked as spam if your current server ip has been added to a block list.

In this guide you will learn how to install and configure a postfix server on debian or ubuntu to send emails through gmail and google workspace. For information on configuring postfix with other external smtp servers, see our guide to configuring postfix to send mail using an external smtp server.

before we start

  1. complete our getting started and securing your server guides and make sure the linode hostname is configured.

    update your system:

    Use your web browser to confirm your email login credentials by logging into gmail.

    install suffix

    In this section, you will install postfix and libsasl2, a package that helps manage the Simple Authentication and Security Layer (sasl).

    1. install postfix and libsasl2-modules package:

      When prompted, select internet site as the type of mail server that the postfix installer should configure. On the next screen, the system mail name should be set to the domain through which you want to send and receive email.

      once the installation is complete, confirm that the myhostname parameter is set to the fully qualified domain name of your server:

      generate an application password for postfix

      When two-factor authentication (2fa) is enabled, gmail is preconfigured to reject connections from apps like postfix that don’t provide second step authentication. While this is an important security measure that is designed to restrict unauthorized users from accessing your account, it makes it difficult to send mail through some SMTP clients like you are doing here. follow these steps to configure gmail to create postfix specific password:

      1. Sign in to your google account and go to your account access and security settings management page.

        Scroll down to the sign in to google section and enable two-step verification. you may be prompted for your password and verification code before continuing.

        in that same section, click app passwords to generate a unique password that can be used with postfix.

        Generate an App password

      2. Click the Select app dropdown and choose Other (custom name). Enter “Postfix” and click Generate.

        the newly generated password will appear. write it down or save it in a safe place that you can easily find in the next few steps, then click done:

        Generated app password

      add gmail username and password to postfix

      Usernames and passwords are stored in sasl_passwd in the /etc/postfix/sasl/ directory. In this section, you will add your email login credentials to this file and to the postfix.

      1. open or create the /etc/postfix/sasl/sasl_passwd file and add the smtp host, username and password information:

        create the db hash file for postfix by running the postmap command:

        If all went well, you should have a new file called sasl_passwd.db in the /etc/postfix/sasl/ directory.

        secure your postfix hash database and email password files

        The /etc/postfix/sasl/sasl_passwd and /etc/postfix/sasl/sasl_passwd.db files created in the previous steps contain your smtp credentials in plain text.

        To restrict access to these files, change their permissions so that only the root user can read or write to the file. run the following commands to change ownership to root and update the permissions for the two files:

        configure postfix relay server

        In this section, you will configure the /etc/postfix/main.cf file to use the gmail smtp server.

        1. find and modify relayhost in /etc/postfix/main.cf to match the example below. make sure the port number matches what you specified in /etc/postfix/sasl/sasl\_passwd above.

          at the end of the file, add the following parameters to enable authentication:

          save your changes and close the file.

          reset suffix:

          troubleshooting: enable “less secure apps” access

          In some cases, gmail may block connections from what it calls “less secure applications”. to enable access:

          1. enable “less secure apps” access

            select enable. a yellow “updated” notice will appear at the top of the browser window and gmail will automatically send a confirmation email.

            Enable “Less Secure Apps”

          2. Test Postfix as shown in the following section. If your test emails don’t appear after a few minutes, disable captcha from new application login attempts and click Continue.

            test sending postfix emails with gmail

            use postfix’s sendmail implementation to send a test email. enter lines similar to those shown below, and note that there are no inline prompts until . end the process:

            verify the destination email account for the test email. open syslog using the tail -f command to show changes as they appear live:

            ctrl + c to exit the log.

            See Also:  Does Mail Run July 5th 2021?

Leave a Reply

Your email address will not be published. Required fields are marked *