Curl you an email for great good
Today I learnt that Curl can also be used to send emails. One of my projects is to manage a fleet of embedded devices that communicate information back to our servers. Email is one part of this process. And,
curl is installed on these devices, greatly simplifying things.
# gmail specific curl --url 'smtps://smtp.gmail.com:587' --ssl-reqd \ --mail-from 'firstname.lastname@example.org' --mail-rcpt 'email@example.com' \ --user 'firstname.lastname@example.org:password' --insecure \ --upload-file fileToBeUploaded.txt
Let’s break down each component:
--url- specifies the SMTP server address and port
--ssl-reqd- mandates SSL/TLS must be used
--mail-from- who sent the email
--mail-rcpt- the addressee
--user- credentials for the mail server
curlidiom that tells
curlto ignore CA errors and continue
curlcannot send a body, but will accept an RFC 5322 compliant text file as its message. Json, HTML and text data can be sent by adding a
Content-Typeheader appropriate to the type of data being sent.
A simple example bash script to illustrate some of the potential that this curl functionality provides us. The code can be found here.
#!/bin/bash # A simple email script that takes a recipient and # RFC5322 compliant text file as the message body. echo "Curl auto-emailer" echo "SUPPORTS GMAIL ONLY" RCPT=$1 # 'email@example.com' FILE=$2 # takes a filename, relative or abs path FROMfirstname.lastname@example.org # 'email@example.com' EMAILfirstname.lastname@example.org:p@ssword # 'email@example.com:password' if [ -z "$1" ] then echo "Missing argument 1; recipient address\ eg firstname.lastname@example.org" exit elif [ -z "$2" ] then echo "Missing argument; File to be uploaded" exit else curl --url 'smtp://smtp.gmail.com:587' --ssl-reqd \ --mail-from $FROM --mail-rcpt $RCPT \ --user $EMAIL --upload-file $FILE --insecure fi
This is for demonstration purposes only. Also, its worth mentioning that entering credentials in the commandline without the appropriate safe guards can be a security issue!
curl wttr.in and
curl rates.sx in your terminal for some of the more interactive things that can be done with this great tool.
The possibilities of
curl are nearly endless and its used by many of us everyday.
Keep up to date with my stuff
Subscribe to get new posts and retrospectives