Skip to content

Commit

Permalink
Callback cleanup, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed May 1, 2016
1 parent 558bcdc commit efbcfa9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 102 deletions.
17 changes: 0 additions & 17 deletions docs/Callback_function_notes.txt

This file was deleted.

75 changes: 75 additions & 0 deletions examples/callback.phps
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* This example shows how to use a callback function from PHPMailer.
*/

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../vendor/autoload.php';

/**
* Example PHPMailer callback function.
* This is a global function, but you can also pass a closure (or any other callable)
* to the `action_function` property.
* @param boolean $result result of the send action
* @param array $to email address of the recipient
* @param array $cc cc email addresses
* @param array $bcc bcc email addresses
* @param string $subject the subject
* @param string $body the email body
*/
function callbackAction($result, $to, $cc, $bcc, $subject, $body)
{
echo "Message subject: \"$subject\"\n";
foreach ($to as $address) {
echo "Message to {$address[0]} <{$address[1]}>\n";
}
foreach ($cc as $address) {
echo "Message CC to {$address[0]} <{$address[1]}>\n";
}
foreach ($bcc as $toaddress) {
echo "Message BCC to {$toaddress[0]} <{$toaddress[1]}>\n";
}
if ($result) {
echo "Message sent successfully\n";
} else {
echo "Message send failed\n";
}
}

require_once '../vendor/autoload.php';

$mail = new PHPMailer;

try {
$mail->isMail();
$mail->setFrom('you@example.com', 'Your Name');
$mail->addAddress('jane@example.com', 'Jane Doe');
$mail->addCC('john@example.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject';
$mail->msgHTML(file_get_contents('../examples/contents.html'));
// optional - msgHTML will create an alternate automatically
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->addAttachment('images/phpmailer_mini.png'); // attachment
$mail->action_function = 'callbackAction';
$mail->send();
} catch (Exception $e) {
echo $e->errorMessage();
}

//Alternative approach using a closure
try {
$mail->isMail();
$mail->action_function = function ($result, $to, $cc, $bcc, $subject, $body) {
if ($result) {
echo "Message sent successfully\n";
} else {
echo "Message send failed\n";
}
};
$mail->send();
} catch (Exception $e) {
echo $e->errorMessage();
}
6 changes: 2 additions & 4 deletions examples/gmail.phps
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
*/
namespace PHPMailer\PHPMailer;

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;

require '../vendor/autoload.php';

Expand Down
81 changes: 0 additions & 81 deletions test/test_callback.php

This file was deleted.

0 comments on commit efbcfa9

Please sign in to comment.