I have this code:
<?php include("db.php"); $result = mysql_query("SELECT * FROM email"); while($row = mysql_fetch_array($result)) { $to = $row['address']; } $subject = "Test mail"; $message = "Hello! This is a simple email message."; $from = "[email protected]"; $headers = "From:" . $from; mail($to,$subject,$message,$headers); ?>
In my table ("email") I have multiple addresses. (They are not comma sepparated.) How could I send my message to all of those addresses?
To send an email to multiple recipients in PHP, we simply separate the emails using a comma. For example, mail("[email protected], [email protected]", SUBJECT, MESSAGE); To add CC and BCC in PHP mail, we have to manually set the “Cc” and “Bcc” fields in the mail headers.
The BCC methodThe BCC (Blind Carbon Copy) method is the most common approach to send emails to multiple recipients at the same time. Emailing to multiple recipients using the BCC feature hides other recipients from the recipient, making it look like he is the sole recipient of the email.
PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );
to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>
while($row = mysql_fetch_array($result)) { $addresses[] = $row['address']; } $to = implode(", ", $addresses);
As specified on the mail()
manual page, the "to" parameter of the function can take a comma-separated list of addresses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With