2011-06-10 133 views
13

我有一個項目,我正在工作,我正在使用Pear的郵件。我需要使用smtp,因爲我們需要能夠跟蹤我們的郵件服務器中的所有內容。用戶需要能夠在發送基於公司的電子郵件之前登錄。我們不能使用php的郵件功能。發送多個CC和BCC使用PHP PEAR郵件

我的問題是,我無法找到網絡上的任何文件發送抄送和密件抄送以及發送多個密件抄送。用php的郵件功能很容易做到。你要做的就是把它添加到$頭變量,像這樣

$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 

這是我在那裏我使用PEAR

function sender_mail($email,$subject,$mailmsg, $cc, $bcc){ 

    include("Mail.php"); 
    /* mail setup recipients, subject etc */ 

    //DEFAULT VALUE OF FROM 
    $from = "[email protected]"; 

    //GET EMAIL OF USER 
    $result = mysql_query("SELECT email, email_pass FROM u_perinfo WHERE user_id = '$_SESSION[uid]'") 
    or die("There was an error when grabbing your email information"); 
    if(mysql_num_rows($result) > 0){ 
     $row = mysql_fetch_array($result); 
     if($row[0] != ''){ 
      $from = $row[0]; 
     } 
     $email_pass = $row[1]; 
    } 

    $recipients = "$email"; 
    $headers["From"] = "$from"; 
    $headers["To"] = "$email"; 
    $headers["Subject"] = $subject; 
    $headers["Cc"] = "$cc"; //Line added by Me to see if it works 
    $headers["Bcc"] = "$bcc"; //Line added by Me to see if it works 


    //$mailmsg = "Welcome to Addatareference.com! \r\n\r\nBelow is your unique login information. \r\n\r\n(Please do not share your login information.)$accountinfo"; 
    /* SMTP server name, port, user/passwd */ 
    $smtpinfo["host"] = "smtp.emailsrvr.com"; 
    $smtpinfo["port"] = "25"; 
    $smtpinfo["auth"] = true; 
    $smtpinfo["username"] = "$from"; 
    $smtpinfo["password"] = "$email_pass"; 
    /* Create the mail object using the Mail::factory method */ 
    $mail_object =& Mail::factory("smtp", $smtpinfo); 
    /* Ok send mail */ 
    $mail_object->send($recipients, $headers, $mailmsg); 

} 

我一直在試圖找到一個解決這個PHP函數代碼沒有真正的信息回來我的方式。如果有人可以幫我解決這個問題,我將不勝感激。

+0

不知道Mail.php你使用什麼,但我建議使用的PHPMailer(HTTP:而不是使用Swiftmailer(http://swiftmailer.org)。 – 2011-06-10 20:15:09

+2

您是否嘗試將所有電子郵件地址(包括密件抄送和抄送地址)添加到收件人列表中,然後在標題中指定抄送和密件抄送地址? – Jrod 2011-06-10 20:26:43

+0

另外,您是否能夠從系統發送郵件。您的主機可能會阻止端口25 – Colum 2011-06-10 20:27:43

回答

3

您只需在$ recipients變量中添加所有to,cc,bcc。標題決定要發送的位置。

25

雖然使用PEAR Mail功能時,Bcc存在問題:Thunderbird顯示Bcc標題,因此收件人未隱藏,這是Bcc的全部要點。它也顯示在收件人列表中(因爲您必須在收件人中包含密件抄送列表)。

編輯:已解決 - 我從另一個網站發現解決方案只是在$收件人中包含密件抄送列表,而不是在任何頭文件中。有用!

這樣:

$bcc = "[email protected]"; 
$to = "[email protected]"; 
$headers = array(..other stuff.., 'To' => $to, ..rest of header stuff); // No Bcc header! 
$recipients = $to.", ".$bcc; 
$mail = $smtp->send($recipients, $headers, $message); 

編輯#2:承認我的源 - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/

+1

感謝來源信用。 :)(我意識到我的謝謝你有點晚,但遲到比從未!) – 2014-03-16 21:39:08

+0

這是更好的答案。我只是測試它,並得到它的工作。直到現在(和這個答案)我無法解決密件抄送問題。 – 2014-03-31 01:56:57

+1

是的,但那不是真正的密送,對吧?它只是發送多封電子郵件。 – 2015-02-06 18:52:19