Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ubuntu sendmail using smtp.gmail.com
#!/bin/bash
HOST=$(hostname)
function install_postfix() {
echo | sudo debconf-set-selections <<__EOF
postfix postfix/root_address string
postfix postfix/rfc1035_violation boolean false
postfix postfix/mydomain_warning boolean
postfix postfix/mynetworks string 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
postfix postfix/mailname string $HOST
postfix postfix/tlsmgr_upgrade_warning boolean
postfix postfix/recipient_delim string +
postfix postfix/main_mailer_type select Internet with smarthost
postfix postfix/destinations string $HOST, localhost.localdomain, localhost
postfix postfix/retry_upgrade_warning boolean
# Install postfix despite an unsupported kernel?
postfix postfix/kernel_version_warning boolean
postfix postfix/not_configured error
postfix postfix/sqlite_warning boolean
postfix postfix/mailbox_limit string 0
postfix postfix/relayhost string [smtp.gmail.com]:587
postfix postfix/procmail boolean false
postfix postfix/bad_recipient_delimiter error
postfix postfix/protocols select all
postfix postfix/chattr boolean false
__EOF
echo "Postfix should be configured as Internet site with smarthost"
sudo apt-get install -q -y postfix
}
if ! dpkg -s postfix >/dev/null;
then
install_postfix
else
echo "Postfix is already installed."
echo "You may consider removing it before running the script."
echo "You may do so with the following command:"
echo "sudo apt-get purge postfix"
echo
fi
if zenity --version >/dev/null; then
GMAIL_USER=$(zenity --entry --title="Gmail username" --text="Enter your gmail username:")
GMAIL_PASS=$(zenity --entry --title="Gmail password" --text="Enter your gmail password:" --hide-text)
else
read -p "Gmail username: " GMAIL_USER
read -sp "Gmail password: " GMAIL_PASS
fi
echo # an empty line
if [ -z "$GMAIL_USER" ]; then echo "No gmail username given. Exiting."; exit -1; fi
if [ -z "$GMAIL_PASS" ]; then echo "No gmail password given. Exiting."; exit -1; fi
if ! [ -f /etc/postfix/main.cf.original ]; then
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.original
fi
sudo tee /etc/postfix/main.cf >/dev/null <<__EOF
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_loglevel = 1
smtp_tls_per_site = hash:/etc/postfix/tls_per_site
smtp_tls_CAfile = /etc/ssl/certs/Equifax_Secure_CA.pem
smtp_tls_session_cache_database = btree:/var/lib/postfix/smtp_tls_session_cache
__EOF
echo "[smtp.gmail.com]:587 $GMAIL_USER:$GMAIL_PASS" | sudo tee /etc/postfix/sasl_passwd >/dev/null
sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
echo "smtp.gmail.com MUST" | sudo tee /etc/postfix/tls_per_site >/dev/null
sudo chmod 400 /etc/postfix/tls_per_site
sudo postmap /etc/postfix/tls_per_site
sudo service postfix restart
echo "Configuration done"
mail -s "Email relaying configured at ${HOST}" $GMAIL_USER@gmail.com <<__EOF
The postfix service has been configured at host '${HOST}'.
Thank you for using this postfix configuration script.
__EOF
echo "I have sent you a mail to $GMAIL_USER@gmail.com"
echo "This will confirm that the configuration is good."
echo "Please check your inbox at gmail."
@sshtmc

This comment has been minimized.

Copy link
Owner Author

commented Oct 25, 2012

You can download and run this using a one-liner:

wget https://raw.github.com/gist/3952294/ -O- | bash

@smonff

This comment has been minimized.

Copy link

commented Nov 28, 2012

Hey, thank you so much for this man !

@ddoherty03

This comment has been minimized.

Copy link

commented Dec 3, 2012

Sasa,

Thanks for this script. It makes a tedious job a snap. I now have cron mailing errors to me again.

Regards,

@kforner

This comment has been minimized.

Copy link

commented Jan 19, 2013

Awesome, thank you so much !

@wvermin

This comment has been minimized.

Copy link

commented Dec 30, 2013

Indeed, awesome, thanks!

@sklazer-disabled

This comment has been minimized.

Copy link

commented Feb 11, 2014

OMG, works like magic!

@IanFinlayson

This comment has been minimized.

Copy link

commented Mar 6, 2014

Wow, thanks I wish I found this a few hours ago!

@cdechery

This comment has been minimized.

Copy link

commented May 27, 2014

Not working for me. :(
I didn't have "mail" installed, so I ran apt-get install mailutils, but it also comes with postfix. So I uninstalled postfix, ran the script again. Still doesn't work. :/

@fpt

This comment has been minimized.

Copy link

commented Aug 14, 2014

Thank you so much!
It works for me on trusty after installing mailutils manually.

@jagoliveira

This comment has been minimized.

Copy link

commented Sep 1, 2014

Works like a charm after installing manually mailutils.
Thanks a lot.

@hoppinjohnz

This comment has been minimized.

Copy link

commented Feb 20, 2016

Hey, thanks a lot!

This script worked great for me. I wasted a day trying to configure sendmail for Ubuntu 14.04 and visited half-dozen sites before finding this well-written script.

It prompted me for not having mail. Once I ran 'sudo apt-get install mailutils' and 'sudo apt-get install sensible-mda', the script started to send email to my newly created gmail account. Don't use your existing gmail accounts, Google may block the traffic as a security protection measure.

@markandersonus

This comment has been minimized.

Copy link

commented Apr 28, 2016

I get this when i run the wget command:

Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|23.235.39.133|:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2016-04-27 22:10:24 ERROR 400: Bad Request.

@AdamPI314

This comment has been minimized.

Copy link

commented Feb 7, 2017

This script works like magic. Save me a lot of time.

@khaledAX

This comment has been minimized.

Copy link

commented Apr 27, 2017

@markandersonus the proper wget command is:

wget https://gist.githubusercontent.com/sshtmc/3952294/raw/7b5b230a04994ab387538b118d7a32dda54eb757/ubuntu-configure-sendmail-with-gmail/ -O- | bash

add sudo if necessary.

@SimonFearby

This comment has been minimized.

Copy link

commented Aug 20, 2017

I can't see where to add my Gmail username and password to /etc/postfix/main.cf

snip from the tail of the command above
Running newaliases
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Processing triggers for systemd (229-4ubuntu19) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
bash: line 44: zenity: command not found
No gmail username given. Exiting.

@wvermin

This comment has been minimized.

Copy link

commented Oct 30, 2017

since end oct. 2017 the thing didn't work anymore. In the syslog complaints:
untrusted issuer /OU=GlobalSign Root CA - R2/O=GlobalSign/CN=GlobalSign
After editing the script: change Equifax_Secure_CA.pem into GlobalSign_Root_CA_-_R2.pem and
re-executing the script it is working again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.