Send authenticated gmail from command line


You can send basic email from the command line. That’s been done a million times before, including by me!
Did you know that you can send email from the command line, from your gmail account that you authenticated to? That’s a big deal for some people. This document describes how to do that.

Sending gmail from command line

Install packages msmtp and mutt.
yum install msmtp mutt
Write the conf file and fill in the pertinent information.
# write conf file
touch "${tf}"; chmod 0600 "${tf}"; chown root:root "${tf}";
cat <<EOF >"${tf}"
account default
tls on
tls_certcheck off
auth on
port 587
password plaintextpassword
account second
tls on
tls_certcheck off
auth on
port 587
password plaintextpassword

Now you can send an email with this command.
echo -e "From: Pretty Name\r\nSubject: Example subject\r\nContent goes here." | msmtp --debug --from=default -t

Sending attachments

Configure mutt if you want to send attachments from the command line.
cat <<EOF >> /etc/Muttrc.local
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="Pretty Name"
set envelope_from=yes

Send an email in html format and with an attachment.
subject="This is the subject
Mime-Version: 1.0
Content-Type: text/html"
mutt -a ~/attachment.txt -s "${subject}" -- << EOF
This will be fixed width font. I find it useful for sending code fragments or log files.

Authenticating to gmail from command line

The Arch Linux wiki provides an important reminder:

Tip: If using Gmail you’ll need to either

  • Allow “Less Secure Apps” in Settings > Security. Make sure to sign out of your other Gmail accounts first because the security settings part of Google Accounts can not manage concurrent sessions of more than one account.
  • Enable two factor authentication and create an app password.


  1. from bgscripts package

One thought on “Send authenticated gmail from command line

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.