0

I have a mailer function, which sends mail to recipients extracted from database. How can I hide other recipients? BCC doesn't work. Mailer source:

$to=array();
while($row = mysql_fetch_array($subscrquery)) {
    array_push($to, $row['subscr_mail']);
}
$msgheader=$ttl;

$mailheaders  = "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-type: text/html; charset=UTF-8\r\n";
$mailheaders .= "From: ".$sender." <".$sender.">\r\n";
$mailheaders .= "Reply-To: ".$sender." <".$sender.">\r\n";
$mailheaders .= "Bcc: ".implode(',', $to)."\r\n";           
$mailmsga .= stripslashes($mailcontent);
$mailmsg .= strtr($mailmsga, array("<" => "\n<"));
mail(implode(',', $to), $msgheader,$mailmsg,$mailheaders);
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
gatorix
  • 31
  • 4
  • 3
    What do you mean by BCC doesn't work? It generates an error? Emails don't arrive? – andrewsi Aug 07 '12 at 20:53
  • 3
    It may not help with your answer, but you should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 07 '12 at 20:54
  • No, other users can see other recipient mails. For example mail1@mail.com, mail2@mail.com, mail3@mail com, mail4@mail.com..I need that other users can`t see other recipients emails, but they can see only her email. – gatorix Aug 07 '12 at 20:56
  • @gatorix - see Greg's answer, in that case. – andrewsi Aug 07 '12 at 21:02

3 Answers3

3

Typically, you want to send one e-mail message per recipient. Sending BCC will make it more likely that you get filtered by spam filters.

If your list is large, then you'll want to avoid using PHP's built-in mail method because it opens and closes a connection for every single e-mail. Instead, you should use an SMTP e-mailer that will only open one connection for all the e-mails that get sent. Possible options:

  • the (probably outdated) PEAR package has an SMTP mailer
  • Zend\Mail\Transport\Smtp is a more up-to-date namespaced package

Most larger frameworks, like Zend, probably have their own SMTP mailer as well.

In general, it is a good idea to use an existing package so that you don't have to worry about header injections, maximum line-lengths for e-mails, etc.

Kevin Nelson
  • 7,613
  • 4
  • 31
  • 42
1

There's a problem on your last line:

mail(implode(',', $to), $msgheader,$mailmsg,$mailheaders);

Right there, you're sending the email "to" everyone. The BCC handles that already. Use a bogus address (or your address, or whatever) in the first argument to mail, and it should fix the problem. The last line SHOULD read something like this:

mail('mailer@wherever.com', $msgheader,$mailmsg,$mailheaders);
gcochard
  • 11,408
  • 1
  • 26
  • 41
0

Simply add this line

$headers .= 'To:  Unknown<undefined>' . "\r\n";
Alfred
  • 21,058
  • 61
  • 167
  • 249
zohaib
  • 1