Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

emailing files with comma's in the name 2

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
A few weeks ago, I posted a long, confusing script and asked why I couldn't email files that have comma's in the name. Nobody answered, so I wrote a shortened version of the script that only deals with the part I have a question on. When you run this script, it will email any files in C:\somedirectory to perl@foo.org. The problem is that if a file has a comma in the filename, it doesn't get emailed. I'm not sure why...
Code:
#!perl
use Mail::Sender;

$dir = 'C:\\somedirectory\\';
opendir(DIR, $dir) or die "Can't open $dir: $!\n";
while (defined($file = readdir(DIR))) {
  chomp;
  push @list, $file;
}

foreach (@list) {
  $_ = $dir . $_;
  $emailto = 'perl@foo.org';
  $sender = new Mail::Sender
  {smtp => '192.168.1.2', from => 'PerlTest@foo.org'};
  $sender->MailFile({to => "$emailto",
  subject => 'Files Emailed',
  msg => "Emailing \" $_ \"",
  file => "$_"});
  print "$_ was emailed to $emailto\n";
}

Thanks,
Chris
 
encapsulate the filename with addslashes(), see if that fixes your problem.

___________________________________
[morse]--... ...--[/morse], Eric.
 
nawlej, I googled a bit and it seems that addslashes() is a PHP function. I don't see any Perl equivalent, but that could work if there was one.

Chris
 
oh, stuck in another world. embarrasing *grin*

In the same case, I would built a regexp to replace all commas with a slashed comma in the output;

Example:

$string = "myattachement's.txt";
$string =~ s/\'/\\\'/g;

Would output myattachment\'s.txt

Or, you could just change the name of the file to strip the slashed before its mailed.

$string =~ s/\'//g;

___________________________________
[morse]--... ...--[/morse], Eric.
 
quotemeta[/n] will add a backslash before every non alphanumeric character in a string


Kind Regards
Duncan
 
Thanks, but that doesn't seem to fix the script. I changed the foreach loop to this, and now it doesn't email anything.
Code:
foreach (@list) {
   $_ = $dir . $_;
   $file = quotemeta($_);
   $emailto = 'perl@foo.org';
   $sender = new Mail::Sender
   {smtp => '192.168.1.2', from => 'PerlTest@foo.org'};
   $sender->MailFile({to => "$emailto",
   subject => 'Files Emailed',
   msg => "Emailing \" $_ \"",
   file => "$file"});
   print "$_ was emailed to $emailto\n";
}
Chris
 
Actually, I can't get it to email any files with commas even if I specify it directly...
Code:
  $emailto = 'perl@foo.com';
   $sender = new Mail::Sender
   {smtp => '192.168.1.2', from => 'PerlTest@foo.com'};
   $sender->MailFile({to => "$emailto",
   subject => 'Emailing a File with comma',
   msg => "Emailing a file with a comma",
   file => 'C:\somedirectory\a,file.txt'});
Maybe I should try a different module out instead of Mail::Sender?

Chris
 
mmm. odd.

i don't feel very focused at the moment but is this right?

Code:
#!perl
use Mail::Sender;

$dir = 'C:\\somedirectory\\';
opendir(DIR, $dir) or die "Can't open $dir: $!\n";
while (defined($file = readdir(DIR))) {
  chomp; [red]<--- this is 'implicit' so won't it be chomping $_ ???[/red]
  push @list, $file;
}

foreach (@list) {
  $_ = $dir . $_;
  $emailto = 'perl@foo.org';
  $sender = new Mail::Sender
  {smtp => '192.168.1.2', from => 'PerlTest@foo.org'};
  $sender->MailFile({to => "$emailto",
  subject => 'Files Emailed',
  msg => "Emailing \" $_ \"",
  file => "$_"});
  print "$_ was emailed to $emailto\n";
}


Kind Regards
Duncan
 
The chomp isn't causing it, I tried commenting it out but I get the same thing.

I'm not crazy right, if anyone else copies and pastes my script into your own, you get the same thing too, right? I'm starting to think that I need to look at a different module for emailing attachments. I like Net::SMTP for sending basic mail, but its a pain to use for attachments and I think you have to include other modules to get it to work.

Chris
 
MIME::Entity, but I don't know if that's still going to be a problem with the commas, it could be the comma is a delimiter for a number of files.

Can you change the filename, mail it, and then rename it back?

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
try chomping [red]$file[/red]?

could it be that the readdir is carrying line endings into [red]$file[/red]


Kind Regards
Duncan
 
Paul, I can't rename it unfortunately, it needs to keep its name throughout.

duncdude, I don't think that that is the problem since I can email any file that doesn't have a comma, including ones with spaces and even punctuation marks.

Let me put it this way, create a directory somewhere and put a single file inside and name the file something with a comma in it...

mkdir /tmp/testing
touch /tmp/testing/some,file.txt

How would you go about emailing that file you just created from a Perl script? What would be the easiest way and your personal preference?

Thanks,
Chris
 
could a comma be seen as a separator with Mail::Sender?

try tr to translate the comma to another character - maybe a pipe |?

Code:
while (defined($file = readdir(DIR))) {
  [red]$file =~ tr/,/|/;[/red]
  push @list, $file;
}


Kind Regards
Duncan
 
duncdude, I'm a little confused. If we changed a file called "test,example.txt" to "test|example.txt", we can't email that file because it doesn't exist.

Chris
 
good point. can't you perform a global rename of all files with a comma in the name beforehand?


Kind Regards
Duncan
 
duncdude and Paul,

You both mentioned renaming the files before emailing them, and I'm not crazy about the idea but so far it looks like my only option. The reason I don't like it is because the directory we open has sometimes 100,000+ files inside and the script already takes 2-3 minutes to run.

I just tried it though and it does work...
Code:
chdir $dir;
foreach $file (@list) {
  if ($file =~ /,/) {
    $old = $file;
    $file =~ s/,/-/g;
    rename $old, $file;
  }
If anyone has any other ideas, please let me know.

Thanks,
Chris
 
thanks for the star dude

real long shot ... but what happens if you remove the speech marks from this line?

file => "$_"});

to

file => $_});


Kind Regards
Duncan
 
not surprised! but thanks for trying... satisfied my curiosity :)

... i'll keep thinking


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top