0
 $sql = "select emailid from table where category = 1";
    while($row=mysql_fetch_array($sql))
    {
            $email=$row['emailid'];
            $to = $email;

            $subject = "E-mail subject";
            $body = "E-mail body";
            $headers = 'From: [email protected]' . "\r\n" ;
            $headers .= 'Reply-To: [email protected]' . "\r\n";
            mail($to, $subject, $body, $headers);
    }

Suppose the above code will fetch 100 e-mail ids from database and send the mail one by one to each e-mail id. but what i want to do is.... fetch all e-mail ids in an array and send them as 'BCC' to each e-mail at once. one more thing i want to customize the e-mail body content for each id....

Any ideas?

1
  • Programming without any API is not possible. Commented Mar 28, 2012 at 15:08

1 Answer 1

1

What you want to do is impossible. If you send them at once via multiple addresses in To/CC/BCC, everyone gets exactly the same email. So you cannot customize the bodies.

If sending the same email to everyone is fine, try this:

$sql = mysql_query("select emailid from table where category = 1");
$recipients = array();
while($row = mysql_fetch_array($sql)) {
    $recipients[] = $row['emailid'];
}

$to = '[email protected]';
$subject = "E-mail subject";
$body = "E-mail body";
$headers = 'From: [email protected]' . "\r\n" ;
$headers .= 'Reply-To: [email protected]' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";

mail($to, $subject, $body, $headers);
Sign up to request clarification or add additional context in comments.

5 Comments

2) what if i want to send mail to 1000 e-mail ids... 3) will it be possible to send a e-mail in groups of 50 - 50 or 100 - 100 e-mail ids...
4) and one more thing i want to schedule this script so that if i run the same script from different computers it checks if the script is already running or not, if it is running it waits for the completion of first and then execute...from all above points i mean to say i want to reduce the load on server
Yes of course, you can simply call the code to send an email every x iterations or array_chunk the final recipient list. To remove the to many people set it to something like undisclosed-recipients (no idea if it's standard-conforming though)
The proper way would be starting it via a cronjob. If that's not available you could create a lock file when the script starts and delete it when it finishes. Don't forget to add a way to delete it manually though in case something goes wrong and it's never deleted by your script.
ok. i am using plesk on windows server... i have a option 'schedule task ' in the panel i used it to schedule the script's execution...its working fine... the thing is i am not getting what you exactly want to say... secondly i don't want to schedule the script like a cronjob what i want to do is.. if 5 users simultaneously call the mailing Script it must not execute simultaneously the execution must be one after one .. you can see it as a first come first serve methodology...if your answer above will do the same thing...will you please elaborate it for me...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.