2017-08-09 80 views
0

我有一個PHP代碼發送電子郵件,cc &密送。PHP發送電子郵件BCC&CC使用SMTP

在這種情況下,cc不起作用。 我測試了它,並在我的電子郵件中看到沒有cc電子郵件。 (BCC電子郵件進行加工)

這裏是我的代碼:

require_once "Mail.php"; 

$from = "WEBAPPS <[email protected]>"; 
$subject = "Calibration will be expiring!"; 

$host = 'smtp.office365.com'; 
$port = '587'; 
$username = '[email protected]'; 
$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>"); 
} 

爲什麼CC不工作?以及如何解決它?

回答

1

碳拷貝是標題。 所有收件人都是郵件服務器的收件人,碳複本和密件抄送之間的區別僅僅是在標題中聲明它的問題。

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

真棒和酷,測試和完美工作。乾杯安德烈和謝謝 –

1

在上面的代碼中,所有recepients似乎對待相同。

$recipients = $to.", ".$cc.", ".$bcc; 

您可以使用PHPMailer類https://github.com/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 = '[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'; 
} 
+0

感謝您的答案,測試和確定。但是我怎樣才能在這個函數中設置更多的電子郵件地址$ mail-> addAddress('[email protected],[email protected],[email protected]');可能嗎? –

+1

如果發送CC標題時出現問題,更改整個庫並不總是最好的解決方案,只是因爲您有該電子郵件的工作片段 – AndreaBogazzi

+0

@HiDayurieDave我們可以逐個添加。 –