SendGrid 使用 CURL 和 INCLUDE() 发送 HTML 电子邮件

问题描述 投票:0回答:1

我正在尝试使用 CURL 和 SendGrid 发送 HTML。我尝试从 GitHub 安装 SendGrid lib,但无法正确获取 autoload.php。我放弃了,尝试了 CURL。这对文本有效。我可以发送文本电子邮件,但不能发送包含的 HTML 内容。我以前可以这样做,但他们更新了 API 中的一些内容,现在它不起作用了。有人可以帮我找到需要哪一行来允许 CURL 中的 HTML 包含从 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);
php curl sendgrid php-curl
1个回答
0
投票

首先,请确保 echo $Content 以确保您的内容已从 custom/WelcomeHTML.php

正确加载

其次,由于您使用的是自己的 HTML 内容,请确保您的 JSON 请求中不包含 filters 部分。 filters 部分用于 SendGrid 模板设置。

因此从代码中删除这两行

$template_id = 'my-Temp-ID';

'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))

所以你的代码应该是这样的:

$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);
© www.soinside.com 2019 - 2024. All rights reserved.