From 357eef8ec908e3a97940af15aa581c98bfa56043 Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Sat, 23 Dec 2023 20:14:10 -0700 Subject: [PATCH] New: emailing with curl notes --- notes/shell/email_with_curl.md | 100 +++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 notes/shell/email_with_curl.md diff --git a/notes/shell/email_with_curl.md b/notes/shell/email_with_curl.md new file mode 100644 index 0000000..becf968 --- /dev/null +++ b/notes/shell/email_with_curl.md @@ -0,0 +1,100 @@ +# Emailing with Curl + +`curl` is an amazing networking tool. It can even automate sending emails! +You'll need a app password or real password to login to your email account + +## Email just a file + +For the most basic email, we can stuff the headers into the email file itself. +Here's a basic example + +```bash +cat < +To: Emiliko Mirror +Subject: Sleep triggered on $(hostname) +Date: $(date) + +Dear me, + +The system went to sleep! I thought I should notify my gmail account, so here +I am. + +cheers, +Emiliko +EMAIL + +curl --silent --ssl-reqd \ + --url 'smtps://smtp.gmail.com:465' \ + --user 'emiliko@cs.ox.ac.uk:' \ + --mail-from 'emiliko@cs.ox.ac.uk' \ + --mail-rcpt 'emiliko@gmail.com' \ + --upload-file email.txt +``` + +The `From:` - `Date:` headers at the top are very important. Gmail seems to +insert them automatically if they're missing, though other mail providers +require them + +If you use 2fa, you'll need to [obtain an app +password](https://support.google.com/accounts/answer/185833?hl=en) instead + +## 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 + +```bash +declare -r attach_file=mirrors_house.png +declare -r mime_type="$(file --mime-type "$attach_file" | sed 's/.*: //')" + +cat <email.txt +Dear classmates, + +I just bought a new house! Check out the picture I attached below + +OMGOMGOMG, I'm so excited! + +cheers, +Kate Mirror +EMAIL + +curl --silent --ssl-reqd \ + --url 'smtps://smtp.fastmail.com:465' \ + --user 'kate%40mirror.house:' \ + --mail-from "kate@mirror.house" \ + --mail-rcpt 'emiliko@cs.ox.ac.uk' \ + --mail-rcpt 'lou@lou.me' \ + --mail-rcpt 'louis@louis.me' \ + -F '=(;type=multipart/mixed' \ + -F "=$(cat email.txt);type=text/plain" \ + -F "file=@${attach_file};filename=home.png;type=${mime_type};encoder=base64" \ + -F '=)' \ + -H "From: Kate Mirror " \ + -H "Subject: Check out my new place!" \ + -H "To: Emiliko Mirror " \ + -H "CC: Lou Mirror " \ + -H "Date: $(date)" +``` + +Notice that for `BCC` the user isn't mentioned in `-H` at all, though still gets +a `--mail-rcpt` + +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 + +## Further reading + + - [A basic official guide](https://everything.curl.dev/usingcurl/smtp) + - [Sending a simple email with + curl](https://stackoverflow.com/questions/14722556/using-curl-to-send-email) + - [An example on using the -F option with + curl](https://blog.ambor.com/2021/08/using-curl-to-send-e-mail-with.html) + - [Backup of the above on + github](https://github.com/ambanmba/send-ses/blob/main/send-ses.sh) + - [A hit at using the filename field in + forums](https://stackoverflow.com/questions/15127732/change-name-of-upload-file-in-curl)