So there are all kinds of RaspberryPi projects out there, and many of them involve some kind of capture and ensuing notification. Pictures from the Pi camera, or motion logs from a passive infrared sensor, or any other alerts you could imagine. Some projects tell you how to post these these to Twitter, Facebook, google drive, and a bunch of others. Some even suggest using email, and this is what I want to do, use email. The one thing I have found lacking, is clear, concise details on using Gmail as the email server. While there are a ton of details, it took me a while to piece it all together for myself, and these are my findings.

The key to this whole operation, in my opinion, is the Linux CLI email client. There are tons. So many, in fact, that I hard time simply deciding which email client to use. In the end, I decided on 'mutt'. There are a number of reasons, but here are the primary ones:

  • allows me to send emails with attachments (and additional formatting) from the CLI
  • it is an interactinve GUI for email, so I can use the same program to also manually check the inbox and send mails from the same CLI
  • still under active development
  • it has been around for EVER, so it has a very VERY large userbase for info

Let's start!

  • First, we need to install mutt. With Debian, use the following command:
    • sudo apt-get install mutt
  • Create and edit the mutt config file and directory:
    • mkdir ~/.mutt
    • touch ~/.mutt/muttrc
    • nano ~/.mutt/muttrc
  • Add the following info to the file (substituting the username and password as needed):

set from = "This email address is being protected from spambots. You need JavaScript enabled to view it."
set realname = "From Name"
set imap_user = "This email address is being protected from spambots. You need JavaScript enabled to view it."
set imap_pass = "P@ssw0rd"
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed ="+[Gmail]/Drafts"
set header_cache =~/.mutt/cache/headers
set message_cachedir =~/.mutt/cache/bodies
set certificate_file =~/.mutt/certificates
set smtp_url = "smtp://This email address is being protected from spambots. You need JavaScript enabled to view it.:587/"
set smtp_pass = "P@ssw0rd"
set move = no
set imap_keepalive = 900

  • Save and exit the file
  • Done!

Now, for a few commands to test this out.

  • Send a basic test email:

echo "Test Message" | mutt -s "Test Subject" This email address is being protected from spambots. You need JavaScript enabled to view it.

  • Send an email to multiple receipients:

echo "Test Message" | mutt -s "Test Subject" This email address is being protected from spambots. You need JavaScript enabled to view it.,This email address is being protected from spambots. You need JavaScript enabled to view it.,This email address is being protected from spambots. You need JavaScript enabled to view it.

  • Carbon Copy (CC) receipient2:

echo "Test Message" | mutt -s "Test Subject" -c This email address is being protected from spambots. You need JavaScript enabled to view it. This email address is being protected from spambots. You need JavaScript enabled to view it.

  • Blind Carbon Copy (BCC) receipient2:

echo "Test Message" | mutt -s "Test Subject" -b This email address is being protected from spambots. You need JavaScript enabled to view it. This email address is being protected from spambots. You need JavaScript enabled to view it.

  • Send an email where a file is the body:

mutt -s "Subject" This email address is being protected from spambots. You need JavaScript enabled to view it. < /path/to/file.txt

  • Send email with attachment:

mutt -s "Subject" -a /file/to/attach.zip This email address is being protected from spambots. You need JavaScript enabled to view it.

  • Send email with a file for the body AND an attachment:

echo /path/to/file.txt | mutt -s "Attachment!" -a /file/to/attach.zip This email address is being protected from spambots. You need JavaScript enabled to view it. <-- UNTESTED

or

mutt -s "Subject" -a /file/to/attach.zip -- This email address is being protected from spambots. You need JavaScript enabled to view it. < /path/to/file.txt <-- UNTESTED

  • Send email with body formatted as HTML:

mutt -e "set content_type=text/html" -s "subject" This email address is being protected from spambots. You need JavaScript enabled to view it. < test.html

  • Send email with body formatted as HTML with attachment:

echo /path/to/file.html | mutt -e "set content_type=text/html" -s "Subject" -a /file/to/attach.zip This email address is being protected from spambots. You need JavaScript enabled to view it.  <-- UNTESTED

or

mutt -e "set content_type=text/html" -s "Subject" -a /file/to/attach.zip -- This email address is being protected from spambots. You need JavaScript enabled to view it. < /path/to/fill.html  <-- UNTESTED

NOTE: Where I have indicated UNTESTED, this means that it has not been tested by me, personally. If/when I get around to testing them, I will make note of it here.

What other CLI email tools do you use? What purpose(s) does it serve for you? Let me know what you think in the comments section below!