Page 1 of 1

How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 10:17 am
by mandingueiro
Hello dudes! :mrgreen: , it's my first post here so I may be a little out-of-rules :crazy:

Well, I'm building a script that will read email addresses from a database (can reach 1000 addresses or more) and now I want to make it to send an email to all of them individually. I don't want to use cc or Bcc fields because some mail servers filter them and they'll never receive them. I've red that the mail() function is not the proper way to do this cause it can break/time out (which makes sense).

So what way do you suggest me to follow to achieve this?
Thanks in advance!

Panagiotis

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 12:24 pm
by Mark Baker
What's wrong with using the mail function?

Quoted directly from the examples in the PHP Manual entry for the mail() function

Code: Select all

 
<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';
 
// subject
$subject = 'Birthday Reminders for August';
 
// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';
 
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
 
// Mail it
mail($to, $subject, $message, $headers);
?> 
 

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 12:34 pm
by mandingueiro
I know about mail() function but as I said before, it will timeout when using it for hundreds of mails. For example if I put a

Code: Select all

for ($i=0;$i=1000;i++) {
mail($to, $subject, $message, $headers);
}
it will propably crash/time out.

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 1:59 pm
by requinix
Consider using a mailing service to do the work for you.

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 2:32 pm
by mandingueiro
tasairis wrote:Consider using a mailing service to do the work for you.
I want to make the script myself that's why I came here for help. I simply want to learn how to make this in batch php and send 10 mails each time.

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 5:26 pm
by califdon
mandingueiro wrote:
tasairis wrote:Consider using a mailing service to do the work for you.
I want to make the script myself that's why I came here for help. I simply want to learn how to make this in batch php and send 10 mails each time.
You could certainly do that. If you have control of the configuration of the SMTP server that you're using, you should be able to set the timeout, but if not, you could code an inner loop that sends, say, 25 emails, then pauses for a few seconds and sends another 25. I have a monthly mailing of about 30, using my web host's SMTP server, and I've never had a time-out problem. If you're asking about the syntax of such a loop, I would try something along these lines:

Code: Select all

...
$done=0;
while($done=0) {
    for($i=0;$i<25;$i++) {
        while($row=mysql_fetch_array($result)) {
            //
            // assemble your email headers, Subject and Body data
            //
            mail($to,$subject,$message,$headers,$parameters);
        }
        if(!$row) {
            $1=25;
    }
    if($row) {
        sleep(10);
    } else {
        $done=1;
    }
}
(I haven't tested that, it might have an error.)

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 5:28 pm
by pickle
What are these emails for? I'm loath to give advice to someone who's going to use my help to SPAM me.

Re: How to mail multiple recipients without using cc or bcc

Posted: Mon Dec 08, 2008 5:35 pm
by mandingueiro
Thanks. sleep() is what I was searching for... :)