Skip to content

Instantly share code, notes, and snippets.

@kosmala007
Created January 20, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosmala007/7d901632ea36daf000f8e311cab0f869 to your computer and use it in GitHub Desktop.
Save kosmala007/7d901632ea36daf000f8e311cab0f869 to your computer and use it in GitHub Desktop.
Send email from bash by curl (UTF-8)
#!/bin/bash
# send email to one recipient (content in var)
# $host - eg. smtp.devpack.pl
# $port - 587
# $user - admin@devpack.pl
# $pass - password
# $recipient - recipient
# $content - message content must hace headers
# $subject - message subject
content=$'From: <'$user$'l> \nTo: <'$recipients$'> \nContent-Type: text/plain;charset=utf-8 \nSubject: '$subject$'\n\n'
curl --connect-timeout 15 -v --insecure "smtp://$host:$port" \
-u "$user:$pass" \
--mail-from "$user" \
--mail-rcpt "$recipient" \
-T - \
--ssl \
<<< "$content"
# send email to multiple recipients (content in var)
# $recipients - email addreses divided by coma eg: 'eamil1@fancybox.pl,eamil2@fancybox.pl'
content=$'From: <'$user$'l> \nTo: <'$recipients$'> \nContent-Type: text/plain;charset=utf-8 \nSubject: '$subject$'\n\n'
curl --connect-timeout 15 -v --insecure "smtp://$host:$port" \
-u "$user:$pass" \
--mail-from "$user" \
$(echo "${recipients}" | tr ', ' '\n' | xargs -r -n1 printf '--mail-rcpt %s ') \
-T - \
--ssl \
<<< "$content"
# send email (content in file)
# $pathToFile - path to file
curl --connect-timeout 15 -v --insecure "smtp://$host:$port" \
-u "$user:$pass" \
--mail-from "$user" \
--mail-rcpt "$recipient" \
-T "$pathToFile" \
--ssl \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment