SendGrid: 将模板应用于 WEB API pup curl 方法
SendGrid: applying templates to WEB API pup curl method
我正在使用 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 电子邮件而不是简单的文本...
首先,我假设您已经在此处创建了模板:https://sendgrid.com/templates 为了这个示例,模板如下所示:
Hello :name,
<%body%>
With Best Regards,
Your Library Tester
然后,使用 the official PHP SendGrid library and optionally 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);
<?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 本身提供的代码
<?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 电子邮件而不是简单的文本...
首先,我假设您已经在此处创建了模板:https://sendgrid.com/templates 为了这个示例,模板如下所示:
Hello :name,
<%body%>
With Best Regards,
Your Library Tester
然后,使用 the official PHP SendGrid library and optionally 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);
<?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);