drztpx8735 2011-06-10 19:54
浏览 66
已采纳

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

I have a project that I am working on at my job and I am using Pear's mailing. I need to use smtp because we need to be able to track everything from our mailserver. And users need to be able to log in before sending a company based email. We cannot use php's mail function fo this.

My problem is that I cant find any documentation on the net for sending CC and Bcc as well as sending multiple BCCs. It is very easy to do with php' mail funciton . All you do is add it to the $header variable like so

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

This is my code for the php function where I use PEAR

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! 

Below is your unique login information.  

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

}

I have been trying to find a solution to this with no real info coming back my way. If someone could help me out with this I would be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • doudou5023 2011-06-10 23:45
    关注

    Have you tried to add multiple addresses "," separated?

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

    This might work, according to line 218 in the source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doumiao0498 2011-08-17 08:12
    关注

    You just need to add all to, cc, bcc in $recipients variable. Header decides where to send.

    评论
  • doubishi8303 2012-05-07 13:35
    关注

    There is a problem with Bcc though when using the PEAR Mail function: Thunderbird displays the Bcc header, so the recipient is not hidden, which is the whole point of Bcc. It is also displayed in the To: header list (since you have to include the Bcc list in recipients).

    Edit: SOLVED - I found from another site that the solution is just to include the Bcc list in the $recipients, and not in any of the headers. It works!

    Thus:

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

    Edit #2: Acknowledging my source - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于TRPD(波形特征)局部放电特征提取
  • ¥15 C语言快速排序函数纠错
  • ¥15 C#的一个应用程序书写
  • ¥65 页面调接口时加载卡住不响应
  • ¥35 用C语言解决编程问题
  • ¥15 unity硬件连接与使用
  • ¥15 鱼缸加热棒的数据分析或者实际案例也行
  • ¥15 postgresql11安装完成后,pgadmin无法启动
  • ¥15 (标签-无人机|关键词-Matlab代码)
  • ¥15 执行shell脚本提示参数太多