Config postfix smarthost » Historie » Version 1
  Jeremias Keihsler, 13.01.2017 12:07 
  
| 1 | 1 | Jeremias Keihsler | h1. Setting up Postfix to send mails via external mail-account  | 
|---|---|---|---|
| 2 | |||
| 3 | h2. Preliminary note  | 
||
| 4 | |||
| 5 | See also:  | 
||
| 6 | * https://www.zulius.com/how-to/set-up-postfix-with-a-remote-smtp-relay-host/  | 
||
| 7 | * http://rs20.mine.nu/w/2011/07/gmail-as-relay-host-in-postfix/  | 
||
| 8 | |||
| 9 | 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.  | 
||
| 10 | |||
| 11 | h2. Install postfix  | 
||
| 12 | |||
| 13 | Should be preinstalled, as it is the default mail-handler with CentOS  | 
||
| 14 | <pre><code class="bash">  | 
||
| 15 | yum install postfix cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain  | 
||
| 16 | </code></pre>  | 
||
| 17 | |||
| 18 | h2. configure postfix  | 
||
| 19 | |||
| 20 | <pre><code class="bash">  | 
||
| 21 | vim /etc/postfix/main.cf  | 
||
| 22 | </code></pre>  | 
||
| 23 | |||
| 24 | <pre>  | 
||
| 25 | myorigin = example.com  | 
||
| 26 | # Set this to your email provider's smtp server.  | 
||
| 27 | # A lot of ISP's (ie. Cox) block the default port 25  | 
||
| 28 | # for home users to prevent spamming. So we'll use port 80  | 
||
| 29 | relayhost = mail.example.com:587  | 
||
| 30 | |||
| 31 | smtpd_sasl_auth_enable = yes  | 
||
| 32 | smtpd_sasl_path = smtpd  | 
||
| 33 | smtp_use_tls = yes  | 
||
| 34 | smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd  | 
||
| 35 | smtp_sasl_type = cyrus  | 
||
| 36 | smtp_sasl_auth_enable = yes  | 
||
| 37 | |||
| 38 | # optional: necessary if email provider uses load balancing and  | 
||
| 39 | # forwards emails to another smtp server  | 
||
| 40 | # for delivery (ie: smtp.yahoo.com --> smtp.phx.1.yahoo.com)  | 
||
| 41 | smtp_cname_overrides_servername = no  | 
||
| 42 | |||
| 43 | # optional: necessary if email provider  | 
||
| 44 | # requires passwords sent in clear text  | 
||
| 45 | smtp_sasl_security_options = noanonymous  | 
||
| 46 | |||
| 47 | canonical_maps = hash:/etc/postfix/canonical  | 
||
| 48 | </pre>  | 
||
| 49 | |||
| 50 | additionally we need to know the server and email-address as well as the password to use  | 
||
| 51 | <pre><code class="bash">  | 
||
| 52 | vim /etc/postfix/sasl_passwd  | 
||
| 53 | </code></pre>  | 
||
| 54 | |||
| 55 | <pre>  | 
||
| 56 | mail.example.com:587 mailadr@example.com:password  | 
||
| 57 | </pre>  | 
||
| 58 | |||
| 59 | The above server hostname and port must exactly match the value for "relayhost" in /etc/postfix/main.cf.  | 
||
| 60 | |||
| 61 | Generate a postfix lookup table from the previous file  | 
||
| 62 | <pre><code class="bash">  | 
||
| 63 | postmap hash:/etc/postfix/sasl_passwd  | 
||
| 64 | </code></pre>  | 
||
| 65 | |||
| 66 | Test the lookup table, if all is good then the following will return the specified username:password  | 
||
| 67 | <pre><code class="bash">  | 
||
| 68 | postmap -q mail.example.com:587 /etc/postfix/sasl_passwd  | 
||
| 69 | </code></pre>  | 
||
| 70 | |||
| 71 | 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.  | 
||
| 72 | <pre><code class="bash">  | 
||
| 73 | vim /etc/postfix/canonical  | 
||
| 74 | </code></pre>  | 
||
| 75 | |||
| 76 | <pre>  | 
||
| 77 | root mailadr@example.com  | 
||
| 78 | </pre>  | 
||
| 79 | |||
| 80 | The above email-address must exactly match the email-address in sasl_passwd.  | 
||
| 81 | |||
| 82 | Generate a postfix lookup table from the previous file  | 
||
| 83 | <pre><code class="bash">  | 
||
| 84 | postmap hash:/etc/postfix/canonical  | 
||
| 85 | </code></pre>  | 
||
| 86 | |||
| 87 | Make sure that sasl_passwd and sasl_passwd.db files are readable/writeable only by root  | 
||
| 88 | <pre><code class="bash">  | 
||
| 89 | chmod 600 /etc/postfix/sasl_passwd  | 
||
| 90 | chmod 600 /etc/postfix/sasl_passwd.db  | 
||
| 91 | </code></pre>  | 
||
| 92 | |||
| 93 | Add postfix to be started at boot  | 
||
| 94 | <pre><code class="bash">  | 
||
| 95 | chkconfig --add postfix  | 
||
| 96 | </code></pre>  | 
||
| 97 | |||
| 98 | h2. Test postfix  | 
||
| 99 | |||
| 100 | Fire up Postfix  | 
||
| 101 | <pre><code class="bash">  | 
||
| 102 | /etc/init.d/postfix start  | 
||
| 103 | </code></pre>  | 
||
| 104 | |||
| 105 | Test it out using sendmail alias from the command prompt  | 
||
| 106 | <pre><code class="bash">  | 
||
| 107 | sendmail email@any.com  | 
||
| 108 | Postfix is good to go.  | 
||
| 109 | .  | 
||
| 110 | </code></pre>  |