Bulk Email Sender
\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
// Define message body with HTML support
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=UTF-8\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message . "\r\n";
// Include attachment if available
if ($attachmentEncoded) {
$body .= "--$boundary\r\n";
$body .= "Content-Type: application/octet-stream; name=\"$attachmentName\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"$attachmentName\"\r\n\r\n";
$body .= $attachmentEncoded . "\r\n";
}
$body .= "--$boundary--"; // End boundary
$sentCount = 0;
$errorCount = 0;
// Send email to each recipient
foreach ($recipients as $email) {
$email = trim($email);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // Validate email format
if (mail($email, $subject, $body, $headers)) {
$sentCount++;
} else {
$errorCount++;
}
} else {
$errorCount++;
}
}
// Display success or error message
if ($sentCount > 0) {
echo "
Successfully sent $sentCount emails.
";
}
if ($errorCount > 0) {
echo "
$errorCount emails failed to send.
";
}
}
?>