How to Send Email from localhost on macOS

 

  • To send the email from localhost we will be using Postfix which is the standard email server installed by default in MacOS.
  • Follow the steps given below to send email from localhost.

Editing main.cf

 

  • Open terminal and edit the file main.cf 
  • Use below command to edit the file.

        sudo vi /etc/postfix/main.cf.

  • Look for the following lines in file main.cf  (They are probably separated).

            mail_owner = _postfix

            setgid_group = _postdrop

    • If above lines doesn't exist then add them in your main.cf file.
    • Now add the following lines at the very end of the main.cf file.


          #Gmail SMTP

           relayhost=smtp.gmail.com:587

          # Enable SASL authentication in the Postfix SMTP client.

           smtp_sasl_auth_enable=yes

           smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd

           smtp_sasl_security_options=noanonymous

           smtp_sasl_mechanism_filter=plain

          # Enable Transport Layer Security (TLS), i.e. SSL.

          smtp_use_tls=yes

          smtp_tls_security_level=encrypt

          tls_random_source=dev:/dev/urandom


      • After adding above lines save main.cf file.

      Add your SMTP Email and Password

      • Above lines tells Postfix to use GMAIL SMTP server with Simple Authentication and Security Layer (SASL). Which will be stored in the path “/etc/postfix/sasl_passwd“. You can use any other SMTP provider (Hotmail, Yahoo, etc…). You only need to update the correct SMTP host and port.

      • Now we will create sasl_passwd file which will store your sender email and password.
      • To create sasl_passwd file, you can use following command to open it in the terminal.

             

                 sudo vi /etc/postfix/sasl_passwd

       

      • Write the following content (sender email and password)  and save sasl_passwd file.

             

                 smtp.gmail.com:587 your_email@gmail.com:your_password

       
      • Create the Postfix lookup table from the sasl_passwd file. To create lookup table use the following command.

               

                 sudo postmap /etc/postfix/sasl_passwd

       

      • This will create the file sasl_passwd.db in directory  /etc/postfix.

      Restart postfix service

      • To apply all new changes we have to start the postfix service. If postfix is already running then restart it. 

               

               start command - sudo postfix start 

               restart command - sudo postfix reload

       

      • Now, we can send the Email.

      Test the Email

      • Create a test.php file in /Applications/AMPPS/www directory and add the below code, change the sender and receiver email and test if email are sent correctly.

      <?php

                $headers = 'From:sender name<sender@gmail.com>' . "\r\n" .

                 'Reply-To: sender@gmail.com' . "\r\n" .

                 'X-Mailer: PHP/' . phpversion();

      var_dump(mail('receiver_email@gmail.com', 'Test Subject Mac', 'Test Message Mac', $headers));

      • Now you can check if you have received the email on receiver email address.

           

        If you are facing any issues, please open a ticket at Support.

        Was this page helpful?