使用PHP PEAR MAIL发送多个CC和BCC

时间:2011-06-10 19:54:57

标签: php email pear

我有一个项目,我正在工作,我正在使用Pear的邮件。我需要使用smtp,因为我们需要能够从邮件服务器跟踪所有内容。用户需要能够在发送基于公司的电子邮件之前登录。我们不能使用php的邮件功能。

我的问题是我无法在网上找到任何有关发送CC和Bcc以及发送多个BCC的文档。使用php'非常容易。邮件功能。你所要做的就是将其添加到$ he​​ader变量中,如此

$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

这是我使用PEAR

的php函数的代码
function sender_mail($email,$subject,$mailmsg, $cc, $bcc){

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

    //DEFAULT VALUE OF FROM 
    $from = "noreply@addata.net";

    //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);

}

我一直试图找到一个解决方案,没有真正的信息回来。如果有人能帮我解决这个问题,我将不胜感激。

3 个答案:

答案 0 :(得分:25)

使用PEAR邮件功能时,密件抄送有问题:Thunderbird显示密件抄送头,因此收件人不会被隐藏,这是密件抄送的全部内容。它也显示在“收件人:”标题列表中(因为您必须在收件人中包含“密件抄送”列表)。

编辑:已解决 - 我在其他网站上发现,该解决方案只是在“收件人”中包含密件抄送列表,而不是在任何邮件头中。它有效!

因此:

$bcc = "foo@example.com";
$to = "bar@example.com";
$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 :(得分:16)

您是否尝试添加多个地址“,”分开?

$headers['Cc'] = 'cc@example.com, bb@example.com, dd@ex.com';

根据line 218 in the source,这可能有用。

答案 2 :(得分:3)

您只需要在$ recipients变量中添加所有to,cc,bcc。标题决定在哪里发送。

相关问题