Sending email from Terminal

published at

I'm curious on how to sending email from Terminal via SMTP, so I'm figuring out about it.

The main key of this post was using msmtp program which you can install it by using Homebrew if you are using Mac, or the usual apt install if you are using Ubuntu.

After that, you need to configure the account you want to use. You can execute this command:

$ msmtp --configure=youremail
$ # example
$ msmtp

If you are using iCloud, you need to generate the app-specific password for that.

After that, you will be prompted the output to stdout and you need to save it to ~/.msmtprc. If your email was, the output was like this:

port 587
tls on
tls_starttls on
auth on
user my+awesome+email

You need to set the default account, if that email was your default, you can add account default: into bottom of the file.

If you don't specify a password, you will be prompted everytime you try to send email. To prevent that, you can use passwordeval so the program will read your password from that "command".

Mine was by reading .msmtp-password.gpg which is my password that encrypted using my PGP. To generate it, you can execute this script:

echo "your-app-specific-password-here" | gpg -es -r <your_email> > ~/.msmtp-password.gpg

And add this line to ~/.msmtprc

passwordeval gpg --no-tty -q -d ~/.msmtp-password.gpg

You will never be prompted anymore!

Send email

To start sending email, you can execute this script:

echo "Subject: This is a subject of title" | msmtp <recipient_address>

Command above was to send email to <recipient_address> but with no content.

To send an "actual" email, you can create a file containing your email with this format:

To: <recipient_address>
From: <your_address>
Subject: Email subject

This is a body, you can write it whatever you want

This is in another line

So you can send the email with following contents by using this script:

cat <the_file> | msmtp <recipient_address>

Let's try it. This is an example of result from script above Working seamlessly :))

Attaching file

By default you cannot attach a file in using msmtp, sad.

If you really need in attaching some file, go use some pretty email client.

Or why you just not to attach it as a link?

Is this useful?

lol no. But its quite fun, will trying another email client like mutt or [aerc](, stay tune.

Thanks for reading this.