首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过SendGrid API从命令行发送复杂的html作为电子邮件正文。

通过SendGrid API从命令行发送复杂的html作为电子邮件正文。
EN

Stack Overflow用户
提问于 2022-02-02 11:32:03
回答 2查看 987关注 0票数 0

我需要将HTML文件作为eamil的主体发送给几个客户。我们公司将为此使用SendGrid,我需要能够通过发送电子邮件。

到目前为止,我所做的方法适用于简单的html或纯文本:

代码语言:javascript
运行
复制
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,以便完成三件事:

  1. 最终结果需要一个有效的命令行字符串

  1. --数据交换参数需要保持有效的JSON包含的字符串

  1. HTML不应该中断。

我所做的就是创建实际的字符串命令,并使用脚本语言执行它。因此,在将html插入内容字段的value字段之前,我可以在html上执行我想要的任何操作。因此,我的问题是:我应该在html上执行哪些字符串操作,以便我可以使用这种方法发送电子邮件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-02 12:25:19

使用jq和bash

我会用静态数据来做,你可以改进它

  • 为API:

定义JSON模板

代码语言:javascript
运行
复制
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

  • 定义HTML内容:

代码语言:javascript
运行
复制
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中的电子邮件内容替换为HTML

代码语言:javascript
运行
复制
json_data=$(
    jq -c -n \
        --arg html "$html_email" \
        --argjson template "$json_template" \
        '$template | .content[0].value = $html'
)

  • 发送查询

代码语言:javascript
运行
复制
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"
票数 2
EN

Stack Overflow用户

发布于 2022-02-02 12:32:58

下面是如何使用jq组合适当的JSON数据有效负载,以便将其发送到API。

jq将确保每个值、接收者、来自、主题和html主体在作为--data @-提交给curl之前分别编码到正确的JSON数据对象、数组和字符串中。

我在任何地方都添加了评论,所以很清楚每一步都做了些什么:

代码语言:javascript
运行
复制
#!/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 @-
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70954855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档