After I discovered that I could automate sending emails with bodies as well as subjects from the command line, I wanted a way for that email to be sent from my Gmail account instead of "user@localname.local". Fortunately, this is pretty easy to do:
Step 1: First, you need to add a chunk of text to the main.cf file. Begin by opening main.cf in Nano:
sudo nano -w /etc/postfix/main.cfOnce the file is open, paste this text into the top of the file:
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_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
To close the file, hit Ctrl-X, Y, then Return.
Step 2: Next we need to edit the sasl_passwd file. Use the following command to open it in Nano:
sudo nano -w /etc/postfix/sasl_passwdAdd the following line of code substituting "username" for your Gmail username and "password" for your gmail password:
[smtp.gmail.com]:587 username@gmail.com:passwordOnce again, close the file by hitting Ctrl-X, Y, then Return.
Step 3: Now we need to put the sasl_passwd file into something that Postfix (the email program) can read. Run the following three commands:
chown root /etc/postfix/sasl_passwdsudo chmod 400 /etc/postfix/sasl_passwdsudo postmap /etc/postfix/sasl_passwdStep 4: Now we need to get a certificate for the email client. While you should get your own certificate, you can use the following one:
-----BEGIN CERTIFICATE-----
MIIDJzCCApCgAwIBAgIBATANBgkqhkiG9w0BAQQFADCBzjELMAkGA1UEBhMCWkExFTATBgNVBAgT
DFdlc3Rlcm4gQ2FwZTESMBAGA1UEBxMJQ2FwZSBUb3duMR0wGwYDVQQKExRUaGF3dGUgQ29uc3Vs
dGluZyBjYzEoMCYGA1UECxMfQ2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjEhMB8GA1UE
AxMYVGhhd3RlIFByZW1pdW0gU2VydmVyIENBMSgwJgYJKoZIhvcNAQkBFhlwcmVtaXVtLXNlcnZl
ckB0aGF3dGUuY29tMB4XDTk2MDgwMTAwMDAwMFoXDTIwMTIzMTIzNTk1OVowgc4xCzAJBgNVBAYT
AlpBMRUwEwYDVQQIEwxXZXN0ZXJuIENhcGUxEjAQBgNVBAcTCUNhcGUgVG93bjEdMBsGA1UEChMU
VGhhd3RlIENvbnN1bHRpbmcgY2MxKDAmBgNVBAsTH0NlcnRpZmljYXRpb24gU2VydmljZXMgRGl2
aXNpb24xITAfBgNVBAMTGFRoYXd0ZSBQcmVtaXVtIFNlcnZlciBDQTEoMCYGCSqGSIb3DQEJARYZ
cHJlbWl1bS1zZXJ2ZXJAdGhhd3RlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0jY2
aovXwlue2oFBYo847kkEVdbQ7xwblRZH7xhINTpS9CtqBo87L+pW46+GjZ4X9560ZXUCTe/LCaIh
Udib0GfQug2SBhRz1JPLlyoAnFxODLz6FVL88kRu2hFKbgifLy3j+ao6hnO2RlNYyIkFvYMRuHM/
qgeN9EJN50CdHDcCAwEAAaMTMBEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQQFAAOBgQAm
SCwWwlj66BZ0DKqqX1Q/8tfJeGBeXm43YyJ3Nn6yF8Q0ufUIhfzJATj/Tb7yFkJD57taRvvBxhEf
8UqwKEbJw8RCfbz6q1lu1bdRiBHjpIUZa4JMpAwSremkrj/xw0llmozFyD4lt5SZu5IycQfwhl7t
UCemDaYj+bvLpgcUQg==
-----END CERTIFICATE-----Copy that to your clipboard, including the "begin certificate" and "end certificate" lines, then run the following command:
pbpaste | sudo tee -a /etc/postfix/cacert.pemStep 5: Finally, we need to restart postfix. You can do this one of two ways. Either run the following command:
sudo postfix reloador reboot your Mac.
That's it! You should now be able to send emails from the "mail" CLI with a proper reply email address. I do not yet know how to receive emails, so this is a one way street for now.


2 comments:
Wow, what a wonderful tip, not only for Mac OS X but for my Linux server too.
Thanks!
Really glad I found this tutorial, simply awesome.
Post a Comment