Skip to content

Error in example: Send Multiple Emails with Personalizations #1112

@muniodiego

Description

@muniodiego

I have used the following example: https://github.com/sendgrid/sendgrid-php/blob/main/USE_CASES.md#send-multiple-emails-with-personalizations

<?php
// Uncomment next line if you're not using a dependency loader (such as Composer)
// require_once '<PATH TO>/sendgrid-php.php';

use SendGrid\Mail\From;
use SendGrid\Mail\Mail;
use SendGrid\Mail\Personalization;
use SendGrid\Mail\Subject;
use SendGrid\Mail\To;

$from = new From("[email protected]", "Twilio Sendgrid");
$to = new To(
    "[email protected]",
    "Example User1",
    [
        '-name-' => 'Example User 1'
    ],
    "Example User1 -name-"
 );
$subject = new Subject("Hello from Twilio Sendgrid!");
$plainTextContent = new PlainTextContent(
    "How's it going -name-?"
);
$htmlContent = new HtmlContent(
    "<strong>How's it going -name-?</strong>"
);
$email = new Mail($from, $to, $subject, $plainTextContent, $htmlContent);

$personalization0 = new Personalization();
$personalization0->addTo(new To(
        "[email protected]",
        "Example User2",
        [
            '-name-' => 'Example User 2'
        ],
        "Example User2 -name-"
));
$personalization0->addTo(new To(
        "[email protected]",
        "Example User3",
        [
            '-name-' => 'Example User 3'
        ],
        "Example User3 -name-"
));
$personalization0->setSubject(new Subject("Hello from Twilio Sendgrid!"));
$email->addPersonalization($personalization0);

$personalization1 = new Personalization();
$personalization1->addTo(new To(
        "[email protected]",
        "Example User4",
        [
            '-name-' => 'Example User 4'
        ],
        "Example User4 -name-"
));
$personalization1->addTo(new To(
        "[email protected]",
        "Example User5",
        [
            '-name-' => 'Example User 5'
        ],
        "Example User5 -name-"
));
$personalization1->addFrom(new From(
    "[email protected]" => "Twilio"
))
$personalization1->setSubject(new Subject("Hello from Twilio!"));
$mail->addPersonalization($personalization1);

$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

The emails are sent, but for each Personalizations ([email protected], [email protected], ..., [email protected]) the "-name-" tokens are not replaced neither in the content nor in the subject. The token is replaced correctly for the email to "[email protected]".

In conclusion, token replacement does not work in Personalizations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions