PHP发送电子邮件BCC&;使用SMTP进行抄送
我有一个PHP代码发送电子邮件到,抄送和密件抄送 在这种情况下,cc不工作。 我测试了一下,发现在我的邮件里没有抄送邮件。(密件抄送电子邮件已处理) 这是我的密码:PHP发送电子邮件BCC&;使用SMTP进行抄送,php,email,Php,Email,我有一个PHP代码发送电子邮件到,抄送和密件抄送 在这种情况下,cc不工作。 我测试了一下,发现在我的邮件里没有抄送邮件。(密件抄送电子邮件已处理) 这是我的密码: require_once "Mail.php"; $from = "WEBAPPS <donotreply@test.com>"; $subject = "Calibration will be expiring!"; $host = 'smtp.office365.com'; $port = '587'; $use
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>");
}
require_once“Mail.php”;
$from=“WEBAPPS”;
$subject=“校准将过期!”;
$host='smtp.office365.com';
$port='587';
$username='1donotreply@test';
$password='w00ew?';
$to=“alwis。david@gmail.com";
$cc=“数据。gw@gmail.com";
$bcc=“hidayurie。dave@yahoo.com";
$body=“aa”;
$headers=数组(
“端口”=>$Port,
'From'=>$From,
'至'=>$至,
“主题”=>$Subject,
'内容类型'=>'文本/html;字符集=UTF-8'
);
$recipients=$to.,“$cc.,”$bcc;
$smtp=Mail::工厂('smtp',
数组('host'=>$host,
“auth”=>正确,
“用户名”=>$username,
“密码”=>$password));
$mail=$smtp->send($recipients,$headers,$body);
if(PEAR::isError($mail)){
echo(“”$mail->getMessage()””;
}否则{
echo(消息已成功发送!);
}
为什么CC不起作用?如何解决这个问题呢?在上面的代码中,所有的接收者似乎都是一样的
$recipients = $to.", ".$cc.", ".$bcc;
您可以在上使用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';
}
复写本是一个页眉。
对于邮件服务器,所有收件人都是相同的收件人,复写副本和盲复写副本之间的区别只是在标题中声明而已
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);
谢谢你的回答,已经测试过了。但是我如何在$mail->addAddress('s)功能中设置更多的电子邮件地址呢ellen@example.com, test@gmail.com, test1@gmail.com'); 有可能吗?如果发送CC头时出现问题,更改整个库并不总是最好的解决方案,因为您有一个可供使用的代码段email@HiDayurieDave我们可以逐一补充。@AndreaBogazzi我同意。只是分享,因为它更一般化。我们不需要设置标题,因为这是非常棒和酷的,经过测试和工作完美。干杯,安德里亚,谢谢你