【问题标题】:SendGrid: applying templates to WEB API pup curl methodSendGrid:将模板应用于 WEB API pup curl 方法
【发布时间】:2015-10-23 17:24:40
【问题描述】:

我正在使用 Sendgrid 自己提供的这段代码

<?php
// use actual sendgrid username and password in this section
$url = 'https://api.sendgrid.com/'; 
$user = 'username'; // place SG username here
$pass = 'password'; // place SG password here
// grabs HTML form's post data; if you customize the form.html parameters then you will need to reference their new new names here
$name = $_POST['name']; 
$email = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message'];
// note the above parameters now referenced in the 'subject', 'html', and 'text' sections
// make the to email be your own address or where ever you would like the contact form info sent
$params = array(
    'api_user'  => "$user",
    'api_key'   => "$pass",
    'to'        => "xxxxxx@gmail.com", // set TO address to have the contact form's email content sent to
    'subject'   => "Contact Form Submission", // Either give a subject for each submission, or set to $subject
    'html'      => "<html><head><title> Contact Form</title><body>
    Name: $name\n<br>
    Email: $email\n<br>
    Subject: $subject\n<br>
    Message: $message <body></title></head></html>", // Set HTML here.  Will still need to make sure to reference post data names
    'text'      => "
    Name: $name\n
    Email: $email\n
    Subject: $subject\n
    $message",
    'from'      => "contact@xxxxxx.com", // set from address here, it can really be anything
  );
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$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);
// Redirect to thank you page upon successfull completion, will want to build one if you don't alreday have one available
header('Location: thanks.html'); // feel free to use whatever title you wish for thank you landing page, but will need to reference that file name in place of the present 'thanks.html'
exit();
// print everything out
print_r($response);
?>

但我不明白如何应用我在 Sendgrid 中创建的模板,我也不明白如何发送 HTML 电子邮件而不是简单文本...

【问题讨论】:

    标签: php api curl sendgrid


    【解决方案1】:
    <?php
    require 'vendor/autoload.php';
    Dotenv::load(__DIR__);
    $sendgrid_apikey = getenv('SG_KEY');
    $sendgrid = new SendGrid($sendgrid_apikey);
    $url = 'https://api.sendgrid.com/'; 
    $pass = $sendgrid_apikey; 
    $template_id = '<your_template_id>';
    $js = array(
      'sub' => array(':name' => array('Elmer')),
      'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))
    );
    echo json_encode($js);
    $params = array(
        'to'        => "elmer.thomas@sendgrid.com", 
        'toname'    => "Elmer Thomas",
        'from'      => "dx@sendrid.com",
        'fromname'  => "DX Team",
        'subject'   => "PHP Test", 
        'text'      => "I'm text!",
        'html'      => "<strong>I'm HTML!</strong>",
        '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 ' . $sendgrid_apikey));
    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);
    

    【讨论】:

    • 谢谢您.. 现在清楚... 最后一个问题:如何添加发件人姓名?
    • 我已更新示例以包含发件人的姓名。您可以在这里找到所有参数:sendgrid.com/docs/API_Reference/Web_API/mail.html
    • 我可以知道如何添加模板的合并字段吗?
    • @IshaS,建议您联系support.sendgrid.com。他们很乐意提供帮助。
    • 感谢@ElmerThomas
    【解决方案2】:

    首先,我假设您已经在这里创建了模板:https://sendgrid.com/templates,为了这个示例,模板如下所示:

    Hello :name,
    
    <%body%>
    
    With Best Regards,
    
    Your Library Tester
    

    然后,使用the official PHP SendGrid library 和可选的phpdotenv 加载您的凭据:

    <?php      
    Dotenv::load(__DIR__);
    $sendgrid_apikey = getenv('SG_KEY');
    $sendgrid = new SendGrid($sendgrid_apikey);
    
    $email = new SendGrid\Email();
    
    $templateId = '<your_template_id>';
    $name = array('Elmer');
    
    $email
        ->addTo('elmer.thomas@sendgrid.com')
        ->setFrom('dx@sendgrid.com')
        ->setSubject('Testing the PHP Library')
        ->setText('I\'m text!')
        ->setHtml('<strong>I\'m HTML!</strong>')
        ->addFilter('templates', 'enabled', 1)
        ->addFilter('templates', 'template_id', $templateId)
        ->addSubstitution(":name", $name)
    ;
    
    $sendgrid->send($email);
    

    【讨论】:

    • 我不想让我们去图书馆。它很旧并且与 Gruzzle 有依赖关系
    • 在这种情况下,您可以使用 SMTP API:sendgrid.com/docs/API_Reference/SMTP_API/index.html 直接使用 curl_setopt:php.net/manual/en/function.curl-setopt.php 添加过滤器和替换数据。
    • 关于图书馆,它最近在本月更新,并将继续更新,因为我们正在改进我们所有的图书馆。我们还没有从 Guzzle 中移除依赖项,因为它目前运行良好,而且我们的数据显示我们的 PHP 库是我们最常用的库之一(在前 3 名中)。您有任何 Guzzle 更换建议吗?感谢您的反馈,我们非常感谢!
    • 知道了...我会在下一轮试一试,而不是 curl 选项,它速度很快,而且可以满足我的简单需求,谢谢!
    • 这个链接是关于 JSON 例子的……你有 php 的例子吗?因为我还是不明白...为什么没有人用 php 中的完整示例扩展该示例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多