How to send cc mail php

In PHP, you can send a CC (Carbon Copy) email using the mail() function or a PHP email library such as PHPMailer. Here are some examples:

Using the mail() function:

<?php
$to = '[email protected]';
$cc = '[email protected]';
$subject = 'Test email with CC';
$message = 'This is a test email with CC';

$headers = 'From: [email protected]'. "\r\n".
           'CC: '. $cc. "\r\n".
           'Subject: '. $subject;

mail($to, $subject, $message, $headers);
?>

In this example, the mail() function is used to send an email to the recipient at [email protected]. The CC header is set to [email protected], which will receive a copy of the email.

Using PHPMailer:

<?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 = 'password';
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->addCC('[email protected]');
$mail->Subject = 'Test email with CC';
$mail->Body = 'This is a test email with CC';

if (!$mail->send()) {
    echo 'Error sending email';
} else {
    echo 'Email sent successfully';
}
?>

In this example, the PHPMailer library is used to send an email to the recipient at [email protected]. The addCC() method is used to add the CC recipient at [email protected].

Using SwiftMailer:

<?php
require_once 'SwiftMailer/Swift.php';

$mail = Swift_Message::newInstance();
$mail->setSubject('Test email with CC');
$mail->setFrom('[email protected]');
$mail->setTo(array('[email protected]'));
$mail->addCc(array('[email protected]'));
$mail->setBody('This is a test email with CC');

$transport = Swift_SmtpTransport::newInstance('smtp.example.com', 587);
$transport->setUsername('[email protected]');
$transport->setPassword('password');

$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($mail);

if (!$result) {
    echo 'Error sending email';
} else {
    echo 'Email sent successfully';
}
?>

In this example, the SwiftMailer library is used to send an email to the recipient at [email protected]. The addCc() method is used to add the CC recipient at [email protected].

Note that the CC header is not supported by all email clients, and some clients may not display the CC recipients. Additionally, some email providers may block or filter emails with CC recipients.