Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sending emails using Gmail and msmtp on Ubuntu / Debian
# Sending emails using Gmail and msmtp
# Author: [Josef Jezek](
# Donate: [Gittip](
# Link: [Gist](
# Usage:
sudo apt-get update -q
sudo apt-get install msmtp-mta ca-certificates heirloom-mailx -yq
if command -v zenity >/dev/null; then
GMAIL_USER=$(zenity --entry --title="Gmail username" --text="Enter your gmail username with domain (")
GMAIL_PASS=$(zenity --entry --title="Gmail password" --text="Enter your gmail password:" --hide-text)
read -p "Gmail username with domain ( " GMAIL_USER
read -p "Gmail password: " GMAIL_PASS
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
sudo tee /etc/msmtprc >/dev/null <<__EOF
# Accounts will inherit settings from this section
auth on
tls on
tls_certcheck off
# tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
# A first gmail address
account gmail
port 587
password $GMAIL_PASS
# A second gmail address
account gmail2 : gmail
password password
# A freemail service
account freemail
host smtp.freemail.example
from joe_smith@freemail.example
user joe.smith
password secret
# A provider's service
account provider
host smtp.provider.example
# Set a default account
account default : gmail
sudo chmod 600 /etc/msmtprc
sudo chown -R www-data:www-data /etc/msmtprc
sudo mail -vs "Email relaying configured at ${HOST}" $GMAIL_USER <<__EOF
The postfix service has been configured at host '${HOST}'.
Thank you for using this postfix configuration script.
echo "I have sent you a mail to $GMAIL_USER"
echo "This will confirm that the configuration is good."
echo "Please check your inbox at gmail."

This comment has been minimized.

Copy link
Owner Author

@JosefJezek JosefJezek commented Aug 9, 2013

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

wget && chmod +x && ./


This comment has been minimized.

Copy link

@tomashook tomashook commented Dec 19, 2015

Thank you, it is what I am looking for ... unfortunately it doesn't work due to increased google security ...

Someone just tried to sign in to your Google Account from an app that doesn't meet modern security standards.
Saturday, December 19, 2015 12:56 AM (Pacific Standard Time)
yyy, OR, USA*
We strongly recommend that you use a secure app, like Gmail, to access your account. All apps made by Google meet these security >standards. Using a less secure app, on the other hand, could leave your account vulnerable. Learn more.

If you decide update your script it will be useful.
Thank you.


This comment has been minimized.

Copy link

@rohitsakala rohitsakala commented Apr 26, 2016



This comment has been minimized.

Copy link

@gpicado gpicado commented May 18, 2016

Thank you Sir, wonderful script.
As for the google security issue:

6. Enable "Less Secure Apps" In Gmail

By default, only the most secure sign-ins, such as logging in to Gmail on the web, are allowed for your Gmail account. To permit relay requests, log in to your Gmail account and turn on Allow less secure apps.
For more information, review the Google Support document "Allowing less secure apps to access your account."

source: Configure Postfix to use Gmail as a Mail Relay


This comment has been minimized.

Copy link

@pirj pirj commented Nov 25, 2016

tls_certcheck   off


Regarding enabling less secure apps, it's better to enable 2-step verification and create a per-app password.


This comment has been minimized.

Copy link

@Nikheel001 Nikheel001 commented Mar 14, 2017

I am trying on Android to send mail... From PHP
I require to edit msmtp.cnf file for use mail() function I copied configuration code from your shell script. but not working .. for Android. If you know something can you provide info?? Please help sir


This comment has been minimized.

Copy link

@Mattia98 Mattia98 commented Aug 14, 2017

Hi, the scripts worked. Thank you for that, but the mail command still doesn't send mails..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment