Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pear mail function bcc won't send

Tags:

php

email

pear

bcc

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 = '[email protected] ';
$to = "[email protected]";
$bcc = "[email protected]";
$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!

like image 957
Chaky31 Avatar asked Jan 28 '13 07:01

Chaky31


2 Answers

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 = "[email protected]";

//Bcc recipients
$bcc = "[email protected]";

$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);
like image 144
MarcF Avatar answered Oct 22 '22 10:10

MarcF


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!

like image 41
Chaky31 Avatar answered Oct 22 '22 10:10

Chaky31