这是最新版本的一个工作示例,在 Office 365 上,我使用它从共享文件夹发送电子邮件...
<?
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once('./phpmailer/Exception.php');
require_once('./phpmailer/PHPMailer.php');
require_once('./phpmailer/SMTP.php');
//* Working Example As Of 09/21/2019 - Sends From Shared Mailbox With Mailbox Member
function SendO365EmailTLS($options)
{
$from = isset($options['from']) ? $options['from'] : false;
$recipients = isset($options['recipients']) ? $options['recipients'] : false;
$ccRecipeints = isset($options['ccrecipients']) ? $options['ccrecipients'] : [];
$bccRecipients = isset($options['bccrecipients']) ? $options['bccrecipients'] : [];
$attachments = isset($options['attachments']) ? $options['attachments'] : [];
$credentials = isset($options['credentials']) ? $options['credentials'] : false;
$subject = isset($options['subject']) ? $options['subject'] : '';
$body = isset($options['body']) ? $options['body'] : '';
if(!$from) throw new Exception('Cannot send email with blank \'from\' field');
if(!$recipients) throw new Exception('Cannot send email, no recipients specified!');
if(!$credentials) throw new Exception('Cannot send email, credentials not provided!');
$mail = new PHPMailer;
foreach($recipients as $recipient) $mail->addAddress( $recipient[ 'email'], $recipient['name']);
foreach($ccRecipeints as $ccRecipient) $mail->addCC( $ccRecipient[ 'email'], $ccRecipient['name']);
foreach($bccRecipients as $bccRecipient) $mail->addBCC( $bccRecipient['email'],$bccRecipient['name']);
foreach($attachments as $attachment) $mail->addAttachment($attachment[ 'path' ], $attachment['name']);
$mail->setFrom($from['email'], $from['name']);
$mail->Username = $credentials['username'];
$mail->Password = $credentials['password'];
$mail->Host = 'smtp.office365.com';
$mail->Subject = $subject;
$mail->SMTPSecure = 'tls';
$mail->Body = $body;
$mail->SMTPAuth = true;
$mail->isHTML(true);
$mail->Port = 587;
$mail->isSMTP();
$success = $mail->send();
return $success;
}
// $options = ['from'=> ['email'=>'', 'name'=>''],
// 'recipients'=> [['email'=>'', 'name'=>'']],
// 'ccrecipients'=> [['email'=>'', 'name'=>'']],
// 'bccrecipients'=>[['email'=>'', 'name'=>'']],
// 'attachments'=> [['path'=>'./attachments/file1.jpg','name'=>'1.jpg'],
// ['path'=>'./attachments/file2.jpg','name'=>'2.jpg'],
// ['path'=>'./attachments/file3.jpg','name'=>'3.jpg']],
// 'credentials'=> ['username'=>'','password'=>''],
// 'subject'=> 'Email Subject Line',
// 'body'=> '<h1>Email Body</h1><p>HTML!!!</p>'];
// $success = SendO365EmailTLS($options);
// echo $success ? 'Email Sent':'Email Not Sent';
// die();