test_callback.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <html>
  2. <head>
  3. <title>PHPMailer Lite - DKIM and Callback Function test</title>
  4. </head>
  5. <body>
  6. <?php
  7. /* This is a sample callback function for PHPMailer Lite.
  8. * This callback function will echo the results of PHPMailer processing.
  9. */
  10. /* Callback (action) function
  11. * bool $result result of the send action
  12. * string $to email address of the recipient
  13. * string $cc cc email addresses
  14. * string $bcc bcc email addresses
  15. * string $subject the subject
  16. * string $body the email body
  17. * @return boolean
  18. */
  19. function callbackAction($result, $to, $cc, $bcc, $subject, $body)
  20. {
  21. /*
  22. this callback example echos the results to the screen - implement to
  23. post to databases, build CSV log files, etc., with minor changes
  24. */
  25. $to = cleanEmails($to, 'to');
  26. $cc = cleanEmails($cc[0], 'cc');
  27. $bcc = cleanEmails($bcc[0], 'cc');
  28. echo $result . "\tTo: " . $to['Name'] . "\tTo: " . $to['Email'] . "\tCc: " . $cc['Name'] .
  29. "\tCc: " . $cc['Email'] . "\tBcc: " . $bcc['Name'] . "\tBcc: " . $bcc['Email'] .
  30. "\t" . $subject . "\n\n". $body . "\n";
  31. return true;
  32. }
  33. require_once '../class.phpmailer.php';
  34. $mail = new PHPMailer();
  35. try {
  36. $mail->isMail();
  37. $mail->setFrom('you@example.com', 'Your Name');
  38. $mail->addAddress('another@example.com', 'John Doe');
  39. $mail->Subject = 'PHPMailer Lite Test Subject via mail()';
  40. // optional - msgHTML will create an alternate automatically
  41. $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
  42. $mail->msgHTML(file_get_contents('../examples/contents.html'));
  43. $mail->addAttachment('../examples/images/phpmailer.png'); // attachment
  44. $mail->addAttachment('../examples/images/phpmailer_mini.gif'); // attachment
  45. $mail->action_function = 'callbackAction';
  46. $mail->send();
  47. echo "Message Sent OK</p>\n";
  48. } catch (phpmailerException $e) {
  49. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  50. } catch (Exception $e) {
  51. echo $e->getMessage(); //Boring error messages from anything else!
  52. }
  53. function cleanEmails($str, $type)
  54. {
  55. if ($type == 'cc') {
  56. $addy['Email'] = $str[0];
  57. $addy['Name'] = $str[1];
  58. return $addy;
  59. }
  60. if (!strstr($str, ' <')) {
  61. $addy['Name'] = '';
  62. $addy['Email'] = $addy;
  63. return $addy;
  64. }
  65. $addyArr = explode(' <', $str);
  66. if (substr($addyArr[1], -1) == '>') {
  67. $addyArr[1] = substr($addyArr[1], 0, -1);
  68. }
  69. $addy['Name'] = $addyArr[0];
  70. $addy['Email'] = $addyArr[1];
  71. $addy['Email'] = str_replace('@', '&#64;', $addy['Email']);
  72. return $addy;
  73. }
  74. ?>
  75. </body>
  76. </html>