phpcurlsendgridphp-curl

SendGrid send HTML Email using CURL and INCLUDE()


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('Mike Jakiner')),
      '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@gmail.com", 
        'toname'    => "First Last Name",
        'from'      => "From@gmail.com",
        '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);

Solution

  • 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@gmail.com",
        'toname'    => "First Last Name",
        'from'      => "From@gmail.com",
        '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);