Skip to content

Instantly share code, notes, and snippets.

@loziju
Last active August 29, 2024 03:01
Show Gist options
  • Save loziju/66d3f024e102704ff5222e54a4bfd50e to your computer and use it in GitHub Desktop.
Save loziju/66d3f024e102704ff5222e54a4bfd50e to your computer and use it in GitHub Desktop.
Configure postfix as relay for OS X

Configure postfix as relay for macOS Sierra – Sonoma

1. Edit postfix configuration file

  1. sudo vi /etc/postfix/main.cf
  2. Ensure that the following values are set:
    mail_owner = _postfix
    setgid_group = _postdrop
    
  3. Add the following lines at the end of the file:
    # Postfix as relay
    #
    #Gmail SMTP
    relayhost=smtp.gmail.com:587
    #Hotmail SMTP
    #relayhost=smtp.live.com:587
    #Yahoo SMTP
    #relayhost=smtp.mail.yahoo.com:465
    # 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
    

2. Create sasl_passwd file

  1. sudo sh -c 'echo "\nsmtp.gmail.com:587 [email protected]:your_password" >> /etc/postfix/sasl_passwd' Replace [email protected] and your_password with actual values.
  2. sudo postmap /etc/postfix/sasl_passwd

3. Autorun postfix on boot and restart postfix

  1. Copy the postfix master plist out of System folder.

    sudo cp /System/Library/LaunchDaemons/com.apple.postfix.master.plist /Library/LaunchDaemons/org.postfix.custom.plist
    
  2. sudo vi /Library/LaunchDaemons/org.postfix.custom.plist

  3. Change the label value from com.apple.postfix.master to org.postfix.custom

    Remove these lines to prevent exiting after 60s

      <string>-e</string>
      <string>60</string>
    

    Add these lines before </dict>

      <key>KeepAlive</key>
      <true/>
      <key>RunAtLoad</key>
      <true/>
    
  4. Relaunch the daemon.

    sudo launchctl unload /Library/LaunchDaemons/org.postfix.custom.plist
    sudo launchctl load /Library/LaunchDaemons/org.postfix.custom.plist
    
  5. Check that daemon has started.

    sudo launchctl list | grep org.postfix
    

4. Turn on less secure apps for gmail

In Gmail we must switch on the option "Access for less secure apps", otherwise we will get the error: SASL authentication failed

5. Test

  1. echo "Test sending email from Postfix" | mail -s "Test Postfix" [email protected]

    Change [email protected] with valid email with mailbox access for easy checking.

  2. Check mail queue and possible delivery errors with mailq.

  3. Check mail log with tail -f /var/log/mail.log.

@FaycalBESS
Copy link

Configure postfix as relay for macOS Monterey

I recently had to repeat this process on a brand-new M1 Macbook running MacOS 12.5 (Monterey). Here is a rewrite of the full instructions, merging my tips from August and September 2020, but

  • I use iCloud so my examples focus on iCloud as the mail service.
  • I cannot test or verify Gmail instructions, so I left them unchanged.
  • I am using mutt instead of mail for sending mail, because I have not attempted mail again.

1. Edit postfix configuration file

  1. sudo vi /etc/postfix/main.cf
    Add the following lines at the end of the file:
# Postfix as relay
#
# iCloud SMTP
relayhost=smtp.mail.me.com:587
#Gmail SMTP
#relayhost=smtp.gmail.com:587
#Hotmail SMTP
#relayhost=smtp.live.com:587
#Yahoo SMTP
#relayhost=smtp.mail.yahoo.com:465
# 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

2. Configure mutt

I could not find a way to convince mail (or mailx or Mail, which on MacOS are all the same tool) to set the From address, so I used mutt and created ~/.muttrc as follows (with appropriate modifications to use your own name and email address):

set use_from
set from="My Name <[email protected]>"
set use_envelope_from
set envelope_from_address="My Name <[email protected]>"
set reply_to

3a. for iCloud users:

For reference, see helpful information from this Apple support page. I also needed to generate an app-specific password here.

I strongly recommend this practice because it avoids placing your normal iCloud password, in cleartext, in the /etc/postfix/sasl_passwd file.
In the event that password is compromised, or laptop lost, it is possible to revoke the app-specific password from that same page on apple.com.

3b. for Gmail users: Turn on less secure apps (unverified)

In Gmail we must switch on the option "Access for less secure apps", otherwise we will get the error: SASL authentication failed (I have not verified this information is up-to-date in 2022)

4. Create sasl_passwd file

  1. sudo vi /etc/postfix/sasl_passwd
    Create this file with one of these lines, corresponding to your email provider:
    smtp.mail.me.com:587 [email protected]:your_password
    smtp.gmail.com:587 [email protected]:your_password
    smtp.live.com:587 [email protected]:your_password
    smtp.mail.yahoo.com:465 [email protected]:your_password
    Replace your_email and your_password with actual values.

    iCloud users: use the app-specific password generated in part 3a above.

  2. sudo postmap /etc/postfix/sasl_passwd
    to inform postfix of the creation or modification of the passwd file.
  3. sudo chmod 640 /etc/postfix/sasl_passwd*
    to prevent others from reading your password files.

5. Autorun postfix on boot and restart postfix

I found this step (creating org.postfix.custom) to be unnecessary. I was able to send mail (with mutt) via postfix, either from the command-line or from scripts. Indeed, I launch some scripts with personal launchctl plists, and they can send mail too.

However, if you do find it helpful or necessary, note that launchctl load should now be launchctl bootstrap, and launchctl unload should now be launchctl bootout.

6. Test

  1. echo "Test sending email from Postfix" | mutt -s "Test Postfix" [email protected]
    Change [email protected] with valid email with mailbox access for easy checking.
  2. Check mail queue and possible delivery errors with mailq.
    a. If you fill your mail queue with failed messages, sudo postsuper -d ALL deferred

Thanks a lot David, I will go thru these steps.
just please can you tell me how to configure Mutt (step 2)
are these commands to put in terminal or any lines to add to a certain file, sorry I'm really what we can call "a newbie"
thanks for your answer dear

@dfkotz
Copy link

dfkotz commented Aug 14, 2022

just please can you tell me how to configure Mutt (step 2)
are these commands to put in terminal or any lines to add to a certain file,....

Those lines are the content of the file ~/.muttrc; use any plain-text editor to create the file. I've updated my post to clarify.

@FaycalBESS
Copy link

just please can you tell me how to configure Mutt (step 2)
are these commands to put in terminal or any lines to add to a certain file,....

Those lines are the content of the file ~/.muttrc; use any plain-text editor to create the file. I've updated my post to clarify.

Thank you very much dear.
I will do it immediately and post here the result.
thank you again

@FaycalBESS
Copy link

IT WOOOORKKKS super
thanks a lot dear David

@FaycalBESS
Copy link

mutt is working via shell, but not with my php.mail() function
is there any additional settings to do or maybe in mamp pro conf or in php.ini file?
thank you for your help david

@FaycalBESS
Copy link

hello @dfkotz
I have explored many solutions and now, i think i could know more about the problem,
i have this error message
(Cannot start TLS: handshake failure)
any advice pease??

@gingerbeardman
Copy link

Thanks @dfkotz my gmail is working it in Monterey!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment