Projekt

Allgemein

Profil

Config postfix smarthost » Historie » Version 1

Jeremias Keihsler, 02.04.2024 11:01

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
dnf 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
# line ~118...
26
myorigin = example.com
27
# Set this to your email provider's smtp server.
28
# A lot of ISP's (ie. Cox) block the default port 25
29
# for home users to prevent spamming.  So we'll use port 587
30
# line ~339...
31
relayhost = mail.example.com:587
32
33
# add at end of file...
34
smtpd_sasl_auth_enable = yes
35
smtpd_sasl_path = smtpd
36
smtp_use_tls = yes
37
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
38
smtp_sasl_type = cyrus
39
smtp_sasl_auth_enable = yes
40
 
41
# optional: necessary if email provider uses load balancing and
42
# forwards emails to another smtp server
43
# for delivery (ie: smtp.yahoo.com --> smtp.phx.1.yahoo.com)
44
smtp_cname_overrides_servername = no
45
 
46
# optional: necessary if email provider
47
# requires passwords sent in clear text
48
smtp_sasl_security_options = noanonymous
49
50
canonical_maps = hash:/etc/postfix/canonical
51
</pre>
52
53
additionally we need to know the server and email-address as well as the password to use
54
<pre><code class="bash">
55
vim /etc/postfix/sasl_passwd
56
</code></pre>
57
58
<pre>
59
mail.example.com:587     mailadr@example.com:password
60
</pre> 
61
62
The above server hostname and port must exactly match the value for "relayhost" in /etc/postfix/main.cf.
63
64
Generate a postfix lookup table from the previous file
65
<pre><code class="bash">
66
postmap hash:/etc/postfix/sasl_passwd
67
</code></pre>
68
69
Test the lookup table, if all is good then the following will return the specified username:password
70
<pre><code class="bash">
71
postmap -q mail.example.com:587 /etc/postfix/sasl_passwd
72
</code></pre>
73
74
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.
75
<pre><code class="bash">
76
vim /etc/postfix/canonical
77
</code></pre>
78
79
<pre>
80
root     mailadr@example.com
81
</pre> 
82
83
The above email-address must exactly match the email-address in sasl_passwd.
84
85
Generate a postfix lookup table from the previous file
86
<pre><code class="bash">
87
postmap hash:/etc/postfix/canonical
88
</code></pre>
89
90
Make sure that sasl_passwd and sasl_passwd.db files are readable/writeable only by root
91
<pre><code class="bash">
92
chmod 600 /etc/postfix/sasl_passwd
93
chmod 600 /etc/postfix/sasl_passwd.db
94
</code></pre>
95
96
Add postfix to be started at boot
97
<pre><code class="bash">
98
systemctl enable postfix
99
</code></pre>
100
101
h2. Test postfix
102
103
Fire up Postfix
104
<pre><code class="bash">
105
systemctl start postfix
106
</code></pre>
107
108
Test it out using sendmail alias from the command prompt
109
<pre><code class="bash">
110
sendmail email@any.com
111
Postfix is good to go.
112
.
113
</code></pre>