2012-11-13 59 views
2

我面臨着小挑戰,但時間在流逝,而不是一個真正的perl傢伙,所以任何幫助都是值得歡迎的。我的腳本是檢查所有正在運行的進程並在/ tmp/error中寫入狀態然後我有perl腳本通過外部stmp發送帶有附件的電子郵件,但是我需要的是帶有/ tmp/error的代碼,在電子郵件正文中,不需要附件。這是我發現的東西>這發送文件作爲附件,但我需要在身體。Perl,讀取文件並將內容插入電子郵件的正文

#!/usr/bin/perl 

use MIME::Lite; 

# Set this variable to your smtp server name 
my $ServerName = "smtp.comcast.net"; 

my $from_address = '[email protected]'; 
my $to_address = '[email protected]'; 
my $subject  = 'MIME Test: Text'; 
my $mime_type = 'text'; 
my $message_body = "Testing text in email.\n"; 

# Create the initial text of the message 
my $mime_msg = MIME::Lite->new(
    From => $from_address, 
    To => $to_address, 
    Subject => $subject, 
    Type => $mime_type, 
    Data => $message_body 
    ) 
    or die "Error creating MIME body: $!\n"; 


# Attach the text file 
my $filename = 'C:\tmp\test.txt'; 
my $recommended_filename = 'test.txt'; 
$mime_msg->attach(
    Type => 'application/text', 
    Path => $filename, 
    Filename => $recommended_filename 
    ) 
    or die "Error attaching text file: $!\n"; 

# encode body of message as a string so that we can pass it to Net::SMTP. 
my $message_body = $mime_msg->body_as_string(); 

# Let MIME::Lite handle the Net::SMTP details 
MIME::Lite->send('smtp', $ServerName); 
$mime_msg->send() or die "Error sending message: $!\n"; 

請幫

回答

1

只需將文件的內容添加到您的$message_body,對不對?

my $message_body = "Testing text in email.\n"; 
{ 
    local $/ = undef; 
    open FILE, "file" or die "...: !$"; 
    $message_body .= <FILE>; 
    close FILE; 
} 

要小心,如果該文件太大雖然。

+0

Thx,打開文件,「/tmp/yourmsg.txt」或死「錯誤打開文件:$!」; – user1821820

相關問題