forked from PHPMailer/PHPMailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.phps
74 lines (67 loc) · 2.28 KB
/
callback.phps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?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[1]} <{$address[0]}>\n";
}
foreach ($cc as $address) {
echo "Message CC to {$address[1]} <{$address[0]}>\n";
}
foreach ($bcc as $toaddress) {
echo "Message BCC to {$toaddress[1]} <{$toaddress[0]}>\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('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Jane Doe');
$mail->addCC('[email protected]', '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->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();
}