Send Dynamic PHP HTML With Sendgrid Web API

Go To StackoverFlow.com

2

I'd like to use Sendgrid WebAPI preferably without SMTP or Swiftmailer using the code below. Is it possibly to pass an entire dynamic webpage to the 'html' $params array without creating a long string variable and needing to escape every quote and echo each variable? Each email varies significantly so Sendgrid's template/mailmerge options will not work for me. Thanks!

Here's a simple html example (mine has much more dynamic content):

<html>
  <head></head>
  <body>
    <p>Hi I'm <?php echo $name; ?>!<br>
       <span style="color: #999999; font-size: 11px;">How are you?</span><br>
    </p>
  </body>
</html>

$url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD'; 

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'example@sendgrid.com',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);
2012-04-05 22:59
by Jeff
If you don't mind my asking, what's wrong with using SMTP - Borealid 2012-04-05 23:02
Nothing really wrong with it... it's just that the web api method seems so simple without needing any additional libraries - Jeff 2012-04-05 23:14


1

The best way to generate the HTML you need would be to use a template engine like Smarty. So in your example, somewhere above actually sending the email, you would do something like:

include('Smarty.class.php');

$smarty = new Smarty;
$smarty->assign('name', 'Swift');
$smarty->assign('name', 'SendGrid');
$smarty->assign('address', '123 Broadway');

// Store it in a variable
$emailBody = $smarty->fetch('some_dynamic_template.tpl');

And then when you actually need to send the email with the new dynamic HTML body:

....

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => $emailBody,
    'from'      => 'example@sendgrid.com',
);

....
2012-04-05 23:23
by Swift
Thanks Mike. I've never used Smarty before. Could I do something similar by creating a second PHP file and using filegetcontents() to retrieve it into the $emailBody variable - Jeff 2012-04-05 23:32
Jeff, its a little trickier than that because you need to replace the dynamic contents. However, here's a great article about making a simple templating system using file_get_contents http://www.broculos.net/en/article/how-make-simple-html-template-engine-ph - Swift 2012-04-06 00:49


0

In the same case SMTP API is easier by using substitution tags, more details : http://sendgrid.com/docs/API_Reference/SMTP_API/substitution_tags.html

2013-10-07 14:24
by Abdelali AHBIB
Ads