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 'email@example.com' --mail-rcpt 'firstname.lastname@example.org' \ --user 'email@example.com: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 # 'firstname.lastname@example.org' FILE=$2 # takes a filename, relative or abs path FROMemail@example.com # 'firstname.lastname@example.org' EMAILemail@example.com:p@ssword # 'firstname.lastname@example.org:password' if [ -z "$1" ] then echo "Missing argument 1; recipient address\ eg email@example.com" 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.