test_callback.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * boolean $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 '../PHPMailerAutoload.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 Test Subject';
  40. $mail->msgHTML(file_get_contents('../examples/contents.html'));
  41. // optional - msgHTML will create an alternate automatically
  42. $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
  43. $mail->addAttachment('../examples/images/phpmailer.png'); // attachment
  44. $mail->addAttachment('../examples/images/phpmailer_mini.png'); // 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>