How to configure postfix mail for iCloud & GMail
Perfect to configure alertnotify in bitcoin.conf for bitcoind
i.e. alertnotify=echo %s | mail -a "From: Sender <[email protected]>" -s "Bitcoin Alert" [email protected]
On Linux, Raspberry Pi, Debian, Ubuntu install:
$ sudo apt-get install postfix mailutils libsasl2-modules
Includes examples on how to:
- send email from terminal using
mail
- attach a single file using
uuencode
orsendmail
- attach multiple files using
mutt
Edit the postfix main config file
$ sudo vi /etc/postfix/main.cf
Add the following lines
relayhost = [smtp.mail.me.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_generic_maps = hash:/etc/postfix/generic
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_mechanism_filter = plain
Create/Edit sasl_passwd
Create a new App Password on https://appleid.apple.com
$ sudo vi /etc/postfix/sasl_passwd
Add the following line
[smtp.mail.me.com]:587 [email protected]:insert_app_password_here
Secure the sasl_passwd
file
$ sudo chmod 400 /etc/postfix/sasl_passwd
Edit the generic maps
$ sudo vi /etc/postfix/generic
Add the following line
[email protected] [email protected]
Read the map files
$ sudo postmap /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/generic
Restart postfix on macOS:
$ sudo launchctl stop org.postfix.master
$ sudo launchctl start org.postfix.master
on Linux:
$ sudo systemctl reload postfix
Test
$ echo "email body" | mail -a "From: Sender <[email protected]>" -s "Email Subject" [email protected]
If, for whatever reason, it doesn't work, check the logs as follows:
$ cat /var/log/mail.log
Send files
- Using uuencode
# Some people call it the ancient way of sending files, but it works and it's simple
$ uuencode ~/Downloads/1228.jpeg ~/Downloads/1228.jpeg | mail -s "file attached 2" [email protected]
- Using sendmail
The verbose but controlled way, can even overwrite the sender email address
$ ( printf '%s\n' \
"Subject: AptoPunks #1228 $(date)" \
"MIME-Version: 1.0" \
"Content-type: image/jpeg; name=\"1228.jpeg\"" \
"Content-Transfer-Encoding: base64" \
""
base64 < ~/Downloads/1228.jpeg ) | sendmail -t [email protected]
- Using sendmail to send multiple files (only the body example)
Content-Type: multipart/mixed; boundary="1cleqPdUuGeYxzy/"
Content-Disposition: inline
--1cleqPdUuGeYxzy/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
See two punks attached
--1cleqPdUuGeYxzy/
Content-Type: image/jpeg
Content-Disposition: attachment; filename="1228.jpeg"
Content-Transfer-Encoding: base64
--1cleqPdUuGeYxzy/
Content-Type: image/jpeg
Content-Disposition: attachment; filename="2478.jpeg"
Content-Transfer-Encoding: base64
--1cleqPdUuGeYxzy/--
- Alternatively install
mutt
to send attachments easily
$ brew install mutt
$ echo "See two punks attached" | mutt -s "Punk #2478 and Punk #1228" \
-a ~/Downloads/1228.jpeg \
-a ~/Downloads/2478.jpeg \
-- [email protected]
Edit the postfix main config file
$ sudo vi /etc/postfix/main.cf
Add the following lines
Comment smtp_generic_maps
if it's set
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_mechanism_filter = plain
Create/Edit sasl_passwd
Create a new App Password https://myaccount.google.com/apppasswords
$ sudo vi /etc/postfix/sasl_passwd
Add the following lines
[smtp.gmail.com]:587 [email protected]:insert_app_password_here
Secure the file
$ sudo chmod 400 /etc/postfix/sasl_passwd
Make sure there aren't any maps in /etc/postfix/generic
$ sudo vi /etc/postfix/generic
Read the map files
$ sudo postmap /etc/postfix/sasl_passwd
Restart postfix on macOS:
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master
on Linux:
$ sudo systemctl reload postfix