Perl - How to send local mail? - linux

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.

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?
why is the -- in your command? that maybe confusing mutt.
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.

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.

Related

Use Japanese characters in subject and attach file with mailx in RedHat Linux

I am trying to use the mailx command for sending email with attachment (zipped) and am facing two issues, below is the command I use:
(echo "$BODY"; UUENCODE $ZIP_FILE $ZIP_FILE) \
| mailx -s $SUBJECT_1 -r " " $SENDER $RECIPIENT
My email subject contains space and Japanese characters.
The variable $SUBJECT_1 has the following statement
Subject: [Budget] Subtype Error and some JAPANESE CHARECTERS
I get bet following error:
contains invalid character '\203'
Moreover for testing purpose I changed the statement of SUBJECT_1 to Test Message
SUBJECT_1="Test Message"
It worked, but I receive only Test instead of Test Message and in the mail I could see two more email ids in the To like Message#domain.com and -r#domain.com
I have not implemented the mail body yet, once subject issue fixed will implement the same in body because Body will also have Japanese characters.
Please help me with this error, how to resolve and what am i doing wrong
There's a list of things you need help with here, more than I want to to handle exhaustively on a sunny Saturday afternoon. But some hints.
Quote your variables.
"$SUBJECT_1" is a single string, whereas $SUBJECT_1 is a list of space-separated words. The second word is your email recipient, and subsequent options are also recipients.
Subject.
The basic idea is that you need to include encoding data in the subject, because email headers are only supposed to include 7-bit ASCII.
Here is a hint at how you put special characters in your Subject line.
Here is another hint.
Here is the RFC that describes in lurid detail what you need to do. Asking your favourite search engine for information about "utf8 email subject" and "rfc1522" is probably a good idea.
Email client.
Finally, rather than learning how to use MIME, consider using mutt instead of mailx to send your mail. Mutt has a -a option to add attachments, making it WAY easier than constructing your own headers and body, which I'm not even sure you'd be able to do with mailx in the first place.

How to fill in a Command Line Interface?

I am to automate the installation phase of a legacy system, because I do not want to put more efforts again and again when ever I want to install it. During the installation process on Linux Terminal, I have to answer some questions regarding the basic configurations. Indeed, it is easy to automate the shell commands by putting them all in a batch file like the following:
bin/startServer destination/sub_v1
bin/startAdminInterface
....
However, I do not know how to automate the answers of a specific questions like the following:
Enter the server's IP address:
Enter the email of the contact person:
Would you like to disable UDP services?(y/n) [n]:
....
Is there any tool or programming language can deal with this situation? or Is there any way to pass the answers as a parameters within the batch file?
Thanks in advance.
The classic Linux tool for this job is expect.
With expect one can expect different questions and variations on a question, and the question does not have to be typed exactly. expect does not blindly answer every prompt, but rather it provides answers to the questions actually asked.
Here is a short example:
#!/usr/bin/expect -f
spawn someScript.sh
expect "Enter the server's IP address:"
send "10.0.0.4\r"
expect "Enter the email of the contact person:"
send "foo#bar.com\r"
expect "Would you like to disable UDP services?(y/n) [n]:"
send "y\r"
So, imagine this is a simplified version of the script:
#!/bin/bash
read -p "Enter something:" thing
read -p "Enter server IP:" ip
read -p "Enter Email:" email
echo Received $thing, $ip, $email
and this is in a file called answers
thingywhatnot
11.12.33.240
bozo#brains.com
You would run
installationScript < answers
and it would print this
Received thingywhatnot, 11.12.33.240, bozo#brains.com

Need to capture the commands fired on Linux

I would like to capture all the commands fired by a user in a session. This is needed for the purpose of auditing.
I used some thing like below,
LoggedIn=`date +"%B-%d-%Y-%M:%H"`
HostName=`hostname`
UNIX_USER=`who am i | cut -d " " -f 1`
echo " Please enter a Change Request Number for which you are looging in : "
read CR_NUMBER
FileName=$HostName-$LoggedIn-$CR_NUMBER-$UNIX_USER
script $FileName
I have put this snippet in .profile file, so that as soon as the user logs in to a SU account this creates the file. The plan is to push this file to a central repository where an auditor can look into those files.
But there are couple of problems in this.
The "script" command spools all the data from the session, for example say, a user cats a property file, It appends all the data of the property file to the auditing file.
Unless user fires the 'exit' command, the data will not be spooled to auditing file, by any chance if user logs out with out firing exit command, the auditing file will be empty.
Is there any better solution for auditing ? History file is not an option since it does not tell me for which Change Request number ( internal to my organisation) the commands are fired. Is there any other way just capture only the commands fired but not the output ?
Some of the previous discussion are here and here
I think this software exactly matches your need:
https://github.com/a2o/snoopy

Perl: Read email and use message content as stdin for an excel update

I am looking for some help with starting a Perl script. I'm relatively inexperienced with Perl so help would be appreciated :)
Basically, want to start a project to write a script that helps keep up to date with hours I have been working. Basically I would like the script to E-mail (automated using cron) me reminding me to send my hours each day, then I send an e-mail back with the message as something like
"03/02/14 7.30 18.30"
The script will then read the data and update an excel spreadsheet keeping a log of hours.
I know how to do everything except having the script read an e-mail. I have been doing research into MIME::* MAIL::* but I'm not entirely sure which package would be the best and how to actually go about it.
As #mpapec suggested you could read email using IMAP or a local mailbox on a linux box.
In windows you could use OLE and read emails in an outlook: Perl: Win32::OLE and Microsoft Outlook - Iterating through email attachments efficiently
You could read emails on exchange in this way: http://metacpan.org/pod/Email::Folder::Exchange
If I were you I would use IMAP to access emails. It is platform independent and not too hard to use (I used it in the past and it was reliable).
http://metacpan.org/pod/Net::IMAP::Client
my $imap = Net::IMAP::Client->new(
server => 'mail.you.com',
user => 'USERID',
pass => 'PASSWORD');
# select folder
$imap->select('INBOX');
#newest first
my $messages = $imap->search({
FROM => 'you',
SUBJECT => 'your email subjet',
}, [ '^DATE' ]);
# fetch full message (newest)
my $data = $imap->get_rfc822_body($messages->[0]);
#process
store_data_in_excel($data);
#move to archive
$imap->copy([$messages->[0]], 'Archive');
$imap->add_flags(\#msg_ids, '\\Deleted');
$imap->expunge;

Create serial mail on linux commandline

I would like to generate serial emails on the linux commandline. Assume I have a file indicating mail address, subject and message text in columns on separate lines for each recipient. I.e.
recipient1#mail.com subject1 text1
recipient2#mail.com subject2 text2
...
The script should use standard commands as I intend to send it to colleagues who should create some emails for me.
The loop over the lines could be with xargs ... Can I use the commandline tool mail?
It is important that the mails are not send immediately. Ideally it creates files for import in the users preferred mail client. So that senders can check the mails before they submit.
I also would like to be able to add attachments to the Mails.
I tried e.g.
function mail_kmail {
kmail -s "$2" --body "$3" --attach "$4" --composer "$1"
}
function mail_thunderbird {
thunderbird -compose "to='$1',subject='$2',body='$3',attachment='$4'"
}
and reading the input data from file with
while read recipient subject body attach $file
do
mail_kmail "$recipient" "$subject" "$body" "$attach";
done
but this will work only if my colleagues installed and set up either of these mail clients.
I found this (closed) related question:
How can i send automated email in linux? .
You can use mutt to send the emails, here is an example:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
Since it's hard to know what you are trying to achieving here, you can even create a configuration file to be used, but you'll to investigate more or give more details about your use case.

Resources