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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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~

Link to comment
Share on other sites

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  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.