Я использую этот код, предоставленный самим 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 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);
Во-первых, я предполагаю, что вы уже создали свой шаблон здесь: https://sendgrid.com/templates и, для примера, шаблон выглядит следующим образом:
Hello :name, <%body%> With Best Regards, Your Library Tester
Затем, используя официальную библиотеку PHP SendGrid и, возможно, 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 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);