forked from PHPMailer/PHPMailer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.