curl is an amazing networking tool. It can even automate sending emails!
Email just a file
For most basic emails, we can stuff the headers into the email content file
itself. Here’s a basic example:
The From: - Date: headers at the top are very important. Gmail seems to
insert them automatically if they’re missing, though other mail providers might
not, so make sure to leave them in.
If you use 2fa, you won’t be able to use your account password to authenticate.
Instead, you’ll need to obtain an app
password for your
account.
Email with attachments
We can also CC, BCC and attach files with our curl emails.
Attachments need a MIME type for the file, which is easiest obtained with the
file command. In this example, we also remove the headers from the email file,
opting to use the more explicit -H curl option.
Notice that for BCC the user isn’t mentioned in -H at all, though you’ll
still need a --mail-rcpt line for them.
Oddly, some mail servers seem sensitive to the filename field. My university
server would consistently bounce emails when I tried to have a filename field.
Consider removing it to use the file’s actual name. In the example above, we’d
use mirrors_house.png instead of home.png.