Link to home
Start Free TrialLog in
Avatar of Biffo
Biffo

asked on

send file bcc addressed via sendmail

I have a script I run nightly that produces a log file, and now a few users want a copy of it sent via e-mail: I need some sub code to add that will open a address.txt file for the email addresses and send the file bcc to each address!


Thanks!!!!!!!
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Try downloading the Mail::Sender program from CPAN, then

use Mail::Sender;
$sender = new Mail::Sender
{smtp => 'mail.yourdomain.com', from => 'your@address.com'};

foreach $current_addr (@all_addresses)
(
 $sender->MailFile
 (
  {
    to => 'some@address.com',
    bcc => $current_addr,
    subject => 'Here is the file',
    msg => "I'm sending you the list you wanted.",
    file => 'filename.txt'
  }
 );
)

$sender->Close;

Avatar of djsansui
djsansui

I'm not sure sendmail can send bcc.  If it can, just do something like:

open(EMAIL, "address.txt") || die $!;
@email = <EMAIL>;
close (EMAIL);
join(", ",@email);

otherwise, skip the join line, and do this:

foreach $i (@email) {
#open the message, etc.
print MAIL "to:$i\n";
#print rest of header & body, then close
}

I prefer the second, especailly since you can have fun costomizing it, like adding their name, etc. to the message you're sending.
Avatar of ozo
Useless use of join in void context at - line 4.
(you may also want to add a chomp)
oops.  Thanks for catching that, ozo.

open(EMAIL, "address.txt") || die $!;
while(<EMAIL>) {
        chomp;
        push(@email, $_);
}
close (EMAIL);
$emails = join(", ",@email);

*should* work.

Avatar of Biffo

ASKER

where is the part of reading the log file into memory to send as body???? Looks kind of incomplete.
That wasn't in your question.  What I gave you is a snippet of code that will take care of emailing your logs to everyone in address.txt.  I thought you already had the part about reading the logs figured out.

if you want to do the mailing from the same script, you should be able to figure it out, just copy the print statements (and any loops) that make the log file, and put those in the mail document print (changing all the "print" statements to "print MAIL").

If you are unfamiliar with Perl programming, please go to http://www.cgi101.com/class/ and read it and all 6 chapters (while playing with the examples) you should be able to figure all this stuff out when you are done, it is pretty basic stuff.

Biffo, you haven't said if the bcc in my answer does what you want. As for reading the log file into memory, I can't really see the need. THe contents will probably be cached after the first email is sent, so will really be in memory anyway.
ASKER CERTIFIED SOLUTION
Avatar of JohnSails
JohnSails

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Biffo

ASKER

Just what I wanted! Thank You. Also, many apologies for my lack of clarification in my original question, I know no one can help another if the question is not clear as to what is needed.

Biffo