繁体   English   中英

PHP使用SMTP发送电子邮件BCC和CC

[英]PHP send email BCC & CC using SMTP

我有一个PHP代码来发送电子邮件给cc,密送。

在这种情况下,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没有用? 以及如何解决?

抄送是一个标题。 所有收件人都以相同的方式接收邮件服务器,碳复制和盲目复制之间的区别只是在标题中声明它。

$headers = array(
'Port'          => $port,
'From'          => $from,
'To'            => $to,
'Subject'       => $subject,
'Content-Type'  => 'text/html; charset=UTF-8',
'Cc'            => $cc
);

在上面的代码中,所有接收者似乎都是一样的。

$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';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM