skip to Main Content

I am trying to send an HTML using CURL and SendGrid. I tried to install the SendGrid lib from GitHub and I could not figure it out to get the autoload.php correctly. I gave up on that and tried CURL. That worked for TEXT. I am able to send a TEXT email but not the HTML content from an include. I used to be able to do it before but they updated something in the API now it does not work. Can someone please help me find what line I need to allow the HTML in the CURL with my own custom templated added from an INCLUDE?

  $url = 'https://api.sendgrid.com/'; 
    $pass = "MY-API-KEY"; 
    $template_id = 'my-Temp-ID';
    $js = array(
      'sub' => array(':name' => array('Ed Vizenor')),
      'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))
    );
    echo json_encode($js);
    /// I don't want to include a template but my own here.
    $Content = file_get_contents('custom/WelcomeHTML.php');
    $params = array(
        'to'        => "[email protected]", 
        'toname'    => "First Last Name",
        'from'      => "[email protected]",
        'fromname'  => "From Name",
        'subject'   => " LIVE EVENT", 
        'text'      => "Hoping this one works",
        'html'      => $Content,
        'x-smtpapi' => json_encode($js),
      );
    $request =  $url.'api/mail.send.json';
    $session = curl_init($request);
    curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $pass));
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);
    print_r($response);

2

Answers


  1. First, Just to be sure echo $Content to be sure your content is loaded properly from custom/WelcomeHTML.php

    Second, since you are using your own HTML content, make sure that you don’t include the filters section in your JSON request. The filters section is used for SendGrid template settings.

    So remove this two lines from your code

    $template_id = 'my-Temp-ID';
    
    'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))
    

    So your code should look like this:

    $url = 'https://api.sendgrid.com/';
    $pass = "MY-API-KEY";
    $js = array(
        'sub' => array(':name' => array('Ed Vizenor'))
    );
    $Content = file_get_contents('custom/WelcomeHTML.php');
    
    $params = array(
        'to'        => "[email protected]",
        'toname'    => "First Last Name",
        'from'      => "[email protected]",
        'fromname'  => "From Name",
        'subject'   => "LIVE EVENT",
        'text'      => "Hoping this one works",
        'html'      => $Content,
        'x-smtpapi' => json_encode($js),
    );
    
    $request =  $url.'api/mail.send.json';
    $session = curl_init($request);
    curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $pass));
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);
    print_r($response);
    
    Login or Signup to reply.
  2. If you view the documentation at: https://docs.sendgrid.com/for-developers/sending-email/quickstart-php

    It provides an excellent step by step instructions for getting setup.

    With specific regard to the autoload, once you successfully run the composer require command, then you need to ensure your PHP file that you want to use for sendmail has the following include towards the top:

    require 'vendor/autoload.php'
    

    I would discourage you from simply writing your own cURL processor because it does not have the error handling that is provided in the official API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search