5

I have integrated sendgrid in Laravel and I managed to send the email template of sendgrid in emails but I am not able to replace the content in the email templates. I am using Sendgrid Web API V3.

I followed the steps given in the below link but it is not replacing the variables in template with my dynamic data.

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

Here is code

$sg = new \SendGrid('API_KEY');           
$request_body = json_decode('{
            "personalizations":[
               {
                  "to":[
                     {
                        "email":"example@example.com"
                     }
                  ],
                  "subject":"Hello World from the SendGrid PHP Library!"

               }
            ],
            "from":{
               "email":"from@example.com"
            },
            "content":[
               {
                  "type":"text/html",
                  "value":"<html><body> -name- </body></html>"
               }
            ],
            "sub": {
                "-name-": ["Alice"]
              },
            "template_id":"xxxxxx-xxx-xxxxxxxx"

        }');

$mailresponse = $sg->client->mail()->send()->post($request_body);
echo $mailresponse->statusCode();
echo $mailresponse->body();
echo $mailresponse->headers();

Please help.

Community
  • 1
  • 1
Karan
  • 2,102
  • 2
  • 22
  • 31

4 Answers4

4

This works perfectly and is much simpler than the solutions already posted:

$email = new \SendGrid\Mail\Mail();
$email->setFrom( "from@example.com", "Some guy" );
$email->addTo( "to@example.com", "Another guy" );
$email->setTemplateId(
    new \SendGrid\Mail\TemplateId( TEMPLATE_ID )
);

// === Here comes the dynamic template data! ===
$email->addDynamicTemplateDatas( [
    'variable1'     => 'Some stuff',
    'templatesRock' => 'They sure do!'
] );

$sendgrid = new \SendGrid( API_KEY );
$response = $sendgrid->send( $email );
Alan Grainger
  • 551
  • 5
  • 8
  • What version are you using? $mail->addDynamicTemplateDatas does not exist on the v7 branch. – Andrioid Feb 05 '19 at 09:11
  • I was using ^7.0 as of one week ago, so it should be there! However I've now moved to Postmark and my life is infinitely better :P – Alan Grainger Feb 07 '19 at 07:30
  • I ended up calling their v3 API directly from the http client. The dynamic-template-args version is still unreleased in packagist (afaik). Seriously painful and their customer-service wasn't very helpful either. I'd expect this from a free service. Not something we pay for every month. – Andrioid Feb 11 '19 at 08:11
2

I have overcome this issue by using another way. Below is code that is working fine. May be help some one..

//create mail object
 $mail = new \SendGrid\Mail();
//set from 
 $from = new \SendGrid\Email("SENDER NAME", "SENDER EMAIL");
 $mail->setFrom($from);
//set personalization
 $personalization = new \SendGrid\Personalization();
 $to = new \SendGrid\Email("RECEIVER NAME", "RECEIVER EMAIL");
 $personalization->addTo($to);
 $personalization->setSubject("SUBJECT");
//add substitutions (Dynamic value to be change in template)
 $personalization->addSubstitution(':name', "Any"); 

 $mail->addPersonalization($personalization);
 $mail->setTemplateId("TEMPLATE_ID");
//send email
 $sg = new \SendGrid("API_KEY");

 $response = $sg->client->mail()->send()->post($mail);
Karan
  • 2,102
  • 2
  • 22
  • 31
0

It's a little bit late but maybe this can help some one to. I faced the same problem and @bwest answer helped me to solve it:

The substitutions values cannot be an array and the should look like:

`"personalizations":[{
       "to":[{
             "email":"example@example.com"
          }],
       "subject":"Hello World from the SendGrid PHP Library!",
       "substitutions": {
                "-name-": "Alice"
       }    
    }
    ],
    ...
Community
  • 1
  • 1
uvo
  • 3
  • 3
-1

IN JAVA use:

    Personalization personalization = new Personalization();

and its methods for sending the dynamic data to sendgrid template and use {{}} for receiving the data on sendgrid.