我需要将HTML文件作为eamil的主体发送给几个客户。我们公司将为此使用SendGrid,我需要能够通过发送电子邮件。
到目前为止,我所做的方法适用于简单的html或纯文本:
curl -s --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header "Authorization: Bearer SECRET_API_KEY" \
--header 'Content-Type: application/json' \
--data '{"personalizations":[{"to":[{"email":"my1@email.com"},{"email":"my2@email.com"}]}],"from":{"email":"info@somewhere.com"},"subject":"Testing sending emails via SendgridAPI","content":[{"type":"text\/html","value":"Test API Email From ME"}]}'
现在这个很好用。问题是,当我想用一个相当大的、复杂的HTML文件的内容替换“来自ME的Test电子邮件”时。这里有所有常见的单调恶梦,比如“和”的混合以及到处都是新的行。我需要清理HTML,以便完成三件事:
。
我所做的就是创建实际的字符串命令,并使用脚本语言执行它。因此,在将html插入内容字段的value字段之前,我可以在html上执行我想要的任何操作。因此,我的问题是:我应该在html上执行哪些字符串操作,以便我可以使用这种方法发送电子邮件?
发布于 2022-02-02 12:25:19
使用jq
和bash
我会用静态数据来做,你可以改进它
定义JSON模板
IFS='' read -r -d '' json_template <<'EOF'
{
"personalizations": [
{
"to": [
{ "email": "my1@email.com" },
{ "email": "my2@email.com" }
]
}
],
"from": { "email": "info@somewhere.com" },
"subject": "Testing sending emails via SendgridAPI",
"content": [
{
"type": "text/html",
"value": "Test API Email From ME"
}
]
}
EOF
IFS='' read -r -d '' html_email <<'EOF'
<!doctype html>
<html>
<head>
title>Simple Email</title>
</head>
<body>
Test API Email From ME
</body
</html>
EOF
json_data=$(
jq -c -n \
--arg html "$html_email" \
--argjson template "$json_template" \
'$template | .content[0].value = $html'
)
curl -s --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header "Authorization: Bearer SECRET_API_KEY" \
--header 'Content-Type: application/json' \
--data "$json_data"
发布于 2022-02-02 12:32:58
下面是如何使用jq
组合适当的JSON数据有效负载,以便将其发送到API。
jq
将确保每个值、接收者、来自、主题和html主体在作为--data @-
提交给curl
之前分别编码到正确的JSON数据对象、数组和字符串中。
我在任何地方都添加了评论,所以很清楚每一步都做了些什么:
#!/usr/bin/env bash
recipients=(
'my1@email.com'
'my2@email.com'
)
from='info@somewhere.com'
subject='Testing sending emails via SendgridAPI'
# Streams null-delimited recipients array entries
printf '%s\0' "${recipients[@]}" |
# jq slurps the null-delimited recipients,
# read the raw html content into the jq $contentHTML variable
# and integrate it all as a proper JSON
jq --slurp --raw-input --rawfile contentHTML example.html \
--arg from "$from" \
--arg subject "$subject" \
'
# Fills the jq $recipient JSON array variable
# by splitting the null-delmited entries
# from the incoming stream
split( "\u0000") as $recipients |
{
"personalizations": [
{
# Uses the $recipients array that has been
# slurped from the input stream
"to": $recipients
}
],
"from": {
# Use the $from that has been passed as --arg
"email": $from
},
# Use the $subject that has been passed as --arg
"subject": $subject,
"content": [
{
"type": "text/html",
"value": $contentHTML
}
]
}
' |
# Get the resultant JSON piped into curl
# that will read the data from the standard input
# using --data @-
# rather than passing it as an argument, because
# the payload could exceed the maximum length of arguments
curl -s --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header "Authorization: Bearer SECRET_API_KEY" \
--header 'Content-Type: application/json' \
--data @-
https://stackoverflow.com/questions/70954855
复制相似问题