Skip to content

Commit 4d66f15

Browse files
committed
feat: add postfix config
1 parent a9541dc commit 4d66f15

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
layout: post
3+
title: "Configuring Postfix with Gmail"
4+
date: 2023-06-26 00:00:00 -0400
5+
category: "Service Setup"
6+
tags: ['linux', 'email', 'gmail']
7+
---
8+
9+
Email is often a critical component notification for jobs and other things in linux. This is how I set up [Postfix](https://www.postfix.org/) on most of my instances. This guide is geared towards Debian based distros but can be translated for others.
10+
11+
## Required Software
12+
13+
```bash
14+
sudo apt install postfix mailutils -y
15+
```
16+
17+
Both Postfix and mailutils make this much easier
18+
19+
## Configuration
20+
21+
Open the Postfix configuration file:
22+
23+
```bash
24+
sudo nano /etc/postfix/main.cf
25+
```
26+
27+
Append the following to the end:
28+
29+
```conf
30+
relayhost = [smtp.gmail.com]:587
31+
smtp_sasl_auth_enable = yes
32+
smtp_sasl_password_maps = hash:/etc/postfix/gmail_credentials
33+
smtp_sasl_security_options = noanonymous
34+
smtp_use_tls = yes
35+
```
36+
37+
Notice that instead of adding your gmail credentials directly, we're adding them to a file named `gmail_credentials`. Your credentials will depend on whether you use multi-factor authentication (MFA/2FA) or not. If you are now, then you can just put your credentials directly into this file, otherwise you'll need to create an app password. It is highly recommended to use multi-factor authentication whenever possible so I'll assume you are.
38+
39+
> It is highly recommended to use multi-factor authentication whenever possible
40+
{: .prompt-tip }
41+
42+
Go to your [Google account page](https://myaccount.google.com/) and select _Security_ from the side navigation. Then in the center, select _2-Step Verification_. After verifying it's really you, scroll to the bottom and find _App Passwords_. From this page you can generate a new app specific password. I often select custom from the drop down and give it a descriptive name, then select _Generate_.
43+
44+
Remember to copy the generated password, because you will never see it again. Now you can use this new generated password instead of your actual password in your `gmail_credentials` file.
45+
46+
```bash
47+
sudo nano /etc/postfix/gmail_credentials
48+
```
49+
50+
```conf
51+
[smtp.gmail.com]:587 <username>@gmail.com:<password>
52+
```
53+
54+
Once you have your credentials file created, you need to secure it add enroll it with postfix.
55+
56+
```bash
57+
sudo chmod 400 /etc/postfix/gmail_credentials
58+
sudo postmap /etc/postfix/gmail_credentials
59+
```
60+
61+
Now you can reload the Postfix service
62+
63+
```bash
64+
sudo systemctl reload postfix
65+
```
66+
67+
## Testing
68+
69+
Once reloaded, you should be able to send email from the CLI
70+
71+
```bash
72+
echo "This is a test email" | mail -s "Test of Postfix using Gmail" <your@email.address>
73+
```

0 commit comments

Comments
 (0)