本文介绍了SendGrid:应用模板到WEB API小狗卷曲的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我利用Sendgrid本身提供的这个code

 < PHP
//使用本节中的实际sendgrid用户名和密码
$ URL ='https://api.sendgrid.com/';
$ USER ='用户名'; //地方SG的用户名在这里
$通过='密码'; //地方SG这里密码
//抓住HTML表单的POST数据;如果您自定义的参数form.html,那么你将需要在这里引用他们的新新名称
$名称= $ _ POST ['名'];
$电子邮件= $ _ POST [电子邮件];
$主题= $ _ POST ['主题'];
$消息= $ _ POST ['消息'];
//注意现在的主体,HTML中引用的上述参数,以及文本节
//使电子邮件将自己的地址或在任何你想发送的联系方式信息
$ PARAMS =阵列(
    api_user'=> $ USER
    API_KEY'=> $通行证,
    '到'=> [email protected],//设置为解决有接触形式的电子邮件内容发送到
    '主题'=> 联系表提交,//要么给每个提交一个主题,或者设置为$主题
    'HTML'=> < HTML和GT;< HEAD><标题>联系表< /标题><身体GT;
    名称:$名\\ N'LT; BR>
    电子邮件:$电子邮件\\ n< BR>
    主题:$主题\\ n< BR>
    消息:$消息<身体GT;< /标题>< /头>< / HTML>中,//设置HTML,这里将仍然需要确保引用后数据名称
    文本=>
    名称:$名\\ n
    电子邮件:$电子邮件\\ n
    主题:$主题\\ n
    $消息,
    '从'=> [email protected],//在这里设置地址,它确实可以是任意
  );
curl_setopt($卷曲,CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_2);
$请求= $ url.'api / mail.send.json';
//生成卷曲的请求
$会话= curl_init($请求);
//告诉卷曲使用HTTP POST
curl_setopt($ session中,CURLOPT_POST,真正的);
//告诉袅袅,这是该职位的身体
curl_setopt($ session中,CURLOPT_POSTFIELDS,$ params)方法;
//告诉卷曲不返回头,但是做返回响应
curl_setopt($ session中,CURLOPT_HEADER,FALSE);
curl_setopt($ session中,CURLOPT_RETURNTRANSFER,真正的);
//获得响应
$响应= curl_exec($会议);
curl_close($会议);
//重定向感谢全成完成后你页面,将要建立一个,如果你不alreday有一个可用
标题(位置:thanks.html'); //随意使用你希望感谢你登陆页面任何冠军,但都需要到位presentthanks.html来引用文件名
出口();
//打印出来的一切
的print_r($响应);
?>

但我不明白,我该如何申请我在Sendgrid创建的模板,我都不知道怎么发,而不是简单的文本HTML电子邮件...


解决方案

 < PHP
要求供应商/ autoload.php';
Dotenv ::负载(__ DIR__);
$ sendgrid_apikey = GETENV('SG_KEY');
$ sendgrid =新SendGrid($ sendgrid_apikey);
$ URL ='https://api.sendgrid.com/';
$传球= $ sendgrid_apikey;
$ template_id ='< your_template_id>';
$ JS =阵列(
  '子'=>阵列(':名称'=>阵列('埃尔默'))
  过滤器= GT;阵列('模板'=>阵列(设置=>阵列(启用=大于1,'template_id'=> $ template_id)))
);
回声json_en code($ JS);
$ PARAMS =阵列(
    '到'=> [email protected]
    toname'=> 埃尔默·托马斯
    '从'=> [email protected]
    FROMNAME'=> DX队,
    '主题'=> PHP测试,
    文本=> 我的文字!
    'HTML'=> <强>!我是HTML< / STRONG>中,
    X-smtpapi'=> json_en code($ JS)
  );
$请求= $ url.'api / mail.send.json';
$会话= curl_init($请求);
curl_setopt($ session中,CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_2);
curl_setopt($ session中,CURLOPT_HTTPHEADER,阵列('授权:承载'$ sendgrid_apikey));
curl_setopt($ session中,CURLOPT_POST,真正的);
curl_setopt($ session中,CURLOPT_POSTFIELDS,$ params)方法;
curl_setopt($ session中,CURLOPT_HEADER,FALSE);
curl_setopt($ session中,CURLOPT_RETURNTRANSFER,真正的);
$响应= curl_exec($会议);
curl_close($会议);
的print_r($响应);

I'm using this code provided by Sendgrid itself

<?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'        => "[email protected]", // 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'      => "[email protected]", // 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);
?>

But I don't understand how do i apply the templates i created in Sendgrid, neither I understand how to send HTML email instead of simple text...

解决方案
<?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'        => "[email protected]", 
    'toname'    => "Elmer Thomas",
    'from'      => "[email protected]",
    '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:应用模板到WEB API小狗卷曲的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:20