test_callback.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. this callback example echos the results to the screen - implement to
  22. post to databases, build CSV log files, etc., with minor changes
  23. */
  24. $to = cleanEmails($to,'to');
  25. $cc = cleanEmails($cc[0],'cc');
  26. $bcc = cleanEmails($bcc[0],'cc');
  27. echo $result . "\tTo: " . $to['Name'] . "\tTo: " . $to['Email'] . "\tCc: " . $cc['Name'] . "\tCc: " . $cc['Email'] . "\tBcc: " . $bcc['Name'] . "\tBcc: " . $bcc['Email'] . "\t" . $subject . "<br />\n";
  28. return true;
  29. }
  30. $testLite = false;
  31. if ($testLite) {
  32. require_once '../class.phpmailer-lite.php';
  33. $mail = new PHPMailerLite();
  34. } else {
  35. require_once '../class.phpmailer.php';
  36. $mail = new PHPMailer();
  37. }
  38. try {
  39. $mail->IsMail(); // telling the class to use SMTP
  40. $mail->SetFrom('you@yourdomain.com', 'Your Name');
  41. $mail->AddAddress('another@yourdomain.com', 'John Doe');
  42. $mail->Subject = 'PHPMailer Lite Test Subject via Mail()';
  43. $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  44. $mail->MsgHTML(file_get_contents('contents.html'));
  45. $mail->AddAttachment('images/phpmailer.gif'); // attachment
  46. $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  47. $mail->action_function = 'callbackAction';
  48. $mail->Send();
  49. echo "Message Sent OK</p>\n";
  50. } catch (phpmailerException $e) {
  51. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  52. } catch (Exception $e) {
  53. echo $e->getMessage(); //Boring error messages from anything else!
  54. }
  55. function cleanEmails($str,$type) {
  56. if ($type == 'cc') {
  57. $addy['Email'] = $str[0];
  58. $addy['Name'] = $str[1];
  59. return $addy;
  60. }
  61. if (!strstr($str, ' <')) {
  62. $addy['Name'] = '';
  63. $addy['Email'] = $addy;
  64. return $addy;
  65. }
  66. $addyArr = explode(' <', $str);
  67. if (substr($addyArr[1],-1) == '>') {
  68. $addyArr[1] = substr($addyArr[1],0,-1);
  69. }
  70. $addy['Name'] = $addyArr[0];
  71. $addy['Email'] = $addyArr[1];
  72. $addy['Email'] = str_replace('@', '&#64;', $addy['Email']);
  73. return $addy;
  74. }
  75. ?>
  76. </body>
  77. </html>