Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a mailing script to send emails to specific users. I get the emails from a mysql database.

The email setup is working fine when sending the email to a single person. when just using to: and no bcc:

But when I insert the line with bbc: I get an error :

$headers .= "Bcc: ". implode(", ", $recipients) ."\r\n"; <-- Error happens here

i have tried both removing space in implode and checked all emails. They are all valid seperated by comma

Also i tried to insert: error_get_last() and the error returns no valuables. The array is empty. So i really cant see where the error should be.

My code is:

$con=mysqli_connect("","","","");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT email FROM users WHERE receive_email = 1") 
or die(mysqli_error());

/* Details for emails */
$name = "domain.com";
$email = "[email protected]" ;
/* End of details */

$recipients = array();
while($row = mysqli_fetch_array($result)) {
    $recipients[] = $row["email"];
}

$to .= "[email protected]";

$subject .= "Domain.com account notification";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: " . $name . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "Bcc: ". implode(", ", $recipients) ."\r\n"; <-- Error happens here
$headers .= "X-Mailer: PHP/" . phpversion(); 

$message .= '
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Domain.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
</body>
</html>
';

$send_email = mail($to, $subject, $message, $headers);

if(!$send_email) {   
    echo "Error<br>";
    echo implode(', ', $recipients);
} else {
    echo "Success<br>";
    echo implode(', ', $recipients);
}
share|improve this question
3  
What is the error ? –  serenesat 19 hours ago
    
$headers .= "Bcc: ". implode(", ", $recipients) ."\r\n"; <-- Error happens here –  HereToHelpPHP 19 hours ago
1  
What error do you get? –  John Conde 19 hours ago
    
Have you checked your $recipients variable, does every element contain a valid e-mail address? –  jeroen 18 hours ago
    
Have you tried to remove the space in the implode ? implode(", ", $recipients) => implode(",", $recipients) –  Heru-Luin 18 hours ago

1 Answer 1

This is not a specific solution to your issue, but the PHP Mail function is really, REALLY poorly implemented and has various flaws and issues because 'emails' in general are as wide a topic as 'web pages', read some articles from sources such as mailchimp ( http://blog.mailchimp.com/background-images-and-css-in-html-email/ ) which touch on the subject of emails in general and specifically HTML emails, but as well as this there is a wealth of issues around the email headers which need to be PERFECTLY right to enable delivery.

It is a huge minefield and if I wasn't such a proud programmer I would often have opted for Mailchimp or similar.

But, there is a solution. Look up PHPMailer ( https://github.com/PHPMailer/PHPMailer ) and download and install this, I have found it to be 100% effective for my PHP code and delivers the email EVERY time, with clear error description and reporting.

I would qualify this by saying that sending any email - you need to specify a valid FROM server and FROM name/email address.

Good luck

share|improve this answer
    
this sounds like a plug but this is not an advert. I really like PHPMailer. Explore it –  Martin 17 hours ago
    
Okay thank you. I will take a look at it. Best, –  HereToHelpPHP 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.