How to send emails from localhost from MAC

How to send emails from localhost from MAC

·

2 min read

I was looking around for a SMTP server to install on MAC. Good part is there is one already install on MAC it is called Postfix.

This tutorial is about how to configure Postfix for Mac OS X.

Step 1. Edit Postfix config file Open a terminal and edit the file main.cf

Add the following lines at the very end of the 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 This configures Postfix to use a 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 know the SMTP host and port. For example for hotmail you should replace the relayhost for the following:

#Hotmail SMTP relayhost=smtp.live.com:587

Or for Yahoo:

#Yahoo SMTP relayhost=smtp.mail.yahoo.com:465

Step 2. Create the sasl_passwd file We need to create the sasl_passwd file with the SMTP credentials

sudo vi /etc/postfix/sasl_passwd Write the following content and save:

smtp.gmail.com:587 :your_password

Create the Postfix lookup table from the sasl_passwd file. sudo postmap /etc/postfix/sasl_passwd This will create the file sasl_passwd.db

Step 3. Restart Postfix To apply all new changes we have to restart Postfix or start if it is never used before:

sudo postfix start or sudo postfix reload

Step 4. Turn on less secure apps (Only Gmail) In Gmail we must switch on the option “Access for less secure apps“ , otherwise we will get the error: SASL authentication failed

Step 5. Test it! Let’s send a mail to our own account to be sure everything is working fine: date | mail -s testing You can check the mail queue and the possible delivery errors using “mailq“

mailq Use the postfix logs to ensure everything is working as expected:

tail -f /var/log/mail.log

Other useful commands

To clear the mail queue: sudo postsuper -d ALL

This is quite a easy process and can be completed in less than 10 mins. Let me know if you have any suggestions.

Did you find this article valuable?

Support Saifuddin by becoming a sponsor. Any amount is appreciated!