Setting-up-postfix-for-sending-mail-using-an-external-SMTP-server

From BubbaWiki
Jump to navigation Jump to search

Setting up postfix for sending mail using an external SMTP server

Author: Eek

Note: This how-to was written for the bubba (B1). It will still work, but the packages no longer need to be installed.

For the B3 you can also use Send mail using GMAIL


This how-to describes the setup of postfix for sending mail using an external SMTP server. My ISP has a SMTP server available, for which i need (of course) to authenticate, using username and password.
Below is the configuration I will be using during the how-to, please replace it with your own.

smtp server : smtp.myisp.com
username : eek
password : thecat
mailuser at myisp.com : eek@myisp.com
mailuser on bubba : eek@bubba.excito.com

Packages

Install the packages, using the Debian sources, see Howto install Debian packages (http://forum.excito.net/viewtopic.php?t=421)

apt-get install postfix-tls
apt-get install libsasl2-modules

Setup

Edit the /etc/postfix/main.cf

vi /etc/postfix/main.cf

Look for the parameter relayhost and fill in your preferred smtp server

relayhost = [smtp.myisp.com]

Add the following parameters at the end

smtpd_sasl_auth_enable = no
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtpd_sasl_local_domain = $myhostname
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtpd_sasl_application_name = smtpd

Create the sasl password file

vi /etc/postfix/sasl_passwd

Add the following contents

# Contents of sasl_passwd
#
[smtp.myisp.com] eek:thecat

Convert the file to a hash format

postmap /etc/postfix/sasl_passwd 

Check the above command by running a simple "hash" key test

$ postmap -q [smtp.myisp.com] /etc/postfix/sasl_passwd

Protect your password so only root can read the file.

chmod 640 /etc/postfix/sasl_passwd


Mapping

Optional: Mapping local email address to valid Internet addresses. I created an account eek on the bubba and get default the email address eek@bubba.excito.com.
But my ISP knows me by eek@myisp.com, so i need to map the email addresses.

My postfix version is

$ postconf mail_version
mail_version = 2.1.5

For version 2.2 and later, you can use a generic file, see reference 4.
For version 2.1 and earlier, do the following:
Edit the /etc/postfix/main.cf

vi /etc/postfix/main.cf

Add the following parameters at the end

canonical_maps = hash:/etc/postfix/canonical
virtual_alias_maps = hash:/etc/postfix/virtual

Create the canonical file, which maps outgoing mail to a given email address

vi /etc/postfix/canonical

Add the following contents

eek@bubba.excito.com eek@myisp.com

Create the virtual file, which maps incoming mail to a given email address

vi /etc/postfix/virtual

Add the following contents

eek@myisp.com eek@bubba.excito.com

Convert the files to a hash format

postmap /etc/postfix/canonical
postmap /etc/postfix/virtual


Restart

Restart postfix

/etc/init.d/postfix restart 

To see if all changes went into effect, run postconf -n

$ postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
canonical_maps = hash:/etc/postfix/canonical
config_directory = /etc/postfix
disable_vrfy_command = yes
home_mailbox = Mail/
inet_interfaces = all
mailbox_size_limit = 0
mydestination = $myhostname $mydomain localhost.$mydomain localhost
mydomain = localdomain
mynetworks_style = subnet
myorigin = /etc/mailname
recipient_delimiter = +
relayhost = [smtp.myisp.nl]
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
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
smtpd_helo_required = yes
smtpd_recipient_restrictions = permit_mynetworks reject_unauth_destination reject_unauth_pipelining reject_invalid_hostname reject_non_fqdn_sender reject_unknown_sender_domain reject_non_fqdn_recipient reject_unknown_recipient_domain reject_rbl_client list.dsbl.org reject_rbl_client relays.ordb.org reject_rbl_client dnsbl.njabl.org reject_rbl_client dnsbl.sorbs.net reject_rbl_client bl.spamcop.net permit
smtpd_sasl_application_name = smtpd
smtpd_sasl_auth_enable = no
smtpd_sasl_local_domain = $myhostname
virtual_alias_maps = hash:/etc/postfix/virtual


Test

Test if the mail is getting delivered
Login as eek

su - eek

Send mail

echo "Test mail" | mail -s "This is a test mail" eek@myisp.com

Check the logfile if the mail got handed to the smtp server

$ cat /var/log/mail.log
Dec 24 17:33:18 bubba postfix/postfix-script: starting the Postfix mail system
Dec 24 17:33:18 bubba postfix/master[5100]: daemon started -- version 2.1.5
Dec 24 17:33:58 bubba postfix/pickup[5104]: A45EA11F043A: uid=1001 from=<eek>
Dec 24 17:33:58 bubba postfix/cleanup[5111]: A45EA11F043A: message-id=<20071224173358.A45EA11F043A@bubba.localdomain>
Dec 24 17:33:58 bubba postfix/qmgr[5105]: A45EA11F043A: from=<eek@myisp.com>, size=338, nrcpt=1 (queue active)
Dec 24 17:33:59 bubba postfix/smtp[5113]: A45EA11F043A: to=<eek@myisp.com>, relay=smtp.myisp.nl[222.111.6.66], delay=1, status=sent (250 2.0.0 lBOGYki1012247 Message accepted for delivery)
Dec 24 17:33:59 bubba postfix/qmgr[5105]: A45EA11F043A: removed

Check if you've got mail!

References

1. http://souptonuts.sourceforge.net/postfix_tutorial.html
2. http://www.postfix.org
3. http://forum.excito.net/viewtopic.php?p=3060
4. http://www.postfix.org/STANDARD_CONFIGURATION_README.html#fantasy