Click to See Complete Forum and Search --> : Read recipient list from file


Extreme
06-29-2003, 10:17 AM
Well, just a simple question regarding sendmail script.. I wonder how to import recipient list from file???
This is how default recipient variable looks like:
$recipient .= "example2@php.net";
$recipient .= "example3@php.net";

I don't want to enter emails one at the time like this, but to read them from file. So I guess $recipient variable need to be changed or connected with some other script... Someone told me that it could be done by conecting $recipient with this script, but I don't know how.... Here is that script:
fgets - Gets line from file pointer
EXAMPLE:

$fd = fopen ("/tmp/inputfile.txt", "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
echo $buffer;
}
fclose ($fd);

brendandonhue
06-29-2003, 01:18 PM
You could put 1 email address on each line of a text file,
then set the text file lines to an array using something like
$emails = file("filename.txt")
Then $emails['0'] would be the 1st address, $emails['1'] would be the 2nd etc.
You could use a loop to cycle through them then.

pyro
06-29-2003, 02:08 PM
Yeah, do something like what brendandonhue suggested. Put the emails on their own line in a .txt file, and get the values like this:

<?PHP
$recipient = "";
$contents = file("listofemails.txt");
foreach ($contents as $value) {
$recipient .= $value;
}

Extreme
06-29-2003, 08:10 PM
Why won't this work? I save it as mailer.php and run it through IE on a server(ex. www.server.com/mailer.php), but nothing.. It wouldn't send any emails... And it only has 3 emails in file..


<?
$recipient = "";
$contents = file("emails.txt");
foreach ($contents as $value) {
$recipient .= $value;
}
$subject = "Subject of the message";

/* message */
$message .= "The following email includes a formatted ASCII table\n";
$message .= "Day \t\tMonth \t\tYear\n";
$message .= "3rd \t\tAug \t\t1970\n";
$message .= "17rd\t\tAug \t\t1973\n";

/* you can add a stock signature */
$message .= "--\r\n"; //Signature delimiter
$message .= "My signature";

/* additional header pieces for errors, From cc's, bcc's, etc */

$headers .= "From: Od mene <my@email.net>\n";
$headers .= "X-Sender: <my@sender.net>\n";
$headers .= "X-Mailer: Microsoft Outlook Express 6.0\n"; // mailer
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: <me@emailt.com>\n"; // Return path for errors

/* If you want to send html mail, uncomment the following line */
// $headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type


/* and now mail it */
mail($recipient, $subject, $message, $headers);

?>

pyro
06-29-2003, 08:19 PM
It won't work because once the foreach loop is done, $recipient is going to equal something like "addressone@domain.comaddress2@domain.comaddress3@domain.com" What you are going to want to do is put the entire mail script in the foreach loop, and then set up the recepients like that... Also, concatenating to a string, before you define the variable can cause problems on some servers. ie ($message .= "whatever"; should possibly have a $message = ""; on a line before it)

Extreme
06-29-2003, 08:32 PM
Could you modify my script above, cause I am not very sure how to do what you said... Also, instead of writing $message .= "bla"; for every new line, is it possible to putt only one $message variable and then modify the text and lines from inside quotes? Include that in example too, if it is possible.. Please forgive me for my lack of knowledge..

pyro
06-29-2003, 08:42 PM
Try this: (note that I didn't use the form script that you were using, but a varriation of mine, found here (http://forums.webdeveloper.com/showthread.php?s=&threadid=9543#post48748))

<?PHP

## The lines below need to be edited...

###################### Set up the following variables ######################
# #
$emailfile = "file.txt"; #file that contains the email addresses
$subject = "Your subject here"; #set the subject line
$message = "This is where the email message goes"; #your message
$headers = "From: you@your.com"; #set the from address
# #
##################### No need to edit below this line ######################

## mail the message ##

$contents = file($emailfile);
foreach ($contents as $email) {
mail($email, $subject, $message, $headers);
}
echo ("Emails successfully sent");

?>

Extreme
06-29-2003, 09:12 PM
Something is not right. I think it is with your code... I have 3 emails in my TXT file... First email is not delivered, second one is delivered but there is no From:(It says Nobody) or Subject:(it is empty), and in the message box this is written:

Subject: Your subject here
From: you@your.com

This is where the email message goes


Third email is delivered correct. Everything is the way it should be... But why only third(last) one?????

pyro
06-29-2003, 09:23 PM
I tested the code before I posted it with 2 email addresses. Please post your modified code. Also, it would be helpful if you attach the file you listed the email addresses in.

Extreme
06-29-2003, 09:55 PM
<?PHP

$emailfile = "emails.txt"; #file that contains the email addresses
$subject .= "Subject of the message";
$message .= "This is where the email message goes"; #your message
$headers .= "From: Od mene <moj@email.net>\n";

$contents = file($emailfile);
foreach ($contents as $email) {
mail($email, $subject, $message, $headers);
}
echo ("Emails successfully sent");

?>

pyro
06-29-2003, 10:03 PM
Try setting these:

$subject .= "Subject of the message";
$message .= "This is where the email message goes"; #your message
$headers .= "From: Od mene <moj@email.net>\n";

to these:

$subject = "Subject of the message";
$message = "This is where the email message goes"; #your message
$headers = "From: Od mene <moj@email.net>\n";

Extreme
06-29-2003, 10:25 PM
Nope, now it sends messages to all three emails, but only the LAST ONE from the list gets email in proper way... Other two get this... I used authentical code that is written last time...
From field: Nobody Subject: Empty Space

Subject: Subject of the message
From: Od mene <moj@email.net>


This is where the email message goes

All this is writtn in the message window...
I don't know... Try to upload to your server and execute the same script with same emails.. Maybe it is an error in my ISP sendmail() configuration.. Just change the Subject so I know that it is you....

pyro
06-29-2003, 10:29 PM
Ok, I ran it from my server...

Extreme
06-29-2003, 10:38 PM
Yap, I got all in perfect order.... So it gotta be my ISP's cofiguration error... Any way to fix this or use some other mailing program? This is my free space I was given with dialup. Is there a way to check if the right mailing program is installed?????
Anyway, more important now that everything is working... Is there a way to make a log file after the emailing has been done, that will log SENT and UNSENT emails...
Or, cool feature, but I don't think that this can be done... When sending, a popup window pops up(offcourse) and says

sending email 1 of 983

and changes as the email is sent... That is a cool way to find out a speed of sending...

pyro
06-29-2003, 10:45 PM
Yes, must be a problem with the server, though a weird one, at that.

Depends on what you mean by "keep a log of sent email." You certainly could log each email that the PHP sends a message to, but I'm not aware of a way to check if the email bounced or not...

And, as far as telling you which email it is on, you'd be able to do it (though I'm not sure about in a popup). But, it might be to fast to really see anything...

Extreme
06-29-2003, 11:08 PM
No need for log file actually...
But the script that counts which email is currently being sent is realy needed, because we can see if the server is still responding, and we can see speed of sending offcourse.. It doesn't have to be in popup.. Anywhere is good, it's just that I thought it cannot be done in the same window where it will say: Emails successfully sent..
Can you please post here some counter?? Again, built into the script... That is the easyest way I could understand.. Just a simple SENDING EMAIL "X" OUT OF "1000".... If it will be too fast to see aynthing, than it will be enough to see if it is on two or three digits.. Maybe the counter could change on every 20 emails, that way you could see...

P.S. Is there any free hostings that support this mail program, or in the worst case, non expencive good hosting with that support....???

pyro
06-30-2003, 07:32 AM
For your counter, you are going to have one of two options.

1. Open the file that is sending the mail, and erase any output. This however, would slow your script down a bit.

2. Just append the data to the current page. It would look something like this:
1 out of 3 sent. 2 out of 3 sent. 3 out of 3 sent. Emails successfully sent.
Obviously, with this method, the output could get quite long...

Also, as far as free hosts go, I don't really know. I'm not a big fan of free hosting. For a good PHP host, I would recommend http://www.dr2.net

Extreme
06-30-2003, 09:55 AM
So, both methods would show the same thing(sending 1 out of 1000)???
It doesn't matter if it would slow sending down a bit, as long as "bit" is not much... Maybe it would get faster if you would make counter to change in this order:
Sending message 10 out of 1000
Sending message 20 out of 1000
..or even to vhange on every hundred, if it will make much dfference in speed... AS long as I know that everything is still working... Dangeour of timeouts is also present, right???
Anyway, could you post some simple script about two methods, tell me where to putt them, and say which one is faster..?

pyro
06-30-2003, 09:59 AM
Well, I'm not going to benchmark it, but as to which method is fast, it is going to depend on how many addresses you are talking about here... Do you only need to know if the script times out? What you could probably do is just echo to the screen every 10 or 100 emails, and you will be able to tell that way if it still working...

Extreme
06-30-2003, 10:10 AM
Yes, that's exactly what I need.... On every 10 or 100 emails, it echoes...
Putt some code if you have, so I can test and tell you if it needs something else...

pyro
06-30-2003, 10:24 AM
This should let you know every ten...

<?PHP

$emailfile = "emails.txt"; #file that contains the email addresses
$subject = "Your Subject";
$message = "This is where the email message goes"; #your message
$headers = "From: Od mene <moj@email.net>\n";

$contents = file($emailfile);
$num_lines = count($contents);

for ($i=0; $i<$num_lines; $i++) {
mail($email, $subject, $message, $headers);
if ($i%10 == 9) {
$i++;
echo "$i sent. ";
}
}
echo ("<br>Emails successfully sent");
?>

Extreme
06-30-2003, 03:03 PM
Argh, it doesn't work for me.... Possibly because the configuration(again)... And the echo showes only when all the emails are supposlly sent, and not after every ten... The final echo(for 20 trial emails...) is:

10 sent. 20 sent.
Emails successfully sent

I wont ask you to find what the error is, because you probably have no time for that.. Instead, I would ask you if it is possible to write code that will move on to next email if timeout occurs..? That way, if the server has stopped responding, the connection would be called again...

pyro
06-30-2003, 10:33 PM
Hmm... perhaps you could put the mail inside an if statement.

if (!mail($email, $subject, $message, $headers)) {
"Error sending email";
}

Extreme
07-03-2003, 03:20 PM
I am clueless... I found a script though, that is quite good, as I can see, and probably working, but it reads recipients from SQL base... So you would just need to change that part so it would read recipients from TXT file... Also, it would be better if it would be possible to load file through HTML form using browse button(but I don't know if it can read onlly files that are on server or it can also read file from remote computer?), and then just click SEND button.. Here is the script:

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

?>