3.4: Sending email
- Page ID
In this appendix we will go over how to send emails with attachments through the Raspbian command line. These instructions are for Raspberry Pis using the Raspbian Buster operating system in mid February of 2020.
These instructions assume you have a Google account, which you will use to send the emails. Because your account password is hard coded into your Pi it is hackable, and you should create a separate account that is only used for IOT activities and this class. Instructions for doing this can be found in appendix 5.2.
Before proceeding you must set up a Google Account for use in this activity, and know its password.
In this activity you will use two packages that are not part of the original software you downloaded and installed when you created your disk image. A package is a collection of modules that do something like connect to a mail server or attach a file to an email. The two packages we need are msmtp and mutt. Note, as of July 2019 the operating image changed from Raspbian Sketch to Raspbian Buster, and in Sketch the smtp package was used.
Anytime you install new packages it is important to update your operating systems.
Update your Raspberry Pi. and install the following libraries:
pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo apt-get upgrade pi@raspberrypi:~ $ sudo apt-get install msmtp pi@raspberrypi:~ $ sudo apt-get install mutt
SMTP stands for Simple Mail Transfer Protocol and is an internet standard protocol for sending email (see wikipedia). MSMTP is a free SMTP client
Mutt is a command line email client. The term "Mutt" comes from its hybrid nature in that it used a bunch of features from other email clients, as indicated in the article that was trawled by the WayBackMachine in 1997.
Create Configuration Files
You need to create new configuration files for each of the packages you added because they don’t exist in the system yet. In the configuration files you will direct them to your Google Account and provide the passwords. As these configurations files could be discoverable you need to be sure not to put into them any passwords that you routinely use. You will use the nano command line editor that was installed with Raspbian. Note that you need to use your keyboard to navigate the file you are creating or editing.
MSMTP configuration file (.msmtprc)
Go to your command line and type:
pi@raspberrypi:~ $ nano
This will open a new file in nano editor. Note, you may need to type sudo nano
account gmail tls on auth on host smtp.gmail.com port 587 user YourPiEmail@gmail.com from YourPiEmail@gmail.com password YourPassword
Where “YourPiEmail” is your newly created email and “YourPassword” is the password for that email.
IMPORTANT: This is why you had to create a new email and a unique password that you won’t use anywhere else. This password is now hard coded here. Anyone can discover this file and see your password.
To save the file type Ctrl+X to exit the editor, then Y for “Yes” and finally nano will ask you for the name of this file. The name has to be .msmtprc (note that the dot is required and this is a hidden file).
Mutt Configuration file (.muttrc)
Go to the command line and create a new file. (If you type over the .msmtprc file be sure to change the name when you exit and save it)
pi@raspberrypi:~ $ nano
Now type the following
set sendmail="/usr/bin/msmtp" set use_from=yes set realname="Name" set from=YourPiEmail@gmail.com set envelope_from=yes
In “Name” field put any name you want displayed in your emails. Save the file with the name .muttrc
Change Permissions of Config files
You need to change files permissions using command-line-utility called chmod. Chmod is a well known command line utility, that's used to manage file permissions on MacOS, Linux and other Unix like operating systems. To change permissions type:
pi@raspberrypi:~ $ chmod 600 .msmtprc pi@raspberrypi:~ $ chmod 600 .muttrc
Create email file
You now need to create an email to send.
Create a new file with nano and type the code found below. Name this file demo_email
from: Name <YourPiEmail@gmail.com> to: <YourEmail@ualr.edu> subject: Test This is the body of the email This is the second line of the body
To send your first email type:
pi@raspberrypi:~ $ cat demo_email | msmtp -a gmail YourEmail@ualr.edu
where YourEmail@ualr.edu is the email address you want to send the email to.
Send email with attachments
6. You can also send emails with attachments from command line. To do that, first make sure to save your attachment in /home/pi/Downloads (this is the location we will be using in this example, but you can use any other location as long as you know how to get there). Now you need to go to Downloads directory in your command line. To do that type:
pi@raspberrypi:~ $ cd pi@raspberrypi:~ $ ls
This will show you the contents of your /home/pi directory. To choose a directory, say your download folder type:
pi@raspberrypi:~ $ cd Downloads/
Now you are in the directory that contains the file that you want to send as an attachment. Use the ls command again to see the contents of this directory and make sure that you see the name of your file on the list. Finally, send the attachment by typing:
pi@raspberrypi:~/Downloads $ mutt -a filename.ext -s "Attachment from Raspberry Pi" -- YourEmail@ualr.edu
where ext is the file extension (jpg,txt,...)
Hit “Enter” for the next two screens. You will see a new file open. This is the body of your email. Type:
This is the first line of the email.
This is the second line of the email.
Save the file with the autogenerated name. You will see the final draft of your email. Hit “Y” to send.