PHPMailer 是一个用于发送电子邮件的 PHP 库。它支持多种邮件传输协议,如 SMTP、POP3 和 IMAP,并且可以处理 HTML 邮件、附件等复杂功能。addBCC 方法是 PHPMailer 中的一个方法,用于添加密送(BCC)收件人。
addBCC 方法属于 PHPMailer 类的一个实例方法,用于添加密送收件人。
在需要发送邮件给多个收件人,但不希望所有收件人都知道其他收件人的邮箱地址时,可以使用 addBCC 方法。例如:
以下是一个使用 PHPMailer 发送邮件并添加密送收件人的示例代码:
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
// 邮件服务器设置
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// 发件人
$mail->setFrom('from@example.com', 'Mailer');
// 收件人
$mail->addAddress('to@example.com', 'Receiver');
// 密送收件人
$mail->addBCC('bcc1@example.com', 'BCC Receiver 1');
$mail->addBCC('bcc2@example.com', 'BCC Receiver 2');
// 邮件内容
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>原因:
解决方法:
Host、Username、Password 等信息正确。原因:
解决方法:
通过以上方法,可以有效解决 PHPMailer 中 addBCC 方法的相关问题。