Question

I copied a code for PEAR mail from a website, and input my data. It works. It sends mail, however, I want to use bcc to send to a lot of people and keep their addresses anonymous, and it will send to the $to recipients, but not the $bcc.

The code:

<?php
$message = "yay email!";
require_once("Mail.php");
$from = 'myaddress@mysite.com ';
$to = "anadress@gmail.com";
$bcc = "thepeopleimemailing@yaddayadda.com";
$subject = " test";
$body = $message;
$host = "smtp.mysite.com";
$username = "myusername";
$password = "mypassword";
$headers = array ('From' => $from,
    'To' => $to,
    'Cc' => $cc,
    'Bcc' => $bcc,
    'Subject' => $subject
);
$recipients = $to;


$smtp = Mail::factory('smtp',
    array ('host' => $host,
        'auth' => true,
        'username' => $username,
        'password' => $password,
        'port' => '25'
    )
);
$mail = $smtp->send($recipients, $headers, $body);
if (PEAR::isError($mail)) {
    echo($mail->getMessage());
}
else {
    echo("Message successfully sent!");
}
?>

P.s. I read on anther forum that I shouldn't put the headers in an array? I'm having trouble grasping the concept of the headers. What do they do and how should I organize them? I just want a to, from, subject, and bcc.

Thanks!

Was it helpful?

Solution 3

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

see the link below for pear mail

Sending multiple CC's and BCCs with PHP PEAR MAIL

or can get help from

http://phpmailer.worxware.com/index.php?pg=exampledb -- it is not pear mail. but it works very fine. I have used this and it is very easy to integrate.

OTHER TIPS

To elaborate on Chaky31's answer to send a Bcc use the following, note that we do NOT specify any Bcc information in the header:

//All other variables should be self explanatory!

//The main recipient
$to = "test@test.com";

//Bcc recipients
$bcc = "bcc@test.com";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

//We append the bcc addresses as comma seperated values to the send method
$mail = $smtp->send($to . "," . $bcc, $headers, $body);

For those who are looking to the solution to adding cc and bcc in PEAR php mail. Here is the simple solution and the abbreviated explanation why.

ANSWER: Everyone who want to receive the mail must be added to the $recipients field. If they are not in this field, they will not get the mail. Everything you want to be visible, add to the header. Therefore, since bcc is BLIND carbon copy, do NOT add it to the header.

WHY?: The recipient field dictates where the mail goes, the headers dictate what is displayed. If you do not add cc to the header, then you can make them blind too. Whichever tickles your fancy. Any questions, check the link ripa added above! Great explanation!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top