Sending Emails with cURL

Curl is a command line utility found on most modern Linux distributions used to retrieve content from  the internet. However, it can do a lot more than that and supports a plethora of protocols, including TFTP, SFTP, and SMTP.

Sending an Email

[rohan@desktop ~]$ echo "Subject: Sending an Email with Curl!

Just wanted to check you were able to receive this email, sent over the curl command.
Has it gone into spam?.

" | curl -s --ssl smtp://$SERVER --mail-from $FROM --mail-rcpt $TO --upload-file /dev/stdin --user
Enter host password for user '':
[rohan@desktop ~]$

Let's see if the email was delivered (nb: the address has been changed to prevent spam)

Email sent with Curl

Securing the credentials

In this example, the --user field did not specify a password, this results in a prompt. If you don't want to enter it manually, you can either hardcode it somewhere (bad) or use GPG encryption. With GPG, the gpg-agent can be configured to store credentials in memory. This way, you only need to enter the password once. Configuring gpg is out of the scope of this article

[rohan@desktop ~]$ curl -s --ssl smtp://$SERVER --mail-from $FROM --mail-rcpt $TO --upload-file $EMAIL --user$(pass email/ 2>/dev/null)
[rohan@desktop ~]$