Click to See Complete Forum and Search --> : Read from TXT instead MYSQL
Extreme
07-05-2003, 03:53 AM
I must post this thread in a new post because the last one where I mentioned this has jumped of the subject and no one is responding... Anyway, found this script that has everything I need.. It has option to limit how many emails will be sent at once, and therefore timeout will not occur. Then, it logs how many messages are sent, but has one "fault".. It reads a list of recipient from SQL database... I need it to read a list from a regular text file... Here is the script that needs to be modified...
<?php
$host = "localhost"; //Host
$user = ""; //Username
$pass = ""; //Password
$database = ""; //Database name
$table = ""; //Name of table that has list of emails
$column = ""; //Name of column in table that has list of emails
$num_per_loop = "5"; //Number of emails sent per page loop (keep number low to avoid page time-out)
$from_name = "Jason Rottman (Developer)"; //Reply address email address
$from_email = ""; //Name the emails came from
$mail_subject = "This is the subject of the message";
$mail_body = "This is the body of the message (you can use html)";
$send_html_messages = "yes"; //yes - Send html messages, no - Do not send html messages
//////////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE ANYTHING BELOW THIS LINE //
//////////////////////////////////////////////////////////////////////////////
if (($first_time == "yes") || ($first_time == "")) {
$total_sent = "0";
$total_time = "0";
$start = "0";
$num_to_end = $num_per_loop;
}
if ($first_time == "no") {
$start = $start;
$num_to_end = $num_per_loop;
}
$mydb = mysql_connect($host,$user,$pass);
if (!$mydb) { echo "Error connecting to database"; die; }
mysql_select_db($database);
$varx = $start + $num_to_end + 1;
$result2 = mysql_query("SELECT " . $column . " FROM " . $table . " LIMIT " . $start . ", " . $varx . "") or die(mysql_error());
$num_left = "0";
$clock_start = number_format(microtime(),3);
while(list($email_addr) = mysql_fetch_row($result2)) {
$num_left = $num_left + 1;
}
$result3 = mysql_query("SELECT " . $column . " FROM " . $table . " LIMIT " . $start . ", " . $num_to_end . "") or die(mysql_error());
while (list($column) = mysql_fetch_row($result3)) {
$xheaders = "From: " . $from_name . " <" . $from_email . ">\n";
$xheaders .= "X-Sender: <" . $from_email . ">\n";
$xheaders .= "X-Mailer: PHP\n"; // mailer
$xheaders .= "X-Priority: 6\n"; // Urgent message!
if ($send_html_messages == "yes") {
$xheaders .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
}
mail("$column",
"$mail_subject",
"$mail_body",
$xheaders);
$total_sent = $total_sent + 1;
}
$clock_stop = number_format(microtime(),3);
$clock_difference = $clock_stop - $clock_start;
$total_time = $total_time + $clock_difference;
mysql_close();
if ($num_left > $num_per_loop) {
$next_start = $start + $num_per_loop;
//echo "<a href=\"" . $PHP_SELF . "?first_time=no&start=" . $next_start . "&total_sent=" . $total_sent . "&total_time=" . $total_time . "\">Next " . $num_per_loop . "</a>\n";
header("Location: " . $PHP_SELF . "?first_time=no&start=" . $next_start . "&total_sent=" . $total_sent . "&total_time=" . $total_time . "");
}
if ($num_left <= $num_per_loop) {
echo "FINISHED<br><br>";
echo "Total Emails Sent: " . $total_sent . "<br>\n";
echo "Total Script Run Time Used: " . $total_time . " seconds<br>\n";
echo "<a href=\"" . $PHP_SELF . "\">Run Again</a><br><br><br>\n";
echo "Provided By: <a href=\"http://www.rottmansales.com/jason/\">http://www.rottmansales.com/jason/</a><br>\n";
end;
}
?>
I tryed inserting this code
$contents = file($emailfile);
$num_lines = count($contents);
instead of the part wher it reads emails from SQL, and I deleted uneccessary parts, but I get errors. I probably deleted too much, or too little... Can you post the whole script valid?
diamonds
07-10-2003, 10:42 AM
from the looks of your script, it looks as if you are trying to e-mail someone. can you explain what it does?
diamonds
07-10-2003, 10:49 AM
never mind...
I found out what t does!
It is a mass mailer!
alright... I am working on it!
diamonds
07-10-2003, 11:19 AM
err--- a little too much for me.
Extreme
07-10-2003, 11:19 AM
Sorry I didn't reply sooner. Didn't see that you replyed... Well, it is a mass mailer, but very special one.. It is simple and yet, has everything I need: read recipients from file, prevention of timeout, statistics on sent mails... But, since I don't have SQl on my server, nore I know anything about it, the script must be changed so it reads recipients from TXT.. Also, it would be good if you could make some kind of form that will have BROWSE button and SEND button... BROWSE would be use to locate TXT file with email recipents....
Extreme
07-10-2003, 11:31 AM
This is a code to read emails from txt file...
$emailfile = "emails.txt"
$contents = file($emailfile);
$num_lines = count($contents);
mail($emailfile, ......)
Maybe it will help.... The biggest problem for me is to eject uneccessary SQL part....
diamonds
07-10-2003, 11:37 AM
I try to do my best, but I only just got startrd with PHP!
Well, this script is unique, because it refreshes the page every 5 e-mails.
i would not know how to do that...
mabye use the foreach() method and translate it to an array...
here is a script I made that uses that example to remove coments from a .txt file.
*opps* forgot to post the code :eek:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>comments</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p><code title="commented-out code">
<?PHP
$config = file('index.txt'); // URL of the config file
foreach ($config as $inc) {
$note = '#';
$pos = strpos($inc, $note);
if ($pos === false) {// not found
$included = $inc;
echo $included . "" ;
}else{// found
$to_include = substr($inc, 0, $pos);// trim comments
echo $to_include . "";
}
echo "<br>\n";
}
?>
</code><br>
And here is is the REAL file... <br>
<a href="index.txt">(link to the .txt file)</a><br>
<a href="source.txt">(source code)</a><br>
<br>
<code title="the REAL code">
<?php
foreach ($config as $inc2) {
echo $inc2;
echo "<br>\n";
}
?>
</code> </p>
</body>
</html>
diamonds
07-10-2003, 12:03 PM
this script converts a .txt file into a array...
<?php
$config = file('mail.txt'); // URL of the config file
$email = array();
$i = 0;
foreach ($config as $inc) {
$email[$i] = $inc;
$i++;
} ?>
and I also found this at http://www.atomar.de/public/code/debuglib/debuglib.demo.php(it was a link submitted onto www.php.net)
Extreme
07-10-2003, 12:40 PM
No need for that... My code I gave above works the same way, with less lines.. But the original problem was how to insert this into the code from the begining of the post, and ejecting all SQL related without getting errors... and getting script to work offcourse...
diamonds
07-10-2003, 04:02 PM
Well... If you cannot know what line of the file you are on, you cannot start from a spacific point in the file. If you convert it into an array, than you can figure out what line it is on. Why would you want to start on a single line? because PHP may take too long and time out, so you want to refresh every once in a while.
Oh, that script I had a link to shows variables when you call the right function. I use it to see if I have made an array correctly.
<?php
$config = file('mail.txt'); // URL of the config file
$email_a = '';//your email
$email_n = '';// your name
$msg_timeout = '';
$subject = '';//the subject of the message.
$body = "
Body here. You can include HTML if you have set it,
otherwise it is a text message
";
$email = array();
$i = 0;
foreach ($config as $inc) {
$email[$i] = $inc;
$i++;
} ?>
Oh just to let you know, this is not finished ;)
P.S. do you want to type the body of the message into a text file, too? I can do that!
diamonds
07-10-2003, 04:11 PM
Originally posted by Extreme
...prevention of timeout,...
Oh... you allredy said you wanted it :rolleyes: (silly me)
Originally posted by Extreme
...since I don't have SQl on my server...
You don't have MySQL?Is a company hosting your server, or are you running one yourself? I could help you install it:)
Extreme
07-10-2003, 09:15 PM
My english is not serving me that well, so I don't know what does mean "array"... My code reds one email at the time from TXT and eveery email is in a new line... Your's does the same thing or better??????
IN the first script I wrote upp there, there is a functon that prevents timouts, because it doesn't allow server to send more than certain number of messages at once(I think)...
I don't have money to buy a server so I must use my free webspace I got with dialup account.... And therefore, I have no SQL, and way to install it...
diamonds
07-12-2003, 09:34 AM
Well, an array is somthing where you can store multiple variables in one variable.
Try this script on your server:
<?php
$see_me = array(
'77',
'66',
'55',
'a' => '678',
'b' => '123'
);
echo $see_me[1]."<br>\n";
echo $see_me[2]."<br>\n";
echo $see_me[3]."<br>\n";
echo $see_me['a']."<br>\n";
echo $see_me['b']."<br>\n";
?>
also search php.net for the array() function.
http://us3.php.net/manual/en/ref.array.php
http://us3.php.net/manual/en/function.array.php
Extreme
10-18-2003, 11:35 PM
OK, so there is a code for reading email list from file..
Now all "we" need to do is to replace SQL code with that one.. It is a bit tricky for me to do it, because there are many variables that can't be just deleted..
CAn someone try to make changes without destroying these valuable options like timeout prevention and echoing the email sent..
diamonds
10-19-2003, 01:41 PM
Ahhh! you replyed!
(i thought you didnt like this forum anymore)
the header would go somthing like this:
<?php
$most_to_mail = 5;
$curr = @$_GET['curr'];
if($curr == ""){$curr = 0;}
$list = file('maillist.txt');
for($i=$curr,$curr>=count($list),$i++){
mail($list[$i]);
}
?>
I continue to work on it.
Extreme
10-19-2003, 04:51 PM
OK, nice... Now. Can you combine this with the script from the first post?? And delete unneccessary part where it reads files from SQL database...
Extreme
10-28-2003, 09:56 PM
Did everyone gave up on this emailer?
diamonds
11-26-2003, 02:26 PM
hey, if i ever blank out, just send me a private message, ok?
goto 'user cp', than 'private mail'.
I am working the script again.
so so sorry.
diamonds
11-26-2003, 04:07 PM
the script is attached.
It's not done, but it's better than nothing.
where it asks for the mailbody, just use a .html OR a .txt file.
before you use it, test it on yourself for bugs.
report any, please.
Extreme
11-27-2003, 08:07 PM
Can you explain output a bit??? What means what?
5 = lenth_to_wait
5 = curr
0 = start
5 = lenth
5 = done
done
"Lenght to wait" is number of threads(number of emails that are sent in the same time)?
And there is a problem.. The script doesn't wish to send emails... I tryed with my list of 5 emails, and none of the emails arrived... DO I need to have some special PHP version or module of some sort?
Actually.. It did send email, but only to first email from my list.. And it was totaly blank.. There was nothing in the body although I included some TXT file as body of mesage.. Subject of message was also blank, but now that I examine the script, I can't really see a field to enter my subject, nor "From:" field?!? You included the $from variable but haven't putt anywhere some function where I would declare what "from:" value is.. Sorry if I am misunderstandable..
Extreme
12-02-2003, 07:35 PM
OK, emails are arriving now.. I have changed the X-Mailer from PHP into seomthing else, because hotmail seems to block that as it thinks it is a spam only because it is PHP..
Also, I have added this:
$from_name = some_name;
$from_email = 'some@mail.com';
so the "from" and "from email" filed are not empty any more in emails I get.
Then.. I replaced $message with $messagebody inside mail() so I get message now..
And Finally.. I got Subject field working now too.. I just needed to add $subject variable into two instances of sendmail() function...
<?php
//OPTIONS UP HERE ;)
$options_use_html = true;
$options_subject = true;
$options_from_name = true;
$options_from_email = true;
$options_mail_body_file = 'testing.html';//location of the body file
$options_to_emails = 'maillist.txt';//location of the peiples emails.
$options_urgent = false;//if this is an urgent message
$subject = 'mile0';
$from_name = mile1;
$from_email = 'mile2@mail.com';
//FEATURE: add
//{firstname} or {lastname} in the mailbody document and this script will replace it with the name in the maillist in the file.
//to use this, just seperate the email(goes first) from the persons name(goes last) with a * charactor.
//P.S. dont know their name? just use '*sir or madam*' this will make the {lastname} be blank, the firstname will appear 'sir or madam'
// example: mailf@mail.mail*firstname*lastname
// mailf@mail.mail*sir or madam*
//END CONFIG
$xheaders = "From: " . $from_name . " <" . $from_email . ">\n";
$xheaders .= "X-Sender: <" . $from_email . ">\n";
$xheaders .= "Reply-To:$to\n";
$xheaders .= "X-Mailer: Certifed Mailer\n"; // mailer
$xheaders .= "Return-Path: $to\n";
if($options_urgent){$xheaders .= "X-Priority: 6\n";} // Urgent message!
if ($options_use_html){$xheaders .= "Content-Type: text/html;\n charset=iso-8859-1\n";}// Mime type
$messagebody = file_get_contents($options_mail_body_file);
function sendmail($location,$subject,$messagebody,$xheaders){
list($to,$firstname,$lastname) = explode('*',$location);
$messagebody = str_replace('{firstname}',$firstname,$messagebody);
$messagebody = str_replace('{lastname}',$lastname,$messagebody);
mail("$to" , "ID: $subject" ,$messagebody,$xheaders);
}
$lenth_to_wait = 5; //in messages
$done = 0;
$curr = @$_GET['curr'];
if($curr == ""){$curr = 0;}
$start = @$_GET['curr'];
if($start == ""){$start = 0;}
$list = file('maillist.txt');
$lenth = count($list);
while($done < $lenth_to_wait){//&&($curr < $lenth)
if($list[$curr] != ""){
sendmail($list[$curr],$subject,$messagebody,$xheaders);}
$curr++;
$done++;
}
echo $lenth_to_wait;
echo " = lenth_to_wait\n<br>";
echo $curr;
echo " = curr\n<br>";
echo $start;
echo " = start\n<br>";
echo $lenth;
echo " = lenth\n<br>";
echo $done;
echo " = done\n<br>";
if($curr < $lenth){
echo "<scr"."ipt>this.location.href='email.php?curr={$curr}';</sc"."ript>";//somthing to make the page advance
}else{
echo "done";
}
//print_r($list);
?>
diamonds
12-29-2003, 09:12 AM
i spotted some things...
$from_name = mile1;
should be
$from_name = 'mile1';
quotes were added, if you have not alredy got those
and
while($done < $lenth_to_wait){//&&($curr < $lenth)
if($list[$curr] != ""){
sendmail($list[$curr],$subject,$messagebody,$xheaders);}
should be
while($done < $lenth_to_wait){//&&($curr < $lenth)
if(trim($list[$curr]) != ""){
sendmail($list[$curr],$subject,$messagebody,$xheaders);}
the trim() function was added
Extreme
12-29-2003, 02:18 PM
Hey, where did you desapear for such long time...
What is the difference with "trim"? What does it do?
Also, I played around with mailer and try to mail myself 2000 times, and I got timeout every time.. I canot bypasss server's settings with set_time_limit, sosomething else must be done.. Someone proposed me that it should be done like this: For ex. mailer sends 100 emails from list, then redirects page to SELF, and then continue to mail another 100 emails, then redirects again etc.. Think that this could be done somehow?
diamonds
12-29-2003, 04:40 PM
trim trims off extra whitespace on both sides of the input string.
well, to debug, see into that (a) the function mail() works by creating a file with just mail();.
and (b) comment out mail() and add echo "mailed someone";
if it still freeses up, it is the loop. if it does not, it is the mail function.
Extreme
12-29-2003, 05:38 PM
It doesn't freeze up.. It sends mail, and when it sends out about 900-1000 emails, script just brakes out and I just get an error in IE, "Cannot find webserver"..
BTW, is it possible to add proxy support with fsockopen() functions???
diamonds
12-31-2003, 08:47 AM
it is probably how the mail() settings in the php.ini are set up. did you make a blank, page with mail('test_email','testing!','testing.'); ? (replace test_email with yours first)
Sux0rZh@jc0rz
01-01-2004, 04:33 AM
not that it really matters now, but due to the fact that you dont have a SQL host, i figure i'd help you.
http://www.freesql.org
free mysql/postgre/other database hosting. have fun.
Extreme
01-01-2004, 01:14 PM
thanks Sux0rZh@jc0rz,
and Diamonds.. It sends email so there is no problem in configuration.. I told you, I sent about 2000 emails to myself.. And I recieved abuot 900 allways.. But then, I don't know why, when it sends this (900+)th email, script gives me "page cannot be found" message...
Sux0rZh@jc0rz
01-02-2004, 12:34 AM
hey, no problem. just figured you will probly need mysql again someday and it's best to have it instead of having to convert again.