/    Sign up×
Community /Pin to ProfileBookmark

sending multiple mails with mail() function

I have a question to do:

I have a table in MySQL database where storing all mail accounts of my website.
Can i send email (like newsletter) to all mail accounts via mail() function or the mail server cut me?(may have many accounts, 300…400…1000 mails)

The PHP code:
<?
for($k=0;$k < mysql_num_rows($result_users);$k++)
{
$row_user=mysql_fetch_array($result_users);
$to=stripslashes($row_user[‘mail_name’]);

echo ‘sending to ‘.$to.’ !!<br>’;
if(!mail($to, $subject, $html, $headers))
{
echo “No mail send!! to $to”;
flush();
sleep(1);
//exit();
}
flush();
set_time_limit(30);
sleep(2);
}//end for($k=0;$k<mysql_num_rows($result_users);$k++)

Please, tell me your opinion,
Thanks a lot for your time..
?>

[code=php]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@yunaMay 21.2005 — [This message assumes *nix platforms, not Windows.]

I've found that sending lots of messages with the PHP mail() function can be extremely slow, since mail() has to submit the message to the sendmail program and await its delivery before moving onto the next message. A much better approach for handling lots of emails is to "pipe" the message directly to sendmail with the appropriate command-line switches required to place the message into the outgoing queue.

Here's a sample:
[code=php]
# who the Sender address should be (the SMTP "envelope sender")
# note this can be different from the From that appears in the message
# headers and is displayed to the recipient
# using unique sender addresses helps in handling bounces
$sender="[email protected]";

$from="[email protected]";
$to="[email protected]";
$subject="Free herbal VIAGRA";
$message_body=
"Try this NOW!!!!!!!

";

$sendmail="/usr/sbin/sendmail -odq -t -i -f$sender";
$message=
"Sender: $sender
From: $from
To: $to
Subject: $subject

$message_body
";
# mail the message (open a "pipe" to the sendmail program;
# messages are queued for later delivery)
$sm=popen($sendmail,"w");
fputs($sm,$message);
pclose($sm);

[/code]


Essentially all I'm doing is composing the message manually and passing the result to sendmail. The "-odq" command-line switch tells sendmail to put the message into the queue for later processing. ("-t" tells it to look in the message body to find the recipient from the To: field; "-i" tells it not to consider a dot on a line by itself to denote an end-of-message; the "-f" is obviously the sender.) Because the PHP script just queues the messages, it's much faster than waiting for each message to be delivered.

You need to make sure that sendmail is configured to check the queue periodically for unsent messages. Most default sendmail installations will use the "sendmail -bd -q1h" type of command at startup which checks the queue every hour. Replace "1h" with "15m" in your Unix startup scripts if you want fifteen minute intervals.
×

Success!

Help @valsamo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...