Jump to content

[SOLVED] loop inside a .txt file (migrating from sql)


johnwayne77

Recommended Posts

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?

 

 

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
}
?>

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
}
?>

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.

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.

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~

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.