php problem emailer script sends twice, I dont know why!?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daredevil
Forum Newbie
Posts: 6
Joined: Mon Apr 05, 2004 7:08 pm
Location: AL

php problem emailer script sends twice, I dont know why!?!

Post by Daredevil »

Hey guys I posted one earlier about getting my email script to send a file along with the email now its doing that but it is also now sending a second email behind the 1st. This script is perfect other than that. If someone could show me what the problem is I would really appreciate the help.

Thanks guys


#!/usr/local/bin/php

Code: Select all

<? 
function sendmsg($to, $subject, $text, $from, $file, $type) { 
$content = $file; 
$content = chunk_split(base64_encode($content)); 
$uid = strtoupper(md5(uniqid(time()))); 
$name = 'report.txt'; 
$header = "From: $from\nReply-To: $from\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "Content-Type: multipart/mixed; boundary=$uid\n"; 
$header .= "--$uid\n"; 
$header .= "Content-Type: text/plain\n"; 
$header .= "Content-Transfer-Encoding: 8bit\n\n"; 
$header .= "$text\n"; 
$header .= "--$uid\n"; 
$header .= "Content-Type: $type; name="$name"\n"; 
$header .= "Content-Transfer-Encoding: base64\n"; 
$header .= "Content-Disposition: attachment; 
filename="$name"\n\n"; 
$header .= "$content\n"; 
$header .= "--$uid--"; 
mail($to, $subject, "", $header); 
return true; 
} 


// Load up array with file names! 

$handle = opendir('/spooler'); 

$array_idx = 0; 

while (false !== ($file = readdir($handle))) 
{ 
$dir_array[$array_idx] = $file; 
$array_idx ++; 
} 


foreach ($dir_array as $n) 
{ 

if ($n == '.' || $n == '..' || $n == 'bin') 
{ 
// Do Nothing for these 
} 

else 
{ 
$filename = "/spooler/".$n; 
$handle = fopen ($filename, "r"); 
$contents = fread ($handle, filesize ($filename)); 
fclose ($handle); 

//testing text file for ownership 

} 

else 
{ 
$filename = "/spooler/".$n; 
$handle = fopen ($filename, "r"); 
$contents = fread ($handle, filesize ($filename)); 
fclose ($handle); 


$email_to = "tester@testing.com"; 

$mailto = $email_to; 
$subject = "ECA Auto-Reports"; 
$body = "Your report is attached to this message."; 
$from = "mail-bot@elasticcorp.com"; 
$file = $contents; 
$type = "text/.txt"; 


sendmsg($mailto, $subject, $body, $from, $file, $type); 

// remove file from dir 
unlink("/spooler/".$n); 
} 


} 
?>
[Edit: Added PHP tags for eyecandy. --JAM][/b]
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

hey use php tags it makes it easier to read and help you!
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

And looking at the code, the else which contains the sending email part has no if to it - the if($n=='.' etc has two else's and the email is in the foreach - so ill assume you have two files in there and its sending an email for each one
Daredevil
Forum Newbie
Posts: 6
Joined: Mon Apr 05, 2004 7:08 pm
Location: AL

Post by Daredevil »

Well it use to, im tryin to modify a script that did that to a script that sends just one file to many people.

The person I replaced copied this from somewhere, and now Im trying to make it work
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Well that foreach statement is going to send an email for every file in the directory - do you know the name of the one file you want to send?
Daredevil
Forum Newbie
Posts: 6
Joined: Mon Apr 05, 2004 7:08 pm
Location: AL

Post by Daredevil »

Well it changes everytime, as400 changes the file name everytime the job is printed
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

but your script know what it is, right? The script as it is now is looking in a directory and sending an email for each file in it. So you either have to ensure that there is only 1 file in the directory or know the name of the file you want to send and just open it without mucking around with the directory


ps(i think false !==($f... should be != ($f...
Daredevil
Forum Newbie
Posts: 6
Joined: Mon Apr 05, 2004 7:08 pm
Location: AL

Post by Daredevil »

Okay, I tried that it still sends two but it changed the order, now im getting the bogus one 1st then the good one second
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

what are the file names? and what do you have in that dir?
Daredevil
Forum Newbie
Posts: 6
Joined: Mon Apr 05, 2004 7:08 pm
Location: AL

Post by Daredevil »

tofile.morgans.33 is an example

they are located in the /spooler directory and there is 2 folders there along with the as400 files
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

ok, so its sending the directory as a file - you can use is_dir and if itts true dont send it! but if you have more than one file, you still need to know the name of the one you want
Post Reply