0

I would like to integrate the following terminal command into a Perl script.

Terminal command:

mutt -s "User Monitoring" -a "/home/mipa/Documents/System_Monitoring/protocol_name.csv" -- mipa@localhost.localdomain

The command sends local mail containing a file attachment to a user on the same system.

I have a small problem with the command though. It does seem to require more user interaction than just the command listed here. The command requires the user to follow a menu to confirm the values and hit the "y" key to send.

My question here is two-folded. Is there a similar mail command that does not require user interaction and works by just following a single command with predefined flags? And how would I integrate this command into a Perl script where I would be able to choose the file name, and the receiving user followed by issuing the command?

Any guidance regarding a possible solution is highly appreciated.

Anden120
  • 3
  • 5
  • [Email::MIME](http://search.cpan.org/~rjbs/Email-MIME-1.934/lib/Email/MIME.pm) or for simple use [Email::Stuffer](http://search.cpan.org/~rjbs/Email-Stuffer-0.012/lib/Email/Stuffer.pm). – serenesat Aug 17 '15 at 05:14

2 Answers2

0
  1. there are a few ways to send command line emails in Linux: How do I send a file as an email attachment using Linux command line?
  2. why is the -- in your command? that maybe confusing mutt.
  3. https://unix.stackexchange.com/questions/108916/automatically-attach-a-file-to-a-mail-with-mutt has a few more suggestions for sending mail with mutt.
Community
  • 1
  • 1
chicks
  • 2,393
  • 3
  • 24
  • 40
0

I prefer to use MIME::Lite to send emails, rather than spawning an external command, which avoids the problems you are having. MIME::Lite is able to handle sending emails with attachments.

Here is a quick example:

#!/usr/bin/perl

use strict;
use MIME::Lite;

my $msg = MIME::Lite->new(
    To      => 'foo.bar@foobar.com',
    Subject => 'Test message with attachments',
    Type    => 'multipart/mixed'
);

$msg->attach(
    Type     => 'TEXT',
    Data     => "Here's the file you wanted"
);
$msg->attach(
    Type     => 'image/png',
    Path     => 'somefile.png',
    Filename => 'somefile.png',
    Disposition => 'attachment'
);

$msg->send();

This would send a message containing a small amount of text and a single attachment.

There are a lot more examples given in the POD for MIME::Lite.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • FYI, [WAIT! MIME::Lite is not recommended by its current maintainer](https://metacpan.org/pod/MIME::Lite#WAIT). `There are a number of alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you should probably use instead. MIME::Lite continues to accrue weird bug reports, and it is not receiving a large amount of refactoring due to the availability of better alternatives. Please consider using something else.` – serenesat Aug 17 '15 at 04:54
  • @serenesat Hmm. Had not noticed that. It certainly works pretty well in the use cases I have, but YMMV – harmic Aug 17 '15 at 05:54