Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-22-2007, 07:41 AM   #1 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Question Anyway to make this faster?

Hi.

I've been given the un-enviable job of writing a script to send 5000 emails to the addresses in a database. At the moment I'm uing this but it seems to take forever:

PHP Code:
<?php

mysql_connect
("###############","##","##");
@
mysql_select_db("##");

$query trim(stripslashes($_POST['query']));

$result mysql_query($query);

while (
$row mysql_fetch_array($result)) {
    
$msg wordwrap($_POST['message'],70);
    
$msg nl2br(trim(stripslashes($msg)));
    
$sub nl2br(trim(stripslashes($_POST['subject'])));
    
$msg preg_replace("`\[n\]`",$row[1],$msg);
    
$sub preg_replace("`\[n\]`",$row[1],$sub);
    
$msg preg_replace("`\[e\]`",$row[0],$msg);
    
$sub preg_replace("`\[e\]`",$row[0],$sub);
    
$msg preg_replace("`\[d\]`",date("d-m-Y"),$msg);
    
$sub preg_replace("`\[d\]`",date("d-m-Y"),$sub);
    
$msg preg_replace("`\[t\]`",date("g:i A"),$msg);
    
$sub preg_replace("`\[t\]`",date("g:i A"),$sub);
    
$msg "Can't read this? Plain text email:<br>\n".$_POST['plaintext']."<br>\r\n<br>\r\n".$msg;
    echo(
"Sending to ".$row[0]."... ");
    
$headers  'MIME-Version: 1.0' "\r\n";
    
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
    
$mail mail($row[0],$sub,$msg,$headers);
}
?>
What can I do to make it faster?
markster is offline   Reply With Quote
Old 04-22-2007, 08:43 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Why not build the message outside the while loop, I mean only the addresses come from the database right?
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 04-22-2007, 08:58 AM   #3 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
I gave that a try and made it tell me for each one if it sent or not. it manages the first 300 exactly then starts failing
markster is offline   Reply With Quote
Old 04-22-2007, 09:40 AM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
It could be that your outgoing smtp server has a flow control cap to prevent spamming.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 04-22-2007, 12:46 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
yeah, most web apps that do stuff like this will send 10-100 emails at a time. you could store the count of the email addresses in a session variable and then use a meta refresh on the page to only send x number of emails at a time. when you print the meta refresh, it will print a GET variable of where to start.

for example, your script maybe: mymailer.php .. so this script will first query the db for the total number of email addresses, store it in a session variable, send out the first 10 emails, and then print a meta refresh with a start variable incremented by 10.

here's some example psuedo code that is untested and not meant for use:
PHP Code:
<?php
// start the session
session_start();

// set how many emails you want to send per page
$limit 20;

// if the count has not been set, then let's set it
if (!$_SESSION['count']) {
  
$result mysql_query("select count(*) from users");
  
$_SESSION['count'] = mysql_result($result,0,0);
  
  
// since we wouldn't have run this before, let's
  // set the starting point.  later this will be set in the url
  
$_GET['start'] = 1;
}

// now we wil run the query with the start and limit vars
$result mysql_query("select email from users limit ".$_GET['start'].", ".$limit);
while (
$rowmysql_fetch_assoc($result)) {
  
// send out the email here
  // maybe echo the people getting emailed here too
}

// now lets print out a meta refresh to the browser so
// the page will refresh and query a new set of results.
// only do this if we haven't reached the end yet.
if ($_GET['start']+$limit $_SESSION['count']) {
  echo 
'<meta http-equiv="refresh" content="2;url=mymailer.php?start='.$_GET['start']+$limit.'">';
} else {
  echo 
"Mail complete";
}
?>
__________________
Mike
sde is offline   Reply With Quote
Old 04-23-2007, 05:53 AM   #6 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
in the meta refresh should it not be mymailer.php?start=$_GET['start']+$limit and not $_GET['start']+10?
markster is offline   Reply With Quote
Old 04-23-2007, 06:37 AM   #7 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
hmm, im using this:

PHP Code:
echo('<meta http-equiv="refresh" content="2;url=step2.2.php?start='.$start+$limit.'">'); 
but it only prints out 5">
markster is offline   Reply With Quote
Old 04-23-2007, 07:28 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
yes, that was a mistake.

what prints out 5?
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Include file shown correctly in FF not in IE Stoner HTML, XML, Javascript, AJAX 2 04-21-2006 07:56 AM
Make a search engine for your website with PHP zhisede PHP 3 10-25-2005 07:54 AM
IBM urges Sun to make Java open source redhead Code Newbie News 0 03-01-2004 10:39 PM
Intel unveils faster silicon chip redhead Code Newbie News 0 02-12-2004 10:37 PM


All times are GMT -8. The time now is 03:23 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting