Send e-mail (and text messages) from the command line
Copyright Β© 2019 Teal Dulcet (both) and Daniel Connelly (Python)
Send e-mail (and text messages), with optional message and attachments, from the command line. Supports Unicode characters in subject, message and attachment filename (MIME). Optionally use your own e-mail address and an external SMTP server.
Useful to know when a cron job failed, when a long running job (LRP) has finished, to quickly backup/share a file or to send notifications as part of a larger script.
See the python-port directory for SendPy, a Python port of the script.
β€οΈ Please visit tealdulcet.com to support these scripts and my other software development.
Requires at least Bash 4.4 and the curl and netcat commands, which are included on most Linux distributions.
Optional S/MIME digital signatures require the openssl command.
Optional PGP/MIME digital signatures require the gpg command.
Run: ./sendmsg.sh <OPTION(S)>... -s <subject>
One or more To, CC or BCC e-mail addresses are required. Send text messages by using the mobile providers e-mail to SMS or MMS gateway. All the options can also be set by opening the script in an editor and setting the variables at the top. See Help below for full usage information.
- Verify that the required commands above are installed.
- Download the script (sendmsg.sh). Run:
wget https://raw.github.com/tdulcet/Send-Msg-CLI/master/sendmsg.sh
. - At a minimum, you need to provide one To e-mail address. If the computer is on a residential network or if it does not have an SMTP server setup then you will also need to provide an external SMTP server. For security, any passwords/passphrases should be set in the script, instead of on the command line.
- Execute the script once to make sure there are no errors. For example, run:
chmod u+x sendmsg.sh
and./sendmsg.sh -s "Test" -m "This is a test!" -t "Example <[email protected]>" -d
. - If you want the script to be available for all users, install it. Run:
sudo cp sendmsg.sh /usr/local/bin/sendmsg
andsudo chmod +x /usr/local/bin/sendmsg
.
See Help below for more examples.
Send a notification when a long running job (LRP) has finished, with the exit code and output:
output=$(myLRP arg1 arg2β¦ 2>&1); ./sendmsg.sh -s "βmyLRP arg1 arg2β¦β has finished"'!' -m "The program βmyLRP arg1 arg2β¦β has finished on β$HOSTNAMEβ"'!'"\nExit code: $?\nOutput:\n$output\n"
Replace myLRP arg1 arg2β¦
with the actual program and arguments.
Backup/Share a file:
./sendmsg.sh -s "Log file" -m "Please see the attached log file." -a status.log
Send notifications as part of a larger script:
./sendmsg.sh -s "β¬οΈ Example Website is DOWN"'!' -m "Example Website (https://www.example.com/) is currently DOWN"'!'"\n\nThis script will alert you when it is back up.\n"
Example adapted from the Linux Remote Servers Status Monitoring Script.
Instructions
To send e-mail from a Gmail account, add these options to the command: -f "User <[email protected]>" -S "smtps://smtp.gmail.com" -u "[email protected]" -p "PASSWORD"
. Or, open the script in an editor and set these variables near the top, where listed:
FROMEMAIL="User <[email protected]>"
SMTP="smtps://smtp.gmail.com"
USERNAME="[email protected]"
PASSWORD="PASSWORD"
Replace example
with the username and PASSWORD
with the actual password. For security, the password should be set in the script, instead of on the command line.
You will also need to either create an App Password or enable "Less secure app access": https://myaccount.google.com/lesssecureapps. It is not actually less secure, since it is using the same SSL/TLS encryption (note the smtps://
protocol). Note that Google may disable this setting if it is not being used. If you get a "Login denied" error, try visiting this page: https://accounts.google.com/DisplayUnlockCaptcha.
This Send E-mail Script | S-nail (formerly Heirloom mailx) | Mutt | SSMTP | SendEmail | smtp-cli | |||
---|---|---|---|---|---|---|---|---|
Send e-mail | β | β | β | β | β | β | β | |
Send text messages (e-mail to SMS) | β | β | β | β | β | β | β | |
Use your own e-mail address | β* | β* | β* | β | β | β | β | |
Use an external SMTP server | β* | β* | β* | β | β* | β* | β* | |
Include attachment(s) | β | β | β | β | β | β | ||
Send e-mails to CC and BCC addresses | β | β | β | β | β | |||
Supports e-mail addresses with display names | β | β | β | β | β | |||
Supports HTML formatted messages | β | β | ||||||
Supports Unicode characters in subject and message (MIME) | β | β | β | β | β | Message only | ||
Supports International email addresses | ββ | β | β | |||||
Supports E-mail Priority | β | High Priority only | ||||||
Digitally sign the e-mails | S/MIME certificate | β | β | β | ||||
PGP/MIME | β | β | β | |||||
Does NOT require compiling or installing anything | β | |||||||
100% Open Source | β | β | β | β | β | β | β | |
Free | π | π | π | π | π | π | π |
* Optional
β Only supported in Internationalizing Domain Names in Applications (IDNA) encoding
This is not a comprehensive list of the Send E-mail Scriptβs functionality.
Source: S-nail, Mutt, SSMTP, SendEmail (archived, source), eMail and smtp-cli
$ sendmsg -h
Usage: sendmsg <OPTION(S)>... -s <subject>
or: sendmsg <OPTION>
One or more To, CC or BCC e-mail addresses are required. Send text messages by using the mobile providers e-mail to SMS or MMS gateway (https://en.wikipedia.org/wiki/SMS_gateway#Email_clients). All the options can also be set by opening the script in an editor and setting the variables at the top. See examples below.
Options:
-s <subject> Subject
Escape sequences are expanded. Supports Unicode characters.
-m <message> Message body
Escape sequences are expanded. Supports Unicode characters.
-a <attachment> Attachment filename
Use multiple times for multiple attachments. Supports Unicode characters in filename.
-t <To address> To e-mail address
Use multiple times for multiple To e-mail addresses.
-c <CC address> CC e-mail address
Use multiple times for multiple CC e-mail addresses.
-b <BCC address>BCC e-mail address
Use multiple times for multiple BCC e-mail addresses.
-f <From address>From e-mail address
-S <SMTP server>SMTP server
Supported protocols: "smtp" and "smtps". Requires From e-mail address. Use "smtp://localhost" if running a mail server on this device.
-u <username> SMTP server username
-p <password> SMTP server password
-P <priority> Priority
Supported priorities: "5 (Lowest)", "4 (Low)", "Normal", "2 (High)" and "1 (Highest)". Requires SMTP server.
-r Request Return Receipt
Requires SMTP server.
-C <certificate>S/MIME Certificate filename for digitally signing the e-mails
It will ask you for the password the first time you run the script with this option. Requires SMTP server.
-k <passphrase> PGP secret key passphrase for digitally signing the e-mails with PGP/MIME
Requires SMTP server.
-z <zipfile> Compress attachment(s) with zip
-l Set Content-Language
Uses value of LANG environment variable.
-U Sanitize the Date
Uses Coordinated Universal Time (UTC) and rounds date down to whole minute. Set the TZ environment variable to change time zone.
-T <seconds> Time to delay sending of the e-mail
-d Dry run, do not send the e-mail
-V Verbose, show the client-server communication
Requires SMTP server.
-h Display this help and exit
-v Output version information and exit
Examples:
Send e-mail
$ sendmsg -s "Example" -t "User <[email protected]>"
Send e-mail with message
$ sendmsg -s "Example" -m "This is an example!" -t "User <[email protected]>"
Send e-mail with message and single attachment
$ sendmsg -s "Example" -m "This is an example!" -a example.txt -t "User <[email protected]>"
Send e-mail with message and multiple attachments
$ sendmsg -s "Example" -m "This is an example!" -a example1.txt -a example2.txt -t "User <[email protected]>"
Send e-mail to a CC address
$ sendmsg -s "Example" -t "User 1 <[email protected]>" -c "User 2 <[email protected]>"
Send e-mail with a From address
$ sendmsg -s "Example" -f "Example <[email protected]>" -t "User <[email protected]>"
Send e-mail with an external SMTP server
$ sendmsg -s "Example" -f "Example <[email protected]>" -S "smtps://mail.example.com" -u "example" -p "password" -t "User <[email protected]>"
Send high priority e-mail
$ sendmsg -s "Example" -f "Example <[email protected]>" -S "smtps://mail.example.com" -u "example" -p "password" -P "1 (Highest)" -t "User <[email protected]>"
Send e-mail digitally signed with an S/MIME Certificate
$ sendmsg -s "Example" -f "Example <[email protected]>" -S "smtps://mail.example.com" -u "example" -p "password" -C "cert.p12" -t "User <[email protected]>"
Send e-mail digitally signed with PGP/MIME
$ sendmsg -s "Example" -f "Example <[email protected]>" -S "smtps://mail.example.com" -u "example" -p "password" -k "passphrase" -t "User <[email protected]>"
Pull requests welcome! Ideas for contributions:
Bash:
- Send e-mails with very long subjects or many e-mail addresses
- Support International email addresses
- Currently they are only supported in Internationalizing Domain Names in Applications (IDNA) encoding.
- Support inputting the message body from standard input (stdin)
Python:
- Do not create temporary files for performance and to reduce disk wear.
Both:
- Improve the performance
- Support HTML formatted messages
- Provide an option to automatically upload large files to an external storage service, such as Send (provided by @timvisee, formerly Firefox Send) or transfer.sh
- Add tests
- Automatically renew the S/MIME certificate, as certbot does for Let's Encrypt certificates
Thanks to Daniel Connelly for helping create the Feature comparison and test the Bash script!