December 27, 2023

Send emails from Ubuntu with GMail account





If you're looking to send emails from your Ubuntu system using a GMail account, here's a straightforward method using Postfix. Follow these steps to set it up:


Prerequisites:

Ensure you have an application password for your GMail account.


On Ubuntu:

1. Install the necessary software:

$ sudo apt-get update
$ sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules


2. Edit or add the following lines in the file /etc/postfix/main.cf:

relayhost = [smtp.gmail.com]:587
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes


3. Add your GMail account and app password to /etc/postfix/sasl_passwd:

[smtp.gmail.com]:587  [email protected]:YOUR_APP_PASSWORD


4. Set permissions:

$ sudo chmod 400 /etc/postfix/sasl_passwd


5. Generate the password file to create /etc/postfix/sasl_passwd.db:

$ sudo postmap /etc/postfix/sasl_passwd


6. Check and restart Postfix:

$ sudo postfix check
$ sudo systemctl restart postfix
$ sudo systemctl status postfix


7. Test the setup:

$ echo "Hello World" | mail -s "Email from Ubuntu using GMail" [email protected]
$ tail /var/log/mail.log


That's it! You should now be able to send emails from your Ubuntu system using your GMail account through Postfix.



No comments: