I want to send personalized letters using SendGrid. The whole body is similar, it's just 3-4 single-word permutations in each mail, so I thought about using SendGrid notes
- Bob ( bob@example.com ) should receive the email "Hello Bob, lorem ipsum".
- Alice ( alice@example.com ) should receive the email "Hi Alice, lorem ipsum"
An environment is installing CodeIgniter using the provided PHP class installed by the composer.
Calling the problem function addSubstitution ($ key, $ value) , which results in error 400 (Bad Request). When sending requests without this call, everything works as expected (including my placeholders, which, of course, were not replaced). I get clean 202, letters arrive. The error text provided by SendMail is that {"errors":[{"message":"Bad Request","field":null,"help":null}]}that helps a little.
I was thinking of adding an array of values to the replacement key. This is copied from this and this code (using the SMTP API in the first example, it is unclear what in the second), but it seems that valueof addSubstitutioncan only process strings.
To be clear: I need this functionality in this universal approach. My problem is not only with the recipient names in the greeting, but also with a personalized unsubscribe link, etc. I add this hint because an answer like “use the Sendgrid-Marketing API and download recipients before” does not meet my needs.
My PHP script (light version):
$sg = new \SendGrid('api_key');
$recipients = array(
array(
'email' => 'bob@example.com',
'name' => 'Bob'
),
array(
'email' => 'alice@example.com',
'name' => 'Alice'
)
);
$mail = new \SendGrid\Mail();
$from = new \SendGrid\Email('myname', 'myname@mycompany.com');
$mail->setFrom($from);
$mail->setSubject('New mail');
$content = new \SendGrid\Content('text/plain', 'Hi -name-, lorem ipsum');
$mail->addContent($content);
$personalization = new \SendGrid\Personalization();
$substitutions_name = array();
foreach ($recipients as $recipient) {
$email = new \SendGrid\Email(null, $recipient['email']);
$personalization->addTo($email);
array_push($substitutions_name, $recipient['name']);
}
$personalization->addSubstitution('-name-', $substitutions_name);
$mail->addPersonalization($personalization);
$response = $sg->client->mail()->send()->post($mail);
Is my approach wrong at all? Is there another similar feature in SendGrid that satisfies my needs?
Calling the SMTP-API, which seems to have the necessary functionality, is not an alternative, since I do not want the php mail () call to be executed in fast and long loops.
:. , . SO . 400? , .
: JSON PHP- Script
{
"from": {
"name": "myname",
"email": "myname@mycompany.com"
},
"personalizations": [
{
"to": [
{
"email": "bob@example.com"
},
{
"email": "alice@example.com"
}
],
"substitutions": {
"-name-": [
"Bob",
"Alice"
]
}
}
],
"subject": "New mail",
"content": [
{
"type": "text/plain",
"value": "Hi -name-, lorem ipsum"
}
]
}
: bwests answer, ():
[...]
$content = new \SendGrid\Content('text/plain', 'Hi -name-, lorem ipsum');
$mail->addContent($content);
foreach ($recipients as $recipient) {
$personalization = new \SendGrid\Personalization();
$email = new \SendGrid\Email(null, $recipient['email']);
$personalization->addTo($email);
$personalization->addSubstitution('-name-', $recipient['name']);
$mail->addPersonalization($personalization);
}
$response = $sg->client->mail()->send()->post($mail);