johnwayne77 Posted March 24, 2009 Share Posted March 24, 2009 ok, this is my situation: i have my newsletter database on mysite1.com (which i can export to a .txt file) the inconvenient is that i cannot send more than 200 messages per hour from mysite1.com so i decided to move the newsletter system to mysite2.com where i can send 500+ messages. my current newsletter was gathering mails from sql but now i have to loop into a .txt file for mails and send mail by mail.. i don't have any idea how to do that.. here are some parts of my current newsletter: $getlist="SELECT * from news where inactiv = '0' order by email ASC"; //select e-mails in ABC order $getlist2=mysql_query($getlist) or die("EROARE: mysql"); while($getlist3=mysql_fetch_array($getlist2)) { mail(....) } how would i do the same thing while replacing sql with a txt file? Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/ Share on other sites More sharing options...
Maq Posted March 24, 2009 Share Posted March 24, 2009 Does your txt file already have the emails? If the contents are in the .txt file and separate by newlines then you can use the file() function: $lines = file('email.txt'); foreach ($lines as $line_num => $email) { echo "email# $line_num: " . $email . " "; //your mail function } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-792833 Share on other sites More sharing options...
johnwayne77 Posted March 24, 2009 Author Share Posted March 24, 2009 nice, it works! now i'll try to implement a sleep() solution to bypass the limit thanks Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-792846 Share on other sites More sharing options...
Maq Posted March 24, 2009 Share Posted March 24, 2009 Not tested but every 10 emails it should wait 10 seconds. $delay_time = 10; $delay_emails = 10; $lines = file('email.txt'); foreach ($lines as $line_num => $email) { if($line_num % $delay_emails == 0) sleep($delay_time); echo "email# $line_num: " . $email . " "; //your mail function } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-792858 Share on other sites More sharing options...
johnwayne77 Posted March 24, 2009 Author Share Posted March 24, 2009 nice.. any idea calling the script via exec() ? Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-792978 Share on other sites More sharing options...
johnwayne77 Posted March 24, 2009 Author Share Posted March 24, 2009 what about importing the mails.txt from another site ... file_get_contents doesn't work any substitute for file() ? Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-792995 Share on other sites More sharing options...
Maq Posted March 24, 2009 Share Posted March 24, 2009 nice.. any idea calling the script via exec() ? Is there a problem? You should be able to call the script in the command line with: shell_exec("php script_name.php &"); (The '&' is to run in the background, you can remove it if you want) what about importing the mails.txt from another site ... file_get_contents doesn't work any substitute for file() ? The txt file is probably behind a folder you can't access... I have a script at home I use, but I'm not sure off hand, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-793001 Share on other sites More sharing options...
johnwayne77 Posted March 24, 2009 Author Share Posted March 24, 2009 uhm.... works with file() but doesn't work with file_get_contents and curl... Warning: Invalid argument supplied for foreach() in /home/metropp0/public_html/newslettertest/sendletter.php on line 58 that's the error Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-793019 Share on other sites More sharing options...
Maq Posted March 24, 2009 Share Posted March 24, 2009 That's because file_get_contents() returns a string (of the ENTIRE file), NOT an array (of EACH line), so you can't call foreach on it. You would have to do file_get_contents, put it in a txt file on your server, then call file() on that txt file. Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-793024 Share on other sites More sharing options...
johnwayne77 Posted March 24, 2009 Author Share Posted March 24, 2009 this is the solution: Alternative for file() Instead of: <?php $lines = file('http://example.com/'); // display file line by line foreach($lines as $line_num => $line) { echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n"; } ?> Use this: <?php $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://example.com'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); $lines = array(); $lines = explode("\n", $file_contents); // display file line by line foreach($lines as $line_num => $line) { echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n"; } ?> found it at https://www.clan-solutions.com/support/index.php?_m=news&_a=viewnews&newsid=5 exact what i needed. thanks and cheers~ Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-793026 Share on other sites More sharing options...
Maq Posted March 24, 2009 Share Posted March 24, 2009 Nice, that's similar to what I said except using cURL, and just exploding the returned string rather than dumping it into a file and opening it back up. Regardless, glad to see it working Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-793028 Share on other sites More sharing options...
johnwayne77 Posted March 24, 2009 Author Share Posted March 24, 2009 im using your method anyways (except the curl) Quote Link to comment https://forums.phpfreaks.com/topic/150917-solved-loop-inside-a-txt-file-migrating-from-sql/#findComment-793031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.