使用 PHP PEAR MAIL 发送多个抄送和密送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6311429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending multiple CC's and BCCs with PHP PEAR MAIL
提问by Dom
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.
我有一个项目正在我的工作中工作,我正在使用 Pear 的邮件。我需要使用 smtp,因为我们需要能够从我们的邮件服务器跟踪所有内容。并且用户需要能够在发送基于公司的电子邮件之前登录。我们不能为此使用 php 的邮件功能。
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
我的问题是我在网上找不到任何用于发送抄送和密件抄送以及发送多个密件抄送的文档。使用 php 的邮件功能很容易做到。你所做的就是将它添加到 $header 变量中
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";
This is my code for the php function where I use PEAR
这是我使用 PEAR 的 php 函数的代码
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);
}
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.
我一直在试图找到解决方案,但没有真正的信息回来。如果有人能帮我解决这个问题,我将不胜感激。
回答by Jeff G
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).
但是在使用 PEAR Mail 功能时,Bcc 存在一个问题:Thunderbird 显示 Bcc 标头,因此不会隐藏收件人,这就是 Bcc 的全部意义。它也显示在收件人:标题列表中(因为您必须在收件人中包含密件抄送列表)。
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!
编辑:已解决 - 我从另一个站点发现该解决方案只是将密件抄送列表包含在 $recipients 中,而不是包含在任何标题中。有用!
Thus:
因此:
$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);
Edit #2: Acknowledging my source - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/
编辑 #2:承认我的来源 - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/
回答by powtac
Have you tried to add multiple addresses "," separated?
您是否尝试添加多个地址“,”分隔?
$headers['Cc'] = '[email protected], [email protected], [email protected]';
This might work, according to line 218 in the source.
根据源代码中的第 218 行,这可能有效。
回答by Marghoob Suleman
You just need to add all to, cc, bcc in $recipients variable. Header decides where to send.
您只需要在 $recipients 变量中将所有内容添加到、cc、bcc。标题决定发送到哪里。