簡體   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