Leo
Leo

Reputation: 239

php mail function multiple emails and mysql_fetch_array

Yes I am very beginner in coding.

So what I am doing is first I get email address (from db) on which a user want to receive an email

Just like

$query = mysql_query("Select * FROM receivers WHERE id=$id")
    or die(mysql_error()); 

while ($grabit = mysql_fetch_array($query)) {

    $iAmReceiver = $grabit['email'];
}

So if a user have one email that will be simply stored in $iAmReceiver but what if a user have many email addresses for receiving emails?

Also next I use php mail function to send email, for one email address I can simply do

$ToEmail = "$iAmReceiver;
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

So sending mail at multiple email addresses directly depends how I store multiple email addresses??..... Something like array??

Upvotes: 0

Views: 923

Answers (2)

GBD
GBD

Reputation: 15981

You can create a comma-separated email like this:

while ($grabit = mysql_fetch_array($query)) {
    $iAmReceiver .= $grabit['email'].',';
}

$iAmReceiver = rtrim($iAmReceiver,',')

$ToEmail = "$iAmReceiver";

//You can pass comma separated email id as first argument of email if you want to send mail on multiple emails

 mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

Upvotes: 1

Sabeen Malik
Sabeen Malik

Reputation: 10880

I am guessing something like this should work

while ($grabit = mysql_fetch_array($query)) {
    $receivers[] = $grabit['email'];
}

$ToEmail = implode("," , $receivers);

Also please consider moving upto mysqli_* or pdo. mysql_* are deprecated.

Upvotes: 2

Related Questions