If your Raspberry Pi operating system was installed before July of 2019 you have Raspbian Sketch and are updating that operating system. There are a few differences as that used the smtp and not the msmtp package.
- Install some software on the Pi
First we install the ssmtp and mutt libraries. SSMTP is a program which delivers email from a local computer to a configured mailhost(mailhub). Mutt is a text-based email client for Unix-like systems. Open a terminal window and type the following:
If you hit the uparrow on the command line you get the previous command So after you type one command, hit the update, and edit the line (instead of typing everything)
pi@hostname:~ $sudo apt-get update pi@hostname:~ $sudo apt-get upgrade pi@hostname:~ $sudo apt-get install ssmtp pi@hostname:~ $sudo apt-get install mutt pi@hostname:~ $sudo apt-get install mailutils pi@hostname:~ $sudo apt-get install mpack
Configure a file
Next we need to make some configuration changes. We will be using a text editor called nano. This is a simple interface for making changes to the files we need to alter. We need to configure the ssmtp.conf file to indicate that we are using the gmail mail hub. To do so at the command prompt type:
pi@hostname:~ $sudo nano /etc/ssmtp/ssmtp.conf
NOTE you must use sudo to allow you to write the changes
Now, you need to alter line 10 of the file. To get to line 10, hit ctrl_ (control and underscore). You can then type in 10 to jump to that line of code.
You will see that the line reads:
change that line of code to read:
Find the last line in the file. It has
Remove the hashtag so it reads
Now after the FromLineOverride line, you will add the following lines:
AuthUserfirstname.lastname@example.org AuthPass=yourgmailpassword UseSTARTTLS=YES UseTLS=YES
Save these changes by hitting ctrl-x (control and x simultaneously, when you do this you exit the nano editor and have the option to save changes).
NOTE yourgmailaccount and your password are from the account you just created.
NOTE 2- very important!!!!! You have just hard coded a password here. This is why you create a new account that you will never use for anything else. Anyone who knows how to find this file will know your password.
- First we will test it out using MUTT, a feature-rich command line email client that we installed with the apt-get install mutt line from above. Test it out by typing the following at the prompt. The first line puts you in your home directory, and the second will send an email to the email you specify. In this example, I am using a generic email@example.com
- for that address. Be sure to change it to your own!
pi@hostname:~ $echo email ‘message from your RasPi’|mutt -s ‘subject line from your RasPi’ your-email@yourschool
NOTE: the above is one command line that may take two lines to enter.
NOTE: You may get a line that state GPGME: CMS protocol not available, but the message should send.
NOTE: The pipe character can be found as the shifted key just above the Enter on your computer:
Echo is a standard unit command to write to something to standard output. In this case we are echoing email "message from you RasPi". We are "piping" the output to the mutt mail client. We are telling the mail client what the subject is and the email address to send the mail to.
- Test it again. This time type the following commands. In the second line after –a type the name of one of your python programs you saw after the ls command:
pi@hostname:~ $cd python_programs pi@hostname:~ $ls pi@hostname:~ $ echo email 'file from your RasPi' |mutt -s 'a file from your RasPi' firstname.lastname@example.org -a one_of_your_programs.py
What showed up in your email? What does the –a do when you add that argument to the command?
7) This time we will test it again using another email utility program that we installed (mailutils). Type the following commands from your current directory:
pi@hostname:~ $ echo "sample text" | mail -s "Subject" email@example.com pi@hostname:~ $ echo "sample text" | mail -s "Subject" firstname.lastname@example.org –attach "somefilename.py" pi@hostname:~ $
What showed up in your email? What does the --attach do when you add that argument to the command?