1> I want to embed below Perl script in BASH script, and the Message should read from a message.txt file. How to realize this?

  use Mail::Sendmail;

  %mail = ( To      => 'you@there.com',
            From    => 'me@here.com',
            Message => "This is a very short message"

  sendmail(%mail) or die $Mail::Sendmail::error;

  print "OK. Log says:\n", $Mail::Sendmail::log;

2> How to attach log files via Perl script?


If you are going to do it in a bash script, then maybe you can do without Perl. In your bash script have something like:

mutt -F $HOME/.muttrc.me -x -a attachment.file -- you@there.com < message.txt

This needs mutt and you can have a custom .muttrc file ($HOME/.muttrc.me in this case) to control the From: header:

my_hdr From: "Me Here" <me@here.com>
my_hdr Reply-To: me@here.com
  • Great! This answers my another question: how to change sender address on the fly using Mutt. But I am now facing another problem: mutt -s "subject here" my@company.net < /dev/null Error sending message, child exited 67 (User unknown.). This command worked well before I edited (but not modified) /etc/mail/local-host-names file. – Andrew Mar 28 '12 at 13:43
  • Try mutt -x -s "subject here" my@company.net < /dev/null. Could it be that company.net is listed in local-host-names but the user does not exist on the system? – adamo Mar 28 '12 at 21:05

The way @adamo told works fine, here's another, perhaps more performant one if there are lots of recipients:

cat message.txt | mail -a 'From: me@here.com' -a 'To: you@there.com' -s 'Subject for the message' you@there.com
  • I tried this command and get error message mail: invalid option --a. – Andrew Mar 28 '12 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.