0

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??

2
  • In the database model your code is suggesting, it might be very unlikely that your query returns multiple lines, assuming id is your primary key or at least unique. Commented Oct 10, 2012 at 16:11
  • @ChristianBock actually it's a seperate table for receivers and I have coded it in such a way that whenever a user may add a new email address it will add a new row in db with the userid and email address. Don't know people do so or not but somehow it works..... Commented Oct 10, 2012 at 16:18

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Unexpectedly---worked PERFECTLY.... I would've done it myself if these stupid mosquitoes stop eating me up.
This is a perfect recipe to completely piss off your users by exposing their emails to everyone. Consider having the email sent to yourself with your users in the BCC field or else be prepared to have some very angry users.
@rickclader Oooops,....you just tried to send a call back to my worries,... But in my case the other email addresses will be of the same user and surely they will not mind,..... :P
@RickCalder I believe he stated that ... but what if a user have many email addresses for receiving emails? which means they are the emails of the same person.
Right. Same user multiple emails, fair enough.
1

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.