繁体   English   中英

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

[英]Sending a complex html as the body of email from the command line via an SendGrid API

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

到目前为止,我这样做的方式适用于简单的 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"}]}'
使用jq<\/code>和 bash<\/h5>

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

  • 为 API 定义一个 JSON 模板:<\/li><\/ul>
    定义 HTML 内容:<\/li><\/ul>
     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<\/code><\/pre>
                       
                 
    • 将 JSON 中的电子邮件内容替换为 HTML<\/li><\/ul>
       json_data=$( jq -c -n \\ --arg html "$html_email" \\ --argjson template "$json_template" \\ '$template | .content[0].value = $html' )<\/code><\/pre>
                               
                       
      • 发送查询<\/li><\/ul>
        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"<\/code><\/pre>"

以下是如何使用jq<\/code>组合正确的 JSON 数据有效负载,以便将其发送到 API。

jq<\/code>将确保每个值、收件人、发件人、主题和 html 正文在作为--data @-<\/code>提交到curl<\/code>之前分别编码为正确的 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 @-

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM