我有一个从一堆cron作业创建的日志。我现在的任务是将特定日志(例如错误输出)作为电子邮件发送。从文件中获取内容并将其作为电子邮件发送的最佳方法是什么?
我已经知道如何在perl中发送电子邮件。我只需要弄清楚如何阅读文件并将其作为电子邮件的文本。
答案 0 :(得分:11)
我使用MIME::Lite,这是我用于夜间备份的cron脚本:
$msg = MIME::Lite->new(
From => 'backup-bot@mydomain.com',
To => 'test@example.com',
Bcc => 'test@example.com',
Subject => "DB.tgz Nightly MySQL backup!",
Type => "text/plain",
Data => "Your backup sir.");
$msg->attach(Type=> "application/x-tar",
Path =>"/var/some/folder/DB_Dump/DB.tgz",
Filename =>"DB.tgz");
$msg->send;
答案 1 :(得分:4)
你可以像这样啜饮文件的内容并像使用任何其他字符串一样使用它:
open my $fh, '<', 'file.txt' or die "Ouch: $!\n";
my $text = do {
local $/;
<$fh>
};
close $fh or die "Ugh: $!\n";
print $text,"\n";
答案 2 :(得分:4)
您用什么来发送电子邮件?我使用MIME::Lite。你可以用它来附加文件。
否则你只需打开日志,一次读取它(或使用File::Slurp)并将文件内容转储到电子邮件中。
答案 3 :(得分:0)
您可以通过多种方式在Perl中打开文件。
perl -f open
以下是一个例子:
my $file = 'filename.txt';
open my $ifh, '<', $file
or die "Cannot open '$file' for reading: $!";
local $/ = '';
my $contents = <$ifh>;
close( $ifh );
现在只需在您的电子邮件中发送电子邮件$contents
。
我不确定您是如何发送电子邮件的,但我经常使用的方式如下:
# Install these modules from CPAN:
use Mail::Sendmail;
use MIME::Base64;
sendmail(
To => 'you@your-domain.com',
From => 'Friendly Name <friendly@server.com>',
'reply-to' => 'no-reply@server.com',
Subject => 'That file you wanted',
# If you are sending an HTML file, use 'text/html' instead of 'text/plain':
'content-type' => 'text/plain',
'content-transfer-encoding' => 'base64',
Message => encode_base64( $contents ),
);
答案 4 :(得分:0)
我认为附件是你要描述的方式,其他人已经为此做出了贡献,但如果你有需求或需要阅读文件并通过Perl将其解析为电子邮件内容(没有附件),这里是这样做的方法:
#!/usr/bin/perl
# this program will read a file and parse it into an email
use Net::SMTP;
#you need to change the four below line
my $smtp = Net::SMTP->new("your_mail_server_goes_here");
my $from_email = "your_from_email";
my $to_email = "yuor_to_email";
my $file="the_full_path_to_your_file_including_file_name";
my $header = "your_subject_here";
$smtp->mail($from_email);
#Send the server the 'Mail To' address.
$smtp->to($to_email);
#Start the message.
$smtp->data();
$smtp->datasend("From: $from_email\n");
$smtp->datasend("To: $to_email\n");
$smtp->datasend("Subject: $header \n");
$smtp->datasend("\n");
#make sure file exists
if (-e $file) {
$smtp->datasend("testing \n\n");
#read the file one line at a time
open( RFILE, "<$file" )||print "could not open file";
while (my $line = <RFILE>){
$smtp->datasend("$line");
}
close(RFILE) || print "could not close file";
}
else {
print "did not find the report $file ";
exit 1;
#End the message.
$smtp->dataend();
#Close the connection to your server.
$smtp->quit();
#Send the MAIL command to the server.
$smtp->mail("$from_email");
答案 5 :(得分:0)
我们也可以使用mail::outlook
代替mime::lite
:
#open file from local machine
open my $fh, '<', "C:\\SDB_Automation\\sdb_dump.txt" or die "Ouch: $!\n";
my $text1 = do {
local $/;
<$fh>
};
close $fh or die "Ugh: $!\n";
print $text1,"\n";
#create the object
use Mail::Outlook;
my $outlook = new Mail::Outlook();
# start with a folder
my $outlook = new Mail::Outlook('Inbox');
# use the Win32::OLE::Const definitions
use Mail::Outlook;
use Win32::OLE::Const 'Microsoft Outlook';
my $outlook = new Mail::Outlook(olInbox);
# get/set the current folder
my $folder = $outlook->folder();
my $folder = $outlook->folder('Inbox');
# get the first/last/next/previous message
my $message = $folder->first();
$message = $folder->next();
$message = $folder->last();
$message = $folder->previous();
# read the attributes of the current message
my $text = $message->From();
$text = $message->To();
$text = $message->Cc();
$text = $message->Bcc();
$text = $message->Subject();
$text = $message->Body();
my @list = $message->Attach();
# use Outlook to display the current message
$message->display;
# create a message for sending
my $message = $outlook->create();
$message->To('xyz@.com');
$message->Subject('boom boom boom');
$message->Body("$text1");
$message->Attach('C:\SDB_Automation\sdb_dump.txt');
$message->send;