Asked  7 Months ago    Answers:  5   Viewed   31 times

I know there are a few similar questions to this but I just can't get it working.

Ok, I have a list of emails grabbed from my database in a variable called $emailList. I can get my code to send an email from a form if I put the variable in the $to section but I cannot get it to work with bcc. I've even added an email to the $to incase it was that but it doesn't make a difference.

Here is my code.

$to = "name@mydomain.com";
$subject .= "".$emailSubject."";
$headers .= 'Bcc: $emailList';
$headers = "From: no-reply@thepartyfinder.co.ukrn" . "X-Mailer: php";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1rn";
$message = '<html><body>';
$message .= 'THE MESSAGE FROM THE FORM';

if (mail($to, $subject, $message, $headers)) {
    $sent = "Your email was sent!";
} else {
    $sent = ("Error sending email.");
}

I've tried both codes:

$headers .= 'Bcc: $emailList';

and

$headers .= 'Bcc: '.$emailList.';

It's not that the emails aren't separated because they are. I know they are because it works if I put $emailList in the $to section.


I Should add, ignore the $message bits and the HTML stuff. I've not provided all of that so that is why it's missing from this code.

 Answers

85

You have $headers .= '...'; followed by $headers = '...';; the second line is overwriting the first.

Just put the $headers .= "Bcc: $emailListrn"; say after the Content-type line and it should be fine.

On a side note, the To is generally required; mail servers might mark your message as spam otherwise.

$headers  = "From: no-reply@thepartyfinder.co.ukrn" .
  "X-Mailer: phprn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1rn";
$headers .= "Bcc: $emailListrn";
Wednesday, March 31, 2021
 
jerrygarciuh
answered 7 Months ago
38

The BCC addresses are not stripped off at the destination email server. That's not how it works.

How SMTP actually works

  • The sender will send a list of RCPT TO commands to the SMTP server, one for each receiver email addresses, and this command does not distinguish whether the receiver is a normal To, CC or BCC type receiver.
  • Soon enough after calling the command that tells the SMTP server who's the sender, who's the server, and everything else, only then the sender will call the DATA command, in which will contain the content of the email - which consist of the email headers and body - the one that are received by email clients. Among these email headers are the usual from address, to address, CC address.
  • The BCC address is not shown to the receiver, simply because it's not printed out under the DATA command, not because the destination SMTP server stripped them away. The destination SMTP server will just refer to the RCPT TO for the list of email addresses that should receive the email content. It does not really care whether the receiver is in the To, CC or BCC list.
    Update (to clarify): BCC email addresses must be listed in the RCPT TO command list, but the BCC header should not be printed under the DATA command.

Quoting a part of the RFC that I think is relevant to your case:

Please note that the mail data includes the memo header items such as Date, Subject, To, Cc, From [2].

Rolling out your own email sender

A couple of years ago, I frankly think, is quite a long time back to assume that you still memorize end-to-end of RFC 821. :)

Wednesday, March 31, 2021
 
Nickool
answered 7 Months ago
74

In your code you are sending one message to 1 address and BCCing to all the others, so there is only a single To address - this is just how BCC works. If you want to have each recipient's address shown as the To address, you must send each message individually - look at the mailing list example provided. If you want to spot failures in a long list of recipients of any kind, make sure you're using an up-to-date version of PHPMailer as that was buggy in the past. Any bad addresses will be reported in $mail->ErrorInfo.

Wednesday, March 31, 2021
 
tpow
answered 7 Months ago
43

The separator should be a comma (,) and there should not be a space.

See RFC 6068.

Thursday, August 12, 2021
 
weegee
answered 2 Months ago
52

Generate a single email per recipient. Use the To field instead of BCC to make it personal.

Advantages

  • The mail queue will accurately reflect what is happening.
  • You can distribute the load to multiple email servers.
  • You can personalize the "To" "Subject" "Body" etc.
  • You can use tracking URL's.
  • Mail servers often have a BCC limit per message. You will not hit a limit if you send a single message at a time.
  • BCC emails typically remain in the queue until all deliveries are complete. It is rare, but we have experienced (with the latest qmail) that sometimes a single recipient will respond with an error that confuses the mail server to send it again, fail, again, fail...until we remove it from the queue. This gets people very upset.

Disadvantages

  • PHP script has to work harder to generate the individual requests.

There are surely other advantages and disadvantages, but that is the list I follow.

UPDATE: Regarding the PDF attachment, I would recommend providing a download link unless it is crucial to include it with the email.

  • PDF attachments make an email look more suspicious to spam/virus scanners, because spam is known to try to exploit vulnerable versions of Acrobat. Those PDF attachments might make your newsletter more likely to end up in the recipient's Spam folder.
  • Large PDF's (1+mb) are not friendly to people checking their email with slow connections or constrained devices such as smartphones.
  • A link is much smaller than the attachment. You will save upwards to 13GB of bandwidth if you leave off that 5MB attachment!
Thursday, August 12, 2021
 
macha
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :