Skip to content

Instantly share code, notes, and snippets.

@velizarn
Last active March 12, 2024 13:13
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save velizarn/0c5c8bf19caaa316b2c0f4a0057178d0 to your computer and use it in GitHub Desktop.
Save velizarn/0c5c8bf19caaa316b2c0f4a0057178d0 to your computer and use it in GitHub Desktop.
Send email from bash script by using SendGrid API
#!/bin/bash
SENDGRID_API_KEY=""
EMAIL_TO=""
FROM_EMAIL=""
FROM_NAME=""
SUBJECT=""
bodyHTML="<p>Email body goes here</p>"
maildata='{"personalizations": [{"to": [{"email": "'${EMAIL_TO}'"}]}],"from": {"email": "'${FROM_EMAIL}'",
"name": "'${FROM_NAME}'"},"subject": "'${SUBJECT}'","content": [{"type": "text/html", "value": "'${bodyHTML}'"}]}'
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer '$SENDGRID_API_KEY \
--header 'Content-Type: application/json' \
--data "'$maildata'"
# Voila! Sending emails from bash script is pretty simple
#
# https://sendgrid.com/docs/API_Reference/Web_API/mail.html
# http://stackoverflow.com/questions/17029902/using-curl-post-with-variables-defined-in-bash-script-functions
# http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
# http://stackoverflow.com/questions/25217462/concatenate-output-of-sed-to-bash-script-variable
# http://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script
# http://stackoverflow.com/questions/22043088/how-to-get-yesterday-and-day-before-yesterday-in-linux
# http://unix.stackexchange.com/questions/157747/force-rsyslogd-to-create-new-file-before-somebody-will-write-to-it
@kirAn-sAi
Copy link

how to add cc and bcc in this code

@laurentngu
Copy link

cc does not seem to work:
maildata='{"personalizations": [{"to": [{"email": "'${EMAIL_TO}'"},{"email": "'${EMAIL_TO2}'"}]}],"from": {"email": "'${FROM_EMAIL}'", "cc": [{"email": "'${CC}'"}],"name": "'${FROM_NAME}'"},"subject": "'${SUBJECT}'","content": [{"type": "text/html", "value": "'${bodyHTML}'"}]}'

@dindibo
Copy link

dindibo commented Mar 30, 2020

Hey, do you know how can I add attachments?

@Peitemoukiemena
Copy link

Thank you for this!! Very helpful!

@ahmad-mushtaq
Copy link

thank you. its very handy

@broland07
Copy link

Hi!
An y example, how can i attach a file?

@GDanielM
Copy link

Hi!
An y example, how can i attach a file?

Did you found how to add a CSV attachment?

@broland07
Copy link

broland07 commented Jan 24, 2021

Hi!
An y example, how can i attach a file?

Did you found how to add a CSV attachment?

Hi!

No, nothing. I solved this problem with python because i didn't find any solution with bash.

https://gist.github.com/broland07/28c7e2601a6e7949d4eceb96fe9bc9f7

@GDanielM
Copy link

Hi!
An y example, how can i attach a file?

Did you found how to add a CSV attachment?

Hi!

No, nothing. I solved this problem with python because i didn't find any solution with bash.

https://gist.github.com/broland07/28c7e2601a6e7949d4eceb96fe9bc9f7

Hey,
In bash, we have to convert the file into base64 encoded format and store in a variable and then pass that variable in the attachment key, so that it accepts the file and sends out an email with attachment

@broland07
Copy link

broland07 commented Jan 25, 2021

Hi!
An y example, how can i attach a file?

Did you found how to add a CSV attachment?

Hi!
No, nothing. I solved this problem with python because i didn't find any solution with bash.
https://gist.github.com/broland07/28c7e2601a6e7949d4eceb96fe9bc9f7

Hey,
In bash, we have to convert the file into base64 encoded format and store in a variable and then pass that variable in the attachment key, so that it accepts the file and sends out an email with attachment

Yes, today i tried to find after your answer and i finded a simple example.
https://gist.github.com/broland07/acd1ed48821335d4de81fa1daf047543

@0xtf
Copy link

0xtf commented Feb 19, 2021

Anyone found a quick way to add CC or additional To recipients?

@amithgc
Copy link

amithgc commented Mar 23, 2021

I wrote a shell script to send emails using SendGrid API, Please feel free to use it

Options:

        -t: To Emails, Separated by ";"
 	-c: CC Emails, Separated by ";"
 	-b: BCC Emails, Separated by ";"
 	-s: Subject
 	-o: Email body
 	-a: Attachment Files, Separated by ";"

https://gist.github.com/amithgc/fe5f1e4821e4a9809117513eb92a362a

@amithgc
Copy link

amithgc commented Mar 23, 2021

Did you found how to add a CSV attachment?

https://gist.github.com/amithgc/fe5f1e4821e4a9809117513eb92a362a

@amithgc
Copy link

amithgc commented Mar 23, 2021

Anyone found a quick way to add CC or additional To recipients?

https://gist.github.com/amithgc/fe5f1e4821e4a9809117513eb92a362a

@adrianaioanei94
Copy link

What about files?

@TitanRob16
Copy link

This doesn't work:

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

@pothi
Copy link

pothi commented Dec 13, 2022

@TitanRob16 Had the same issue. For me, I had to remove the single quote in the line --data "'$maildata'". I am on bash 5.0.x, btw.

@rad4alex
Copy link

Hey guys. How we can put value from bash variable as a body letter template. Can somebody help with it

Something like this :
bodyHTML=echo -e "$LETTER"

....

maildata='{"personalizations": [{"to": [{"email": "'${EMAIL_TO}'"}]}],"from": {"email": "'${FROM_EMAIL}'",
"name": "'${FROM_NAME}'"},"subject": "'${SUBJECT}'","content": [{"type": "text/html", "value": "'${bodyHTML}'"}]}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment