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);
}
$recipients
variable, does every element contain a valid e-mail address? – jeroen 18 hours ago