Skip to content

Instantly share code, notes, and snippets.

@afzouni
Last active March 29, 2021 09:14
Show Gist options
  • Save afzouni/7f7a579603ea16b0d5f43e82219b94ac to your computer and use it in GitHub Desktop.
Save afzouni/7f7a579603ea16b0d5f43e82219b94ac to your computer and use it in GitHub Desktop.
How To Config Postfix as SMTP Client (smtp relay)

Introduction

Postfix using /usr/sbin/sendmail to send emails.

postfix can be configured as an SMTP-Client which called smtp-relay method.

Configuration

Authenticaion

Authenticaiton for smtp-relay using sasl and we have to create a username/password file with plain auth information and create a Hash database from it. Postfix is using the hashed datapase:

Create Plain Username/Password File /etc/postfix/sasl_passwd:

smtp.address.com username:password

Run this command to generate hashed database:

postmap etc/postfix/sasl_passwd

and this file will be created:

/etc/postfix/sasl_passwd.db

now, to Secure files run these commands:

chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

Configuration the postfix

Edit the main configuration of postfix /etc/postfix/main.cf and add these lines:

smtp_use_tls = no
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous

# If this line is exists, overwite it
relayhost = webus.us

Restart the postfix

service postfix restart

Test Sending E-Mail

echo  "body of your email" | mail -s "This is a subject " -a "From:sender@address.com"   myemail@example.com 

Troubleshoot

Check logs in /var/log/syslog file.

Overwrite Sender

Sometimes we need to overwrite the sender address.

  • Note: This Configs will affect on ALL outcoming Emails.

To that add below lines to /etc/postfix/main.cf:

sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps =  regexp:/etc/postfix/sender_canonical_maps
smtp_header_checks = regexp:/etc/postfix/header_check

Add /etc/postfix/sender_canonical_maps:

/.+/   new-sender@address.com

Add /etc/postfix/header_check:

/From:.*/ REPLACE From: new-sender@address.com

Refrences

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