PHP mail()BCC – 仅在To:标题中显示结束接收者地址

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP mail()BCC – 仅在To:标题中显示结束接收者地址脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 PHP mail()从数据库BCC到订阅者列表.一切正常,但我遇到的问题一直困扰着我.我可以使用BCC发送列表,但无法将接收端邮件地址附加到deader“To:”.

例如,我将列表发送到以下电子邮件地址(test1 @ example.COM,test2 @ example.com和test3@example.com).每个电子邮件地址都会收到一封电子邮件,其他电子邮件地址因BCC而被隐藏.

我的问题是在标题中,“To:”在列表的所有接收端都显示为空白.我明白并知道标题不会显示,因为我在传出消息中只有BCC标题.我已经注意到循环过程,但是我收到了所有的电子邮件,在一个循环中加了一个地址.

这是我的工作代码:工作代码包括我尝试解决To:标头的循环.我可以发送电子邮件,但我会收到所有已发送的电子邮件.

<?PHP

/*
   Gathers the number of rows wIThin the database. Used for the loop that displays the entire list in the BCC. 
*/

   session_start();
   include_once '../dbconnect.PHP';
   $result = MysqL_query("SELECT * From news");
   $num_rows = MysqL_num_rows($result);
   $rows = $num_rows;

/*
   Requests the list from the database,please the list in a loop,displays the list only once,and places list in an operator.
*/

   $result = MysqL_query("SELECT * FROM `news`");
     while($row = MysqL_fetch_array( $result )) {
       for ($x = 1; $x <= 1; $x++) {
         $contacts.= "".$row['email'].",";
       }
     }

/*
   ATTEMPT (It works... sort of): Sends mail to the list using BCC,displays the ONLY receivers email address in the To: header
*/

   $result = MysqL_query("SELECT * FROM `news`");
     while($row = MysqL_fetch_array( $result )) {
       for ($x = 1; $x <= 1; $x++) {
         $receiver= "".$row['email']."";

         $to = "$receiver";
         $subject = 'Test Email';
         $headers = "From: example@example.com\r\n";
         $headers .= "BCC: $contacts";

         $headers .= "MIME-Version: 1.0\r\n";
         $headers .= "Content-tyPE: text/htML; charset=ISO-8859-1\r\n";
         $message = '<html><body>';
         $message .= '<h1 style="">Test Message</h1>';
         $message .= '</body></html>';
         mail($to,$subject,$message,$headers);
      }
    }
?>

我的一般想法是循环,但我找不到一个实际上完全解决这个问题的解决方案,BBC到列表并且只显示To:标题中的接收端电子邮件地址.有什么想法或想法吗?

使用SMTP服务器更新

我一直在尝试使用此线程中找到的解决方案并将其应用于SMTP服务器.使用SendGrid发送消息是理想的选择.我已经提出了以下选项,但脚本似乎只发送一条消息到数据库中的一个地址而不是所有地址.

<?PHP
require_once "Mail.PHP";

$sub = $_POST['subject'];
$TTL = $_POST['title'];
$img = $_POST['image'];
$bdy = $_POST['body'];
$lk = $_POST['link'];

MysqL_connect("","","") or die(MysqL_error()) ; 
   MysqL_select_db("") or die(MysqL_error()) ; 

  $result = MysqL_query("SELECT `email` FROM news");

  $num_rows = MysqL_num_rows($result);

  $receivers = array();
  while ($row = MysqL_fetch_array($result)) {
    $receivers[] = $row['email'];
  }

  foreach ($receivers as $receiver) { }

$from = "test@example.com";
$to = $receiver;
$subject = $sub;
$mime = "1.0";
$ctype = "text/html; charset=ISO-8859-1";
$body = '
<html><body>
<p>Test Message!</p>
</body></html>
';

$host = "";
$port = "";
$username = "";
$password = "";

$headers = array ('From' => $from,'To' => $to,'Subject' => $subject,'MIME-Version' => $mime,'Content-Type:' => $ctype);

$smtp = Mail::factory('smtp',array ('host' => $host,'port' => $port,'auth' => true,'username' => $username,'password' => $password));

$mail = $smtp->send($to,$headers,$body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>;message successfully sent!</p>");
}
?>

解决方法

代码包含对代码的一些常规改进.我已添加内联注释来解释我所做的事情.

<?PHP

  // General thing: If you do not need a session,do not start one ;)
  session_start();

  include_once '../dbconnect.PHP';

  // Select only what you really need from the table. This saves you memory
  // and it speeds up the query.
  $result = MysqL_query("SELECT `email` FROM news");

  // You are not using these numbers in the script you showed us. I am just
  // leaving them in here to show you,how you can reuse the "$result"
  // VARiable without querying the database again.
  $num_rows = MysqL_num_rows($result);

  // We are reusing the "$result" here without re-querying the database,which
  // speeds the whole PRocess up and takes load away from the database. We are
  // storing all receivers in a dedicated variable,to reuse them later on.
  $receivers = array();
  while ($row = MysqL_fetch_array($result)) {
    $receivers[] = $row['email'];
  }

  // Now,instead of querying the database again,we are using our Stored mail
  // addresses in "$receivers" to send the emails one by one. We Could have
  // done this part in the "while" loop before,but I wanted to stay close to
  // your code,so you would recognize it ;)
  foreach ($receivers as $receiver) {
    // I have removed the "for" loop here,because it runs only once. If a loop
    // only runs once and you control all its input,you really do not need a
    // loop at all (except some exceptions,just in case someone kNows one^^).

    // You can actually just put the value of $receiver in $to. PHP is pretty
    // good at typecasting of scalar types (integers,strings etc.),so you do
    // not need to worry about that.
    $to = $receiver;

    $subject = 'Test Email';

    // I am putting the headers into an array and implode them later on. This
    // way we can make sure that we are not forgetting the new line characters
    // somewhere.
    $headers = array(
      "From: example@example.com","MIME-Version: 1.0","Content-Type: text/html; charset=ISO-8859-1",// I have removed the "BCC" header here,because this loops send out an
      // email to each user seperately. It is basically me sending an email to
      // you. Afterwards I copy&amp;paste all the content to another new mail and
      // send it to another fella. You would never kNow that you both got the
      // same mail ;)
    );

    $message = '<html><body>';
    $message .= '<h1 style="">Test Message</h1>';
    $message .= '</body></html>';

    // Imploding the headers.
    $imploded_headers = implode("\r\n",$headers);

    mail($to,$imploded_headers);
  }

代码基本上一次发送一封电子邮件.收到此类电子邮件的人都不知道该电子邮件发送到哪个电子邮件地址.

代码中所述,此代码段可以进一步优化.由于电子邮件发送本身是一个非常困难的领域,我真的建议您找到一些PHP库,它捆绑整个事情并使用它.你可以在整个电子邮件发送过程中犯很多错误,如果你不希望很快就被标记垃圾邮件,我就不会在生产中运行这样的脚本.

脚本宝典总结

以上是脚本宝典为你收集整理的PHP mail()BCC – 仅在To:标题中显示结束接收者地址全部内容,希望文章能够帮你解决PHP mail()BCC – 仅在To:标题中显示结束接收者地址所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。