Reputation: 1807
I have an PHP code to send email to, cc & bcc.
In this case, the cc is not working. I tested it and saw on my email there is no cc email. (BCC email is worked)
Here is my code:
require_once "Mail.php";
$from = "WEBAPPS <[email protected]>";
$subject = "Calibration will be expiring!";
$host = 'smtp.office365.com';
$port = '587';
$username = 'donotreply@test';
$password = 'w00ew?';
$to = "[email protected]";
$cc = "[email protected]";
$bcc = "[email protected]";
$body = "aa";
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8'
);
$recipients = $to.", ".$cc.", ".$bcc;
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
Why CC is not working? and how to solve it?
Upvotes: 3
Views: 12254
Reputation: 839
in the above code, all recepients seems treating as same.
$recipients = $to.", ".$cc.", ".$bcc;
You can use PHPMailer class at https://github.com/PHPMailer/PHPMailer . This is user friendly library.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail = new PHPMailer(true); //turn on the exception, it will throw exceptions on errors
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
if (!empty($recipientArr)) { //have mutiple recepients
foreach ($recipientArr AS $eachAddress) {
$mail->addAddress($eachAddress);
}
}
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$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';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Upvotes: 3
Reputation: 14731
The carbon copy is an header. All recipients are recipients in the same way for the mail server, the difference between carbon copy and blind carbon copy is just a matter of declaring it in the headers.
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);
Upvotes: 2