SendGrid unique arguments with individual emails

耗尽温柔 提交于 2019-12-01 18:11:52

I see that you're mentioning unique arguments first but then you're mentioning substitutions that are a completely unrelated matter. Unique arguments apply to the API call as a whole, for example those may contain a batch ID in your system so that you can more easily match the email events to your data. Substitutions, however, is basically string replacement in emails to personalize each email for its recipient and those apply to each recipient of the email instead of the API call.

Sendgrid unique arguments

You don't have to mess with the JSON request headers if you're using their API library, simply use the library as any other PHP object. For example, if you have to set three variables, var1, var2, var3 it's either this:

$Sendgrid -> setUniqueArguments(array(
    'var1' => 'value1',
    'var2' => 'value2',
    'var3' => 'value3'
));

or this:

$Sendgrid -> addUniqueArgument('var1', 'value1');
$Sendgrid -> addUniqueArgument('var2', 'value2');
$Sendgrid -> addUniqueArgument('var3', 'value3');

The difference being that the first option setUniqueArguments completely replaces any other variables you've added before but the second one, addUniqueArgument, adds a variable to the existing ones.

Sendgrid substitutions

Let's say you're using their API library and you have 2 recipients, bob@example.com and alice@example com, and you need to mention their name in the email. In this case you use a placeholder string in the body of the email, basically anything that wouldn't occur normally. In our case, let's assume it could be:

Hello <<Name>>

where <<Name>> is the placeholder for the recipients name. In this case you can construct the API call as such (I'm leaving out the parts related to email content, etc.):

$Sendgrid -> addTo('bob@example.com');
$Sendgrid -> addTo('alice@example.com');
$Sendgrid -> addSubstitution('<<Name>>', array('Bob', 'Alice'));

The values in the addSubstituion call must be in the same order as the recipient list.

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