-
-
Save velizarn/0c5c8bf19caaa316b2c0f4a0057178d0 to your computer and use it in GitHub Desktop.
#!/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 |
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/28c7e2601a6e7949d4eceb96fe9bc9f7Hey,
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
Anyone found a quick way to add CC or additional To recipients?
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
Did you found how to add a CSV attachment?
https://gist.github.com/amithgc/fe5f1e4821e4a9809117513eb92a362a
Anyone found a quick way to add CC or additional To recipients?
https://gist.github.com/amithgc/fe5f1e4821e4a9809117513eb92a362a
What about files?
This doesn't work:
{"errors":[{"message":"Bad Request","field":null,"help":null}]}
@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.
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}'"}]}'
yuck.
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