How to mail multiple recipients without using cc or bcc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mandingueiro
Forum Newbie
Posts: 9
Joined: Fri Oct 03, 2008 6:46 pm

How to mail multiple recipients without using cc or bcc

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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);
?> 
 
mandingueiro
Forum Newbie
Posts: 9
Joined: Fri Oct 03, 2008 6:46 pm

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Consider using a mailing service to do the work for you.
mandingueiro
Forum Newbie
Posts: 9
Joined: Fri Oct 03, 2008 6:46 pm

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

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mandingueiro
Forum Newbie
Posts: 9
Joined: Fri Oct 03, 2008 6:46 pm

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

Post by mandingueiro »

Thanks. sleep() is what I was searching for... :)
Post Reply