Guest User

Untitled

a guest
Sep 24th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. <?php
  2. /*
  3. * This example is used for Swift Mailer V4
  4. */
  5. include "./lib/swift_required.php";
  6. include 'SmtpApiHeader.php';
  7. $hdr = new SmtpApiHeader();
  8. // The list of addresses this message will be sent to
  9. // [This list is used for sending multiple emails using just ONE request to SendGrid]
  10. $toList = array('destination1@example.com', 'destination2@example.com');
  11. // Specify the names of the recipients
  12. $nameList = array('Name 1', 'Name 2');
  13. // Used as an example of variable substitution
  14. $timeList = array('4 PM', '5 PM');
  15. // Set all of the above variables
  16. $hdr->addTo($toList);
  17. $hdr->addSubVal('-name-', $nameList);
  18. $hdr->addSubVal('-time-', $timeList);
  19. // Specify that this is an initial contact message
  20. $hdr->setCategory("initial");
  21. // You can optionally setup individual filters here, in this example, we have enabled the
  22. // footer filter
  23. $hdr->addFilterSetting('footer', 'enable', 1);
  24. $hdr->addFilterSetting('footer', "text/plain", "Thank you for your business");
  25. // The subject of your email
  26. $subject = 'Example SendGrid Email';
  27. // Where is this message coming from. For example, this message can be from
  28. // support@yourcompany.com, info@yourcompany.com
  29. $from = array('yourcompany@example.com' => 'Name Of Your Company');
  30. // If you do not specify a sender list above, you can specifiy the user here. If a sender
  31. // list IS specified above
  32. // This email address becomes irrelevant.
  33. $to = array('defaultdestination@example.com'=>'Personal Name Of Recipient');
  34. # Create the body of the message (a plain-text and an HTML version).
  35. # text is your plain-text email
  36. # html is your html version of the email
  37. # if the reciever is able to view html emails then only the html
  38. # email will be displayed
  39. /*
  40. * Note the variable substitution here =)
  41. */
  42. $text = <<<EOM
  43. Hello -name-,
  44. Thank you for your interest in our products. We have set up an appointment
  45. to call you at -time- EST to discuss your needs in more detail.
  46. Regards,
  47. Fred
  48. EOM;
  49. $html = <<<EOM
  50. <html>
  51.   <head></head>
  52.   <body>
  53.     <p>Hello -name-,<br>
  54.        Thank you for your interest in our products. We have set up an appointment
  55.              to call you at -time- EST to discuss your needs in more detail.
  56.                 Regards,
  57.                 Fred, How are you?<br>
  58.     </p>
  59.   </body>
  60. </html>
  61. EOM;
  62. // Your SendGrid account credentials
  63. $username = 'sendgridusername@yourdomain.com';
  64. $password = 'example';
  65. // Create new swift connection and authenticate
  66. $transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 25);
  67. $transport ->setUsername($username);
  68. $transport ->setPassword($password);
  69. $swift = Swift_Mailer::newInstance($transport);
  70. // Create a message (subject)
  71. $message = new Swift_Message($subject);
  72. // add SMTPAPI header to the message
  73. // *****IMPORTANT NOTE*****
  74. // SendGrid's asJSON function escapes characters. If you are using Swift Mailer's
  75. // PHP Mailer functions, the getTextHeader function will also escape characters.
  76. // This can cause the filter to be dropped.
  77. $headers = $message->getHeaders();
  78. $headers->addTextHeader('X-SMTPAPI', $hdr->asJSON());
  79. // attach the body of the email
  80. $message->setFrom($from);
  81. $message->setBody($html, 'text/html');
  82. $message->setTo($to);
  83. $message->addPart($text, 'text/plain');
  84. // send message
  85. if ($recipients = $swift->send($message, $failures))
  86. {
  87. // This will let us know how many users received this message
  88. // If we specify the names in the X-SMTPAPI header, then this will always be 1.
  89. echo 'Message sent out to '.$recipients.' users';
  90. }
  91. // something went wrong =(
  92. else
  93. {
  94. echo "Something went wrong - ";
  95. print_r($failures);
  96. }
Add Comment
Please, Sign In to add comment