Set up Postfix to Relay Mail to an External SMTP Server on CentOS 6

Part 5 of setting up a Linux home lab environment with VirtualBox. Check this blog post for more info.

Postfix is a free and open-source mail transfer agent (MTA) that routes and delivers electronic mail.

Software

Software used in this article:

  1. CentOS 6.7
  2. Postfix 2.6.6

Install Postfix

Install Postfix:

# yum install -y postfix cyrus-sasl-plain

In case Sendmail is installed, erase it:

# yum erase -y sendmail*

Open /etc/postfix/main.cf for editing and configure the following:

smtpd_banner = $myhostname ESMTP
inet_interfaces = all
inet_protocols = ipv4

relayhost = [mail.example.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CApath = /etc/ssl/certs
smtp_use_tls = yes

mynetworks = 127.0.0.0/8, 10.8.8.0/24
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
#mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
debug_peer_level = 2
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no

Create /etc/postfix/sasl_passwd file and add the authentication details of the external SMTP server:

[mail.example.com]:587 [email protected]:passwd

Prevent non-root access:

# chmod 0600 /etc/postfix/sasl_passwd

Create a database file:

# postmap sasl_passwd

Restart Postfix service:

# /etc/init.d/postfix restart

Iptables will be configured via Puppet. The line below is used for the time being only:

# iptables -A INPUT -s 10.0.0.0/8 -p tcp -m state --state NEW --dport 25 -j ACCEPT

Troubleshooting

Check logs:

# tail /var/log/messages
# tail /var/log/maillog

10 thoughts on “Set up Postfix to Relay Mail to an External SMTP Server on CentOS 6

  1. I would like to get some idea about what is the relation between postfix and dovecot, which function they serve regarding mail server setup? any thought?

    • Postfix is a mail transport agent (MTA), it sends email to your desired destination. Dovecot is a delivery agent (MDA), meaning it’s not responsible for receiving email from other servers (that’s Postfix’s job). Dovecot only handles email messages coming out of the local message store.

  2. So let me see if i understand this correctly ….Postfix (an mta) is responsible for receiving and delivering email messages between server to servers.
    Once they arrives at a server , Dovecot (an mda) distributing them in local mailboxes according to the intended recipient.

    And from there, its a mail clients’ job (like roundcube) to help user get his mail from his mailbox.
    Correct me if i am wrong.

    • Yes, in general. Dovecot stores emails on a server and lets you fetch messages using POP3 and IMAP protocols. Roundcube is a webmail interface so that you can read emails using a web browser. Roundcube uses IMAP which is provided by Dovecot.

Leave a Reply

Your email address will not be published. Required fields are marked *