Projekt

Allgemein

Profil

Config postfix smarthost » Historie » Revision 2

Revision 1 (Jeremias Keihsler, 04.02.2020 10:21) → Revision 2/3 (Jeremias Keihsler, 21.10.2021 08:42)

h1. Setting up Postfix to send mails via external mail-account 

 h2. Preliminary note 

 See also: 
 * https://www.zulius.com/how-to/set-up-postfix-with-a-remote-smtp-relay-host/ 
 * http://rs20.mine.nu/w/2011/07/gmail-as-relay-host-in-postfix/ 

 A typical email scenario: you're a developer, and you've got a development Linux box at home. You need to be able to send emails from your code or cron jobs, but you're too lazy to set up a full fledged email server on your LAN. Or you just want to use an email account provided by Google Apps, Yahoo, your ISP or in our case a iRedMail-installation. 

 h2. Install postfix 

 Should be preinstalled, as it is the default mail-handler with CentOS 
 <pre><code class="bash"> 
 dnf yum install postfix cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain 
 </code></pre> 

 h2. configure postfix 

 <pre><code class="bash"> 
 vim /etc/postfix/main.cf 
 </code></pre> 

 <pre> 
 # line ~118... 
 myorigin = example.com 
 # Set this to your email provider's smtp server. 
 # A lot of ISP's (ie. Cox) block the default port 25 
 # for home users to prevent spamming.    So we'll use port 80 
 # line ~339... 
 relayhost = mail.example.com:587 

 # add at end of file... 
 smtpd_sasl_auth_enable = yes 
 smtpd_sasl_path = smtpd 
 smtp_use_tls = yes 
 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 

 canonical_maps = hash:/etc/postfix/canonical 
 </pre> 

 additionally we need to know the server and email-address as well as the password to use 
 <pre><code class="bash"> 
 vim /etc/postfix/sasl_passwd 
 </code></pre> 

 <pre> 
 mail.example.com:587       mailadr@example.com:password 
 </pre>  

 The above server hostname and port must exactly match the value for "relayhost" in /etc/postfix/main.cf. 

 Generate a postfix lookup table from the previous file 
 <pre><code class="bash"> 
 postmap hash:/etc/postfix/sasl_passwd 
 </code></pre> 

 Test the lookup table, if all is good then the following will return the specified username:password 
 <pre><code class="bash"> 
 postmap -q mail.example.com:587 /etc/postfix/sasl_passwd 
 </code></pre> 

 next is to bind the local username to the email-address. You may also have a look into @/etc/aliases@ and check who's mail are sent to whom. 
 <pre><code class="bash"> 
 vim /etc/postfix/canonical 
 </code></pre> 

 <pre> 
 root       mailadr@example.com 
 </pre>  

 The above email-address must exactly match the email-address in sasl_passwd. 

 Generate a postfix lookup table from the previous file 
 <pre><code class="bash"> 
 postmap hash:/etc/postfix/canonical 
 </code></pre> 

 Make sure that sasl_passwd and sasl_passwd.db files are readable/writeable only by root 
 <pre><code class="bash"> 
 chmod 600 /etc/postfix/sasl_passwd 
 chmod 600 /etc/postfix/sasl_passwd.db 
 </code></pre> 

 Add postfix to be started at boot 
 <pre><code class="bash"> 
 systemctl enable postfix 
 </code></pre> 

 h2. Test postfix 

 Fire up Postfix 
 <pre><code class="bash"> 
 systemctl start postfix 
 </code></pre> 

 Test it out using sendmail alias from the command prompt 
 <pre><code class="bash"> 
 sendmail email@any.com 
 Postfix is good to go. 
 . 
 </code></pre>