Sendgrid-php: Sendgrid with template

Created on 19 Apr 2017  路  3Comments  路  Source: sendgrid/sendgrid-php

My code works (email is sent) without the line with setTemplateId() method, whenever I try to use a template, email is not sent.
I follow example from https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#transactional_templates with both cases: "With Mail Helper Class", "Without Mail Helper Class".
$from = new SendGrid\Email(null, "[email protected]");
$subject = "I'm replacing the subject tag";
$to = new SendGrid\Email(null, "[email protected]");
$content = new SendGrid\Content("text/html", "I'm replacing the body tag");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$mail->personalization[0]->addSubstitution("-name-", "Example User");
$mail->personalization[0]->addSubstitution("-city-", "Denver");
$mail->setTemplateId("myTemplateId...");

    $apiKey = 'myApiKey';
    $sg = new \SendGrid($apiKey);

    try {
        $response = $sg->client->mail()->send()->post($mail);
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
help wanted question

Most helpful comment

Hi, thanks.
It turns out that we setup an account which expire the trial period, so that email with template will not be sent. I tried with another newly-created sendgrid account and email with template can be sent.

All 3 comments

Hello @moontrv,

Are you getting any exception message? If so, what is it?

With Best Regards,

Elmer

Hi, thanks.
It turns out that we setup an account which expire the trial period, so that email with template will not be sent. I tried with another newly-created sendgrid account and email with template can be sent.

Thanks for following up @moontrv!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamanupammaity picture iamanupammaity  路  4Comments

atsareva picture atsareva  路  4Comments

solonifer picture solonifer  路  3Comments

bjornmann picture bjornmann  路  3Comments

billizzard picture billizzard  路  5Comments