0
0
mirror of https://github.com/PHPMailer/PHPMailer.git synced 2024-09-20 01:52:15 +02:00
PHPMailer/examples/callback.phps

77 lines
2.2 KiB
Plaintext
Raw Normal View History

2016-05-01 17:00:48 +02:00
<?php
2016-05-01 17:00:48 +02:00
/**
* 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 bool $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
2016-05-01 17:00:48 +02:00
*/
function callbackAction($result, $to, $cc, $bcc, $subject, $body)
{
echo "Message subject: \"$subject\"\n";
foreach ($to as $address) {
2016-05-01 17:06:20 +02:00
echo "Message to {$address[1]} <{$address[0]}>\n";
2016-05-01 17:00:48 +02:00
}
foreach ($cc as $address) {
2016-05-01 17:06:20 +02:00
echo "Message CC to {$address[1]} <{$address[0]}>\n";
2016-05-01 17:00:48 +02:00
}
foreach ($bcc as $toaddress) {
2016-05-01 17:06:20 +02:00
echo "Message BCC to {$toaddress[1]} <{$toaddress[0]}>\n";
2016-05-01 17:00:48 +02:00
}
if ($result) {
echo "Message sent successfully\n";
} else {
echo "Message send failed\n";
}
}
require_once '../vendor/autoload.php';
$mail = new PHPMailer();
2016-05-01 17:00:48 +02:00
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'));
2021-02-19 13:42:01 +01:00
//Optional - msgHTML will create an alternate automatically
2016-05-01 17:00:48 +02:00
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
2021-02-19 13:42:01 +01:00
$mail->addAttachment('images/phpmailer_mini.png');
2016-05-01 17:00:48 +02:00
$mail->action_function = 'callbackAction';
$mail->send();
} catch (Exception $e) {
echo $e->errorMessage();
}
//Alternative approach using a closure
try {
2019-10-08 13:35:03 +02:00
$mail->action_function = static function ($result, $to, $cc, $bcc, $subject, $body) {
2016-05-01 17:00:48 +02:00
if ($result) {
echo "Message sent successfully\n";
} else {
echo "Message send failed\n";
}
};
$mail->send();
} catch (Exception $e) {
echo $e->errorMessage();
}