Forum Moderators: coopster

Message Too Old, No Replies

sending a great deal of email via PHP and MySQL

mail() mass-email mysql

         

andylarks

10:40 am on Oct 7, 2003 (gmt 0)

10+ Year Member



I am trying to send a single email to a couple of thousand recipients from our database. Being fairly new at this, I am quite sure that I'm just being a bit thick...

To begin with, I looped through sending a mail to each recepient, which worked fine for small numbers, but large number causes a timeout.

I therefore changed it to this:

<snip>

$query = "SELECT * FROM mailing_list;";
$result = (mysql_query($query));
$no_entries = mysql_num_rows($result);
$subject = $_POST['subject'];
$contents = $_POST['contents'];

// post it to me initially
$to = "me@my.dns.uk";

// create the rest of the mail header
$headers = "From: me@my.dns.uk\r\n";
$headers .= "Reply-To: me@my.dns.uk\r\n";
$headers .= "Return-Path: me@my.dns.uk\r\n";

// now BCC it to everyone else
$headers .= "BCC: ";
$x=0;
while ($row=mysql_fetch_assoc($result)) {
if ($x==0) {
$headers .= $row['address'].",";
$x++;
} else if ($x==1) {
$headers .= $row['address'];
$x++;
}
$headers .= ",".$row['address'];
}
$headers .= "\r\n";

// we will need an 'unsubscribe' footer
$footer = "Please note: If you wish to unsubscribe, click on the following web link:";
$y = "\n------------------------------------------\n\n";
$y .= $footer."\nhttp://my.url.uk/unsubscribe.php?ID=".$row['ID']."\n";
$y .= "or email me@my.dns.uk with the subject \"Unsubscribe\".";
$y .= "-------------------------------------------\n\n";

// put the body of the mail together
$email = $contents.$y;

// now send it
if (mail($to,$subject,$email,$headers))
echo 'The email was successfully sent.';
else
echo "Sorry, there was an error, the message has not been sent";

<snip>

Any ideas?

Thanks

Andy

Timotheos

5:51 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So in your example you're putting thousands of addresses in the bcc. I'm sure there must be a limitation on the length of the mail header.

Going back to your original method you might want to look at opening a socket connection to sendmail. There's a very interesting example in the user contributed notes [php.net] on the php web site by grey at greywyvern dot com.

dmorison

5:54 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can stop PHP from timing out by using...

set_time_limit(0);

...as the first line of your script.

Then go back to your first method...!

rincey

1:47 pm on Oct 8, 2003 (gmt 0)

10+ Year Member



Please note that set_time_limit() does not work if PHP runs in safe_mode (which can be the case with some providers).

You could try to make a for-loop which reads 10 adresses, sends out a mail() using bcc:, marks these 10 as sent and then calls itself (with Javascript or header()) to repeat the same procedure for the next 10 entries of your list.

Ad

bcolflesh

1:50 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll greatly increase the speed and reliability of sending email by using one of the SMTP transfer scripts - ex. PHPmailer:

phpmailer.sourceforge.net/

andylarks

2:26 pm on Oct 8, 2003 (gmt 0)

10+ Year Member



I forgot to mention that this is on Windoze, IIS

bcolflesh

2:30 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On IIS, make sure the SMTP service is started and send mail through it with PHPmailer.

andylarks

10:30 am on Oct 9, 2003 (gmt 0)

10+ Year Member



I tried using the

set_time_out(0);

with my while loop, which seems to work, but gives me this message:

Warning: mail() [function.mail]: SMTP server response: 503 5.5.2 Need Rcpt command. in C:\Inetpub\wwwroot\WEDC_Intra\do_email.php on line 97

Line 97 is the mail() function

Any ideas?

And Bcolflesh, I will take a look at PHPMailer, thanks

andylarks

10:31 am on Oct 9, 2003 (gmt 0)

10+ Year Member



oops, I meant set_time_limit(0)

:*>

coopster

4:12 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is your PHP version < 4.3? You may need to consider your mail function [us4.php.net], since you are on a Windows server:
...the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP. PHP < 4.3 only supported the Cc: header element (and was case-sensitive). PHP >= 4.3 supports all the mentioned header elements and is no longer case-sensitive.

mykel

11:02 pm on Oct 18, 2003 (gmt 0)

10+ Year Member



So in your example you're putting thousands of addresses in the bcc. I'm sure there must be a limitation on the length of the mail header.

I would send each email individually. There are a couple of reasons:
1.some mail filters treat an email where you have a couple of thousand recipient addresses as spam
2.some filters treat an email with "To: undisclosed recipient" as spam
3.If you send them seperately, everyone gets an email addressed only to him - looks a lot nicer.

konduct

5:21 pm on Oct 26, 2003 (gmt 0)

10+ Year Member



I used phpmailer and MySQL in a custom newsletter program and it is much more robust than PHP's default mail() function. On a single system it was sending out 250 opt-in emails concurrently until it ran to the end of the list, which lasted about 15 minutes. My favorite phpmailer feature is the MX failover capability, and the ability to handle MIME attachments.