如何将动态数据传递到sendgrid webapp上设计的电子邮件模板? : - | Sendgrid

问题描述 投票:14回答:2

我在我的示例应用程序中集成了sendgrid电子邮件服务。并且还能够向用户发送电子邮件。

但是如何将动态数据传递给sendGrid webapp上的电子邮件模板设计?有没有办法将动态数据发送到sendgrid设计的电子邮件模板?

对于前我在sendgrid上设计了欢迎电子邮件模板。我希望将动态数据传递给此模板,即UserName,emailId,City等..使用java代码?当任何用户在我的应用程序中注册时,我想向该用户发送欢迎电子邮件。通过我们的数据库与UserName,emailId,City等用户的信息。

该怎么办 ?怎么做 ?

email email-integration html-email sendgrid
2个回答
12
投票

而不是通过电子邮件模板应用程序(这是不可能的)专注于替换,你应该看看SMTPAPI。通过在邮件中添加X-SMTPAPI标头,即可

  1. 控制SendGrid帐户的设置(即change filter settings on the fly
  2. 在单个SMTP事务中发送最多1000个收件人地址(SMTPAPI To: Array
  3. 在邮件正文中执行邮件合并替换(SMTPAPI Sub: Associative Array

最后,假设您通过我们的SMTP或Web API直接发送电子邮件。如果您正在使用我们的新闻通讯功能,则可以在新闻稿模板中使用Custom Tags进行邮件合并,例如替换。

- 乔

SendGrid


1
投票

我认为it is possible now,通过在选项中添加substitutions键。我还添加了to密钥,因为那是强制性的。

$request_body = json_decode('{
  "personalizations": [
    {
      "substitutions": {
        "-first_name-": "John",
        "-last_name-": "Doe"
      },
      "to": [
        {
          "email": "[email protected]",
          "name": "John Doe"
        }
      ]
    }
  }
}');
$response = $sg->client->mail()->send()->post($request_body);
echo $response->statusCode();
echo $response->body();
print_r($response->headers());

Sendgrid中的模板如下所示:

Dear -first_name- -last_name-,

Foo bar...

Kind Regards
© www.soinside.com 2019 - 2024. All rights reserved.