1

I'm trying to send an e-mail with a bcc. I can send it with a bcc fairly easily, but that's not quite what I want. The code that does work is

$message = new Mail_mime(); 
$message->setTXTBody($_text); 
...
$body = $message->get();
$thisSubject = $subject;
if (isset($_POST['_cc_sender'])) {
    $extraheaders = array("Bcc"=>$from_email,
                          "From"=>$from_email,
                          "Subject"=>$thisSubject);
} else {
    $extraheaders = array("From"=>$from_email, "Subject"=>$thisSubject);
}
$headers  = $message->headers($extraheaders);
$mail_obj = Mail::factory("mail");
$mail_obj->send($to, $headers, $body);

Where an e-mail form can set "_cc_sender" to have the submitter receive a copy. The problem is that exposes the $to address. To hide it, I figured I'd just send the e-mail to the submitter with a bcc to the intended recipient, as so:

$message = new Mail_mime(); 
$message->setTXTBody($_text); 
...
$body = $message->get();
$thisSubject = $subject;
if (isset($_POST['_cc_sender'])) {
    $extraheaders = array("Bcc"=>$to,
                          "From"=>$from_email,
                          "Subject"=>$thisSubject);
    $send_to = $from_email;
} else {
    $extraheaders = array("From"=>$from_email, "Subject"=>$thisSubject);
    $send_to = $to;
}
$headers  = $message->headers($extraheaders);
$mail_obj = Mail::factory("mail");
$mail_obj->send($send_to, $headers, $body);

However, when I do that, I end up getting two messages sent to the $from_email. One seems to be the bcc because my (Thunderbird) message filters treat them differently - moving one into a subfolder.

To be clear, if I just use a bcc, the submitter gets a blind copy while the intended recipient gets the e-mail. However, when I try to switch who gets the bcc, both e-mails go to the submitter.

If I switch the bcc to a cc, I see both headers are set correctly (it is going to the $from_email with a cc to $to). However, the e-mail still gets sent to the $from_email twice.

I've done var_dumps of everything I can think of and they just show what I'd expect. From what I understand, the "to:" field / recipients is set in $mail_obj->send - it doesn't show up in my var_dumps - so I can't figure out why the send is messing up.

One thing I have discovered is that changing the Mail::factory parameter from "mail" to "sendmaiL" has removed the second e-mail. Now I'm just getting the one to the $send_to address. This suggests the problem is with the mail program, which may be handling the headers strangely.

I'd seen another post which suggested there should be a difference between the recipients list and the headers. When using "sendmail" (but not "mail"), that seems to be true. With "sendmail", the recipients list (first parameter in $mail_obj->send) is not shown in the headers - it's all a "bcc". Adding $from_email to the $send_tolist achieves the results I was looking for:

    $message = new Mail_mime(); 
    $message->setTXTBody($_text); 
    ...
    $body = $message->get();
    $thisSubject = $subject;
    if (isset($_POST['_cc_sender'])) {
        $send_to = $from_email . "," . $to;
    } else {
        $send_to = $to;
    }
    $extraheaders = array("From"=>$from_email,
                          "Subject"=>$thisSubject);
    $headers  = $message->headers($extraheaders);
    $mail_obj = Mail::factory("sendmail");
    $mail_obj->send($send_to, $headers, $body);
Gary Dale
  • 79
  • 9

0 Answers0