How can I divide long list of e-mails?

the mail() function

Moderators: macek, egami, gesf

User avatar
ameenov
New php-forum User
New php-forum User
Posts: 70
Joined: Mon Jun 13, 2005 2:03 am
Location: Bahrain
Contact:

How can I divide long list of e-mails?

Postby ameenov » Mon Jun 13, 2005 2:14 am

I have about 1000 members and i made a mail file to send emails to them but the file sends the message all onr time
I want it to send mail divided to 10 by 10 or 50 by 50 until it reaches the 1000 (for example)

How can I do that?

the code I used ..

Code: Select all


<?php
$Select = mysql_query("SELECT email FROM users")Or Die(MySQL_Error());

//get

while ($row = mysql_fetch_array($Select)) {

/* what is $to?
all emails*/
$to = $row[email];

//send
$Send = mail($to, "$from" ,"text");
}

//see if sent
if ($Send) {
echo "
<p>&nbsp;</p>
<p><strong><font color='#000099' size='5'>E-mails sent!!</font></strong></p>

";
}
else {
echo "
<p>&nbsp;</p>
<p><strong><font color='#FF0000' size='5'>Error, E-mails not sent
</font></strong></p>
";
}
exit;

?>

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Mon Jun 13, 2005 6:26 am

the To argument may be an array.. but this will show email addresses... use Bcc header and give a list of all email addresses (string) to it :)

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Mon Jun 13, 2005 6:26 am

Forgot to tell you that this will require calling SEND only once :Di think you are going to love it that way :)

User avatar
ameenov
New php-forum User
New php-forum User
Posts: 70
Joined: Mon Jun 13, 2005 2:03 am
Location: Bahrain
Contact:

Postby ameenov » Wed Jun 15, 2005 12:11 am

Thanks
Alexei Kubarev

But can you explan it more? :o
I never used an array before .. but i want to

if you can modify the code above it would be a great help
also I will get the idea easier.

thanks in advance

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Wed Jun 15, 2005 1:34 am

I actually think that using a string of lists to a Bcc header is better as you do not have to worry about your users seeing other addresses..

Code: Select all

<?php
$Select = mysql_query("SELECT email FROM users")Or Die(MySQL_Error());

//No fancy tricks here, simply headers:
$headers = "From: Me<no-reply@myhost.com>\r\n";

$row = mysql_fetch_assoc($Select); // We are getting the first value manually
//Reason: so that we dont have to make some logick for the sepearating comma in the loop

$headers .= "Bcc: ".$row['email'];
while ($row = mysql_fetch_assoc($Select)) // NOTE: use assoc instead of array. always
{
   $headers .= $row['email'];   // use appending operator .=
}

$headers .= "\r\n"; // Simply ending the headers list

$to = ""; // You may set some fake address or even a string like "Undisclosed addresses"
$Send = mail($to, $from ,"text", $headers); //$from should actually be your Subject, from is ALWAYS set in headers

?>


Please take a look at php.net for more inofrmation on custom headers and mail function :)

User avatar
ameenov
New php-forum User
New php-forum User
Posts: 70
Joined: Mon Jun 13, 2005 2:03 am
Location: Bahrain
Contact:

Postby ameenov » Fri Jun 17, 2005 10:30 pm

Thank you very very much
But i have a question

Is this code going to divide the result of the database to send 10 emails and send the other emails like 10 by 10?

because I will need that also

thank you again :)

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sat Jun 18, 2005 2:23 am

ahh... that code actually sends one single email to different people.. lol... it uses Bcc so that noone will see other addresses...
but wwhy would you want to devide?

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Sat Jun 18, 2005 4:26 pm

I think i know why ameenov2 is saying that.
I'm not sure (however i believe it's true), but i think there's a limit of email addresses we can pass in both Cc or Bcc fields.
What you can say about that Alexei ?

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sun Jun 19, 2005 5:51 am

well... i dont know... my system for a webshop sends about 150 emails in Bcc without any problems...
Will take a better look at it :)

User avatar
ameenov
New php-forum User
New php-forum User
Posts: 70
Joined: Mon Jun 13, 2005 2:03 am
Location: Bahrain
Contact:

Postby ameenov » Tue Jun 21, 2005 12:00 am

thank you for your response
you are really a great help


why would you want to devide?


I think i know why ameenov2 is saying that.
I'm not sure (however i believe it's true), but i think there's a limit of email addresses we can pass in both Cc or Bcc fields.
What you can say about that Alexei ?

will i don't have this problem but I'm considering it :D
Also It will be a big load on the server, i don't want that to happened

For example if you have a huge members databse it will be a problem to send about 1000 or 2000 emails one time.

Anyway, have you seen (vbulletin) forum's control panel?
When the admin sends emails to his members it will send 50 emails pre page ( the page redirected every 50 emails sent )

Got the idea?

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Tue Jun 21, 2005 12:08 am

hmmz okej... big load on server it will not be... its simply one single email... not 1000 email... but ONE and SMTP servers will simply have to deliver it to those addresses in BCC header... but i think bcc really has the max amount of addresses in the header... still need to check it up

User avatar
ameenov
New php-forum User
New php-forum User
Posts: 70
Joined: Mon Jun 13, 2005 2:03 am
Location: Bahrain
Contact:

Postby ameenov » Tue Jun 21, 2005 4:51 am

big load on server it will not be... its simply one single email... not 1000 email... but ONE and SMTP servers will simply have to deliver it to those addresses in BCC header


New thing to know :D
So it is ok to send so many mails

but i think bcc really has the max amount of addresses in the header... still need to check it up


Ok take your time

and thank you soOoOoOoOo MUCH :)

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Tue Jun 21, 2005 4:57 am

:) i will make a small research on it... and will get back to you asap

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Fri Jun 24, 2005 7:18 pm

Ok,

To, Cc, Bcc and (almost all) address' lines like From and Reply_to... can have up to 1024 characters in length.
It's configurable, but... i bet nobody touch this!

So, considering 1024 character lenght and using a short email address like: me@aol.com (10 characters) for all of the 1000 users:

10 chrs x 1000 addrs = BOOM!
Guess what ? We have 1000 chrs < 1024 if we send 10 emails.

Assuming that not every user's email address is that short, you'll probably need to send up to 10 emails.... or not!
However, i don't know where that up to 1024 characters value goes to!
Anyways... 5, 10, 15!? Wont be that big server load :P

Maybe a better idea is sending emails but/and filling the Bcc address line til 1024 character long (dont brake any email at middle) and til there's no email address left.

Well... this is all theory and also i never saw any massive email system code, so... you better check some BBS or whatever else code :P

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Fri Jun 24, 2005 11:45 pm

okey..well: mailing systems do use BCC.. and i never saw it splitting the emails and excecuting several sendings..
well... hmmz... gonna talk to BBC developer, wanna see what he has to say as i think they have something simmilar..

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Mon Jun 27, 2005 2:00 am

okey..well: mailing systems do use BCC.. and i never saw it splitting the emails and excecuting several sendings..
Agree 100%!
It's just amazing how that addresses' lines do have a max characters in length.
This is a different case, anyways, also sotfwares like OUTLOOK can't send several emails. I think 50 is its max number of addresses for Bcc and Cc. :P Why not more ?

Well, i don't know anyone who can really explain this to me, so... i'm waiting your answer Alexei!

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Mon Jun 27, 2005 2:12 am

Yeah, like im some kind of master in this :)
Im still getting my master degree in IT :P haha

But the thing is that php is its own mail agent. I might be a BAD searcher but im pretty sure that my freaking mailing class doesnt split ANYTHING and ive sent tons of emails with lots of addresses in BCC header and all went through.. well, all im aware of.

I still dont really get the problem with the max length.. but i never succeeded in getting some emails not sent :S


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest

cron