Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


send file bcc addressed via sendmail

Posted on 1999-07-09
Medium Priority
Last Modified: 2010-03-05
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!

Question by:Biffo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 19

Expert Comment

by:Kim Ryan
ID: 1214009
Try downloading the Mail::Sender program from CPAN, then

use Mail::Sender;
$sender = new Mail::Sender
{smtp => '', from => ''};

foreach $current_addr (@all_addresses)
    to => '',
    bcc => $current_addr,
    subject => 'Here is the file',
    msg => "I'm sending you the list you wanted.",
    file => 'filename.txt'



Expert Comment

ID: 1214010
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.
LVL 84

Expert Comment

ID: 1214011
Useless use of join in void context at - line 4.
(you may also want to add a chomp)
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 1214012
oops.  Thanks for catching that, ozo.

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

*should* work.


Author Comment

ID: 1214013
where is the part of reading the log file into memory to send as body???? Looks kind of incomplete.

Expert Comment

ID: 1214014
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 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.

LVL 19

Expert Comment

by:Kim Ryan
ID: 1214015
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.

Accepted Solution

JohnSails earned 800 total points
ID: 1214016
# read in a file:
$file = "../logfile/log.txt";
open(big,$file) || die "$file not available... Try again.\n";

 while ($log ne "") {
# done reading file, all log data stored in $log

#Using standard sendmail: Here's your BCC code

$mailprog = '/usr/sbin/sendmail'

open(MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
print MAIL "Sender:  $FORM{'myname*'} ($FORM{'email*'})\n";
print MAIL "From: ($FORM{'email*'})\n";
print MAIL "Reply-To: $FORM{'email*'} ($FORM{'myname*'})\n";
print MAIL "To:  $mailto\n";
print MAIL "BCC:  $mailbcc\n";
print MAIL "Subject: My Log file\n\n";
print MAIL "$log\n\n"
print MAIL "\n\n";

Author Comment

ID: 1214017
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.


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question