How to Send an Email from a Gmail Account using Ubuntu Command Line
Learn how to send an email from a Gmail account using the Ubuntu command line. Specifically, we will use the SendEmail apt package. We will also learn what an app password is and why Google requires one for third party apps.
Table of Contents 📖
Gmail Security and App Passwords
Sending an email with a Gmail account using a third party email client isn't as easy as other email providers as Google doesn't allow us to use a regular password for 3rd party applications. Instead, we have to generate an app password for our Gmail account. An app password is a 16 digit passcode that gives less secure apps permission to access our Google account.
INFO: Note that an app password can only be generated for accounts that have two factor authentication enabled. So if you are using an account without two factor authentication, you will not be able to generate an app password.
To generate an app password, follow these steps:
- Navigate to your Google account.
- In the search bar type "app password" and click on it in the drop down.
- Create an app name and click "Create".
- After clicking "Create" an app password should be displayed on the screen.
INFO: Copy the code and save it somewhere safe as if it is lost you will have to generate a new one.
Installing SendEmail
To send emails from the Ubuntu command line, we will be using SendEmail, a lightweight SMTP email client. It can be installed using apt.
sudo apt install sendemail
To check the package was installed correctly, run sendemail --help.
sendemail --help
sendemail-1.56 by Brandon Zehm <caspian@dotconf.net>
Sending an Email
We can send an email with sendemail using the following command:
sendemail \
-f <FROM_EMAIL> -t <TO_EMAIL> \
-u <EMAIL_SUBJECT> -m <EMAIL_BODY> \
-s smtp.gmail.com:587 -o tls=yes \
-xu <YOUR_GMAIL> -xp <APP_PASSCODE>
- -f - The email address of the sender.
- -t - The email address of the recipient.
- -u - The subject of the email.
- -m - The body of the email.
- -s - The smtp mail relay. An SMTP mail relay is the process of transferring an email from one server to another. The default is localhost:25.
- -o - Advanced options.
- tls - Enable/disable TLS. Setting it to yes requires TLS, which is required when using Gmail.
- -xu - The username for the email account. Alias for -o username=USERNAME.
- -xp - The password for the email account. For Gmail this is the app password. Alias for -o password=PASSWORD.
INFO: SMTP uses port 25 while SMTPS (Secure SMTP) uses port 587.
However, if we run this command we will get the following error:
ERROR => No TLS support! SendEmail can't load required libraries. (try installing Net::SSLeay and IO::Socket::SSL)
Installing TLS Libraries
We get this error because when using TLS, which Gmail requires, we need to have the IO::Socket::SSL and Net::SSLeay perl modules available. We can check this by running apt-cache depends.
apt-cache depends sendemail
sendemail
Depends: perl
Depends: libio-socket-inet6-perl
Suggests: libio-socket-ssl-perl
Suggests: libnet-ssleay-perl
apt-cache queries and displays information about the provided package. The depends command shows dependency information for a package. To get TLS to work, lets install both the required and suggested packages. For example, we can install libio-socket-ssl-perl like so:
apt install libio-socket-ssl-perl
...
The following additional packages will be installed:
libnet-libidn-perl libnet-ssleay-perl
...
The following NEW packages will be installed:
libio-socket-ssl-perl libnet-libidn-perl libnet-ssleay-perl
INFO: libio-socket-ssl-perl is a drop-in replacement for IO::Socket::INET that uses SSL to encrypt data before it is transferred to a remote server or client.
INFO: libnet-ssleay-perl is a drop-in replacement for Net::SSLeay that allows you to call SSL functions.
INFO: libio-socket-inet6-perl is a drop-in replacement for IO::Socket::INET6 that provides an object interface to create and use sockets in the AF_INET6 domain.
From checking the logs, we can see that the other packages were installed aswell. However, install each one to be sure. Next simply run the sendemail command again and look for the following logged to the console.
May 06 18:02:48 blog sendemail[783]: Email was sent successfully!