Skip to content

πŸ“§ Send e-mail (and text messages) from the command line

License

Notifications You must be signed in to change notification settings

tdulcet/Send-Msg-CLI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Actions Status

Send Msg CLI

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.

Usage

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.

  1. Verify that the required commands above are installed.
  2. Download the script (sendmsg.sh). Run: wget https://raw.github.com/tdulcet/Send-Msg-CLI/master/sendmsg.sh.
  3. 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.
  4. 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.
  5. If you want the script to be available for all users, install it. Run: sudo cp sendmsg.sh /usr/local/bin/sendmsg and sudo chmod +x /usr/local/bin/sendmsg.

Examples

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.

Gmail

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.

Feature comparison

This Send E-mail Script S-nail (formerly Heirloom mailx) Mutt SSMTP SendEmail eMail 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

Help

$ 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]>"

Scripts where this is incorporated

Contributing

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!

About

πŸ“§ Send e-mail (and text messages) from the command line

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published