9951 explained code solutions for 126 technologies


phpmailerHow can I use PHPMailer to send emails with a BCC address?


PHPMailer is an open-source library for sending emails with PHP. To use PHPMailer to send emails with a BCC address, you need to include the PHPMailer library in your PHP script and create an instance of the PHPMailer class.

// Include PHPMailer library
require_once 'PHPMailer/PHPMailerAutoload.php';

// Create instance of PHPMailer
$mail = new PHPMailer;

// Set BCC address
$mail->addBCC('[email protected]');

You can then add the other required details such as the sender, recipient, and subject, as well as the message body. Finally, you can send the email using the send() method.

// Set sender, recipient, subject, and body
$mail->setFrom('[email protected]', 'From Name');
$mail->addAddress('[email protected]', 'To Name');
$mail->Subject = 'PHPMailer BCC Test';
$mail->Body = 'This is a test of PHPMailer BCC';

// Send email
if ($mail->send()) {
    echo "Email sent successfully";
}
else {
    echo "Error sending email: " . $mail->ErrorInfo;
}

Output example

Email sent successfully

Code explanation

  • require_once 'PHPMailer/PHPMailerAutoload.php';: Include PHPMailer library.
  • $mail = new PHPMailer;: Create instance of PHPMailer class.
  • $mail->addBCC('[email protected]');: Set BCC address.
  • $mail->setFrom('[email protected]', 'From Name');: Set sender address and name.
  • $mail->addAddress('[email protected]', 'To Name');: Set recipient address and name.
  • $mail->Subject = 'PHPMailer BCC Test';: Set email subject.
  • $mail->Body = 'This is a test of PHPMailer BCC';: Set email body.
  • $mail->send(): Send email.

Helpful links

Edit this code on GitHub