How to pass dynamic data to email template desgined on sendgrid webapp ? :-| Sendgrid

旧巷老猫 提交于 2019-11-30 12:25:27

问题


I integrated sendgrid email service with my sample application. & also able to send email to user.

But how to pass dynamic data to Email template design on sendGrid webapp? is there any way to send dynamic data to email template designed on sendgrid?

for ex. I designed Welcome email template on sendgrid. & I want to pass dynamic data to this template i.e UserName , emailId, City etc.. using java code ? when any user registered with my application then I want to send welcome email to that user. with information of user like UserName , emailId, City etc.. through our database.

What to do ? How to do ?


回答1:


Instead of focusing on substitution via the Email Template application (which is not possible), You should take a look at the SMTPAPI. By adding a X-SMTPAPI header in your message, you can

  1. Control the settings of your SendGrid account (i.e. change filter settings on the fly)
  2. Send to up to 1000 recipient addresses in a single SMTP transaction (SMTPAPI To: Array)
  3. Perform mail-merge like substitutions in the mail body (SMTPAPI Sub: Associative Array)

Finally this is assuming you are sending email directly via our SMTP or Web API. If you are using our newsletter feature, mail-merge like substitution is possible by using Custom Tags in the newsletter template.

-- Joe

SendGrid




回答2:


I think it is possible now, by adding a substitutions key in the options. I also added the to key because that one is mandatory.

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

Template in Sendgrid looks like this:

Dear -first_name- -last_name-,

Foo bar...

Kind Regards


来源:https://stackoverflow.com/questions/4014393/how-to-pass-dynamic-data-to-email-template-desgined-on-sendgrid-webapp-sen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!