我有一个用于发送电子邮件的PHP代码,cc& BCC。
在这种情况下,cc无效。 我测试了它并在我的电子邮件中看到没有cc电子邮件。 (BCC电子邮件已经完成)
这是我的代码:
require_once "Mail.php";
$from = "WEBAPPS <donotreply@test.com>";
$subject = "Calibration will be expiring!";
$host = 'smtp.office365.com';
$port = '587';
$username = 'donotreply@test';
$password = 'w00ew?';
$to = "alwis.david@gmail.com";
$cc = "data.gw@gmail.com";
$bcc = "hidayurie.dave@yahoo.com";
$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>");
}
为什么CC无效?以及如何解决它?
答案 0 :(得分:2)
抄送是一个标题。 所有收件人都以相同的方式收发邮件服务器,复制和盲目复制之间的区别只是在标题中声明它。
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);
答案 1 :(得分:1)
在上面的代码中,所有接收者似乎都是一样的。
$recipients = $to.", ".$cc.", ".$bcc;
您可以在https://github.com/PHPMailer/PHPMailer使用PHPMailer类。这是用户友好的库。
<?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 = 'user@example.com'; // 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('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
if (!empty($recipientArr)) { //have mutiple recepients
foreach ($recipientArr AS $eachAddress) {
$mail->addAddress($eachAddress);
}
}
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$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';
}