PHP mail()BCC - 仅显示To:头中的结束接收者地址 [英] PHP mail() BCC - display only the end receivers address in To: header

查看:117
本文介绍了PHP mail()BCC - 仅显示To:头中的结束接收者地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP mail()从BCC获取数据库中的订阅者列表。一切都有效果,但是我遇到了一个整个早上困扰我的问题。我可以使用BCC发送列表,但无法将收件人的电子邮件地址附加到To:中。

I'm attempting to BCC to a list of subscribers from a database, using PHP mail(). Everything works, but I'm running into a problem that has troubled me all morning. I'm able to send the list with BCC, but are unable to append the receiving end email address to the deader "To:".

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

For example, I send the list to the following email addresses (test1@example.com, test2@example.com, and test3@example.com). Each email address receives an email and the other email addresses are hidden because of BCC.

我的问题是,在标题中,To:显示为空白列表的接收端。我明白并知道该标题不会显示,因为我只有外发消息中的BCC标题。我已经认识到循环过程,但我收到了所有的电子邮件,在一个循环中加了一个电子邮件。

My problem is that in the header, "To:" is displayed blank on all of the receiving ends of the list. That I understand and know that header won't display because I have only the BCC header within the outgoing message. I've attemted to for loop the process but I receive all of the emails, plus one for that address in one loop.

这是我的工作代码:工作代码包括我尝试解决 To:标题的循环。我可以发送电子邮件,虽然我收到所有发送的电子邮件。

Here is my working code: The working code includes the loop that I attempted for solving the To: header. I'm able to send the email, though I receive all of the emails that were sent.

<?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列表,并在收件人标题中仅显示收件人电子邮件地址。任何想法或想法?

My general thought was to loop, but I can't find a solution that actually solves this completely, by BBC to the list and displaying only the receiving ends email address in the To: header. Any thoughts or ideas?

使用SMTP服务器更新

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

I've been attempting to use the solution found in this thread and apply it to an SMTP server. Send the message using SendGrid was the ideal choice. I've come up with the below option, but the script only seems to send one message to one of the addresses in the database and not all the address.

<?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>");
}
?>


推荐答案

代码包括对代码的一些一般性改进。我已经添加了内联评论来解释我已经做了什么。

The code includes some general improvements to your code. I have added inline comments to explain what I have done.

<?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&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, $subject, $message, $imploded_headers);
  }

此代码基本上一次发送一封电子邮件。没有人收到这样的电子邮件知道电子邮件发送到哪个电子邮件地址。

This code basically send out one email at a time. No one who receives such an email knows which email addresses the email went to.

如代码所述,此代码段可以进一步优化。由于电子邮件发送本身是一个非常困难的领域,我真的建议您找到一些绑定整个事情并与之配合使用的PHP库。您可以在整个电子邮件发送过程中发生如此多的错误,如果您不想在短时间内将其标记为垃圾邮件,那么我不会在生产中运行这样的脚本。

As mentioned in the code, this snippet can be further optimized. Since email sending itself is a pretty difficult area, I would really suggest that you find some PHP library that bundles that whole thing and work with it. You can make so many mistakes with the whole email sending thing, that I would not run a script like that in production if you do not want to get marked as spam soon after.

这篇关于PHP mail()BCC - 仅显示To:头中的结束接收者地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆