How to add cc in smtp mail php

To add a CC (Carbon Copy) recipient in an SMTP email using PHP, you can use the addCC method of the PHPMailer class. Here's an example:

<?php
require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'From Name');
$mail->addAddress('[email protected]', 'To Name'); // Add a recipient
$mail->addCC('[email protected]', 'CC Name'); // Add a CC recipient
$mail->addBCC('[email protected]', 'BCC Name'); // Add a BCC recipient

$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email';

if (!$mail->send()) {
    echo 'Error: '. $mail->ErrorInfo;
} else {
    echo 'Email sent!';
}
?>

In this example, we're using the PHPMailer class to send an email. We're setting the from address, adding a recipient, a CC recipient, and a BCC recipient. The addCC method is used to add the CC recipient.

Make sure to replace the placeholders (smtp.example.com, [email protected], your_password, [email protected], [email protected], [email protected], [email protected]) with your actual email server settings and email addresses.

Also, make sure to install the PHPMailer class by running the following command in your terminal:

composer require phpmailer/phpmailer

This will install the PHPMailer class and its dependencies.