Send Email or SMS Messages from Terminal
This post is about how to send an Email or SMS message from a Raspberry Pi Terminal command line without subscribing to a SMTP relay service or SMS message Gateway.
Searching for a completely free solution, I found that some carriers offer specific email-to-SMS gateway. A List of SMS gateways for carriers in the United States below:
Carrier | Email to SMS |
AT&T | number@txt.att.net |
Boost Mobile | number@sms.myboostmobile.com |
Cricket Wireless | number@sms.cricketwireless.net |
Google Fi | number@msg.fi.google.com |
MetroPCS | number@mymetropcs.com |
Sprint | number@messaging.sprintpcs.com |
T-Mobile | number@tmomail.net |
US Cellular | number@email.uscc.net |
Verizon | number@vtext.com |
Virgin Mobile | number@vmobl.com |
(This list of gateways can change, check with each carrier prior to sending out any email-to-SMS messages.)
My solution use the msmtp SMTP client and a Yahoo email account to send an Email or SMS message via an email to a mobile phone .
Table of Contents
1. Generate a Yahoo app password
2. Install the msmtp SMTP client
3. Send Email from command line
4. Send SMS message via Email from command line
5. Conclusion
6. Links
Generate a Yahoo app password
- Sign in to your Yahoo Account Security page
- Click Generate app password or Generate and manage app passwords
- Click Get Started
- Enter your app's name in the text field (msmtp)
- Click Generate password (copy-save the password)
- Use the app password to log in from a 3rd party app
- Click Done Note: the first time you use the app password you might get a email informing you know that some one logged in using the app password.
Heads up app password: Don't include dashes or spaces it should be 16 charecter
Install the msmtp SMTP client
- Log in to your Yahoo email account
- SSH in to the Raspberry Pi
(enter the following commands) - sudo apt-get update
- sudo apt-get install msmtp
- sudo nano /etc/msmtprc
- copy/past:
# Set default values for all following accounts.
before you save change "youremail" and "app-password"
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# yahoo
account yahoo.com
host smtp.mail.yahoo.com
port 587
from youremail@yahoo.com
user youremaile@yahoo.com
password app-passwod
# Default
account default : yahoo.com
Ctrl x-y to save
Send Email from command line
- Send email to yourself
(enter the following commands change "youremail" )printf "To: youremail@yahoo.com\n\nSubject: Test email\n\nHello, This is my test message." | msmtp -a default youremail@yahoo.com
Send SMS message via Email from command line
If you received "Hello, This is my test message." from the Send Email from command line test you can now try to send a SMS Messages via email to your mobile device.
- AT&T Example
(command change "youremail" and "yourphonenumber - 10-digit wireless number" )echo -e "Subject: Test SMS\r\n\r\nHello, This is my test message." |msmtp --debug --from=youremail@yahoo.com -t yourphonenumber@txt.att.net
Conclusion
OK, It is possible to send an Email or SMS message from a Raspberry Pi terminal.
This post only demonstrate how to send an Email or SMS via email from command line.
Example: This can be used in a bash script which monitors a Temperature sensors which sends a High-Low temp alert to an email address or mobile phone number. (See Link Below)
Yahoo un-secured app access was used because it was easy to set up.
Your email account might have un-secured app access, so you can use that with the necessary (msmtp SMTP client) changes.
Note: Google removed un-secured app access so this won't work with Gmail.
Note: "msmtp" was tested on Raspberry Pi Buster.
Note: "ssmtp" does not seem to work on Raspberry Pi Buster.