How to send mails in POSTFIX through external SMTP

postfixThere are many reasons why you may want to do it. I have set it up on my POSTFIX because I have dynamic IP that is banned in most spam databases. I could get mails to my mailbox but could not reply to any as my IP was rejected by most of mail servers. But there is a quick solution.

Requirements

So what will you need? Not to much. All that you need to set it up is working Postfix server (we will make changes in it’s configuration file) and external e-mail account with SMTP access (probably 99% of e-mail account will be ok, for ex. Gmail). So if you want to send emails with external (relay your mail). Here is a quick howto.

Step 1: Edit your configuration file

You will need to make changes in your configuration file (in standard postfix configuration it would be /etc/postfix/main.cf). You need to add at the end of configuration file following lines:

relayhost = smtp.provider.com:25
 
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes
 
# optional: necessary if email provider uses load balancing and
# forwards emails to another smtp server
# for delivery (ie: smtp.yahoo.com --> smtp.phx.1.yahoo.com)
smtp_cname_overrides_servername = no
 
# optional: necessary if email provider
# requires passwords sent in clear text
smtp_sasl_security_options = noanonymous

Just make sure that you will edit “relayhost” (in first line of this listing), with your account SMTP address and port number.

Step 2: Generate password file

Postfix have to know your account login and password as most SMTP servers would not allow to rely emails from unauthorized connections. You will need to make a password file “/etc/postfix/sasl_passwd” with similar content:

# The server info must exactly match the value
# for "relayhost" in /etc/postfix/main.cf
smtp.provider.com:25 username:password

Now you need to generate POSTFIX lookup table (from previous file), with the following command:

$ postmap hash:/etc/postfix/sasl_passwd

You may test it by typing:

$ postmap -q smtp.provider.com:25 /etc/postfix/sasl_passwd

If you will get an output like “username:password” it means that password file has been generated correctly.

You can remove “/etc/postfix/sasl_passwd“. POSTFIX will not need it anymore.

It should be working now! Restart your POSTFIX and test it! If you have any problems please make a comment below. I will try to help you.

11 Responses to “How to send mails in POSTFIX through external SMTP”


  • Thanks, this is exactly what I needed!

    Now how do I extend this to use SSL authentication where my target server requires an encrypted connection?

  • Nice one, thanks for the tip

  • nice! It works fine for me! Thank you so much

  • Brilliant, thanks for this!

  • Hi,

    i access emails using outlook. i cant send emails, am getting error “HELO command rejected: need fully-qualified hostname: proto=ESMTP helo=postfix”.

  • Hi,

    Thanks for the post: am getting Send test e-mail message: The connection to the server was interrupted. If this problem continues, contact your server administrator or Internet service provider (ISP).

  • Gmail SMTP is now on port 587 not 25

  • Thanks, this is exactly what I needed!

  • I receive errors like FROM=root@localhost and ” Sender address rejected: not owned by user root@localhost (in reply to RCPT TO command)” on clear postfix configuration after install Nagios system and configure it.

    resolved by:
    vim /etc/postfix/main.cf (add this line on end)
    sender_canonical_maps = hash:/etc/postfix/sender_canonical

    vim /etc/postfix/sender_canonical
    root myemail@mydomain.tld

    sudo postmap /etc/postfix/sender_canonical

    sudo /etc/init.d/postfix reload

  • HI Thank you so much for this. really helpfull.
    God bless

  • I get this funny error
    postfix/master[230400]: fatal: bind 0.0.0.0 port 25: Address already in use

    I find it funny, because I’m running the same services on the other server {Proxmox based on Debian 6.07) and on the other one it worked great!

    postfix check shows no output. I got the line from /var/log/mail.log and therefore postfix won’t start.

    using lsof -i:25 I can see that
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    master 7094 root 12u IPv4 99258 0t0 TCP localhost:smtp (LISTEN)
    master 7094 root 13u IPv4 99260 0t0 TCP 127.0.0.2:smtp (LISTEN)
    master 9217 root 12u IPv4 150437 0t0 TCP *:smtp (LISTEN)
    __citserver__ 103211 citadel 26u IPv4 1317582 0t0 TCP *:smtp (LISTEN)

    Citadel server is listening on my smtp port? You think it’s possible to shut it down? Anyway, this was surely not problem on the previous machine. Even the firewall is configured ALMOST the same way. The only reason is that the one where it doesn’t work has 2 IPs.

    Could you please help?
    Thank a lot
    Kind regards,

Leave a Reply