Perl,读取文件并将内容插入电子邮件正文

时间:2012-11-13 19:22:05

标签: perl

我面临着小小的挑战,但是时间在流逝而不是真正的perl家伙,所以欢迎任何帮助。我所拥有的是检查所有正在运行的进程并在/ tmp / error中写入状态的脚本然后我有perl脚本通过外部stmp发送带有附件的电子邮件但是我需要的是刺穿代码/ tmp / error并将其放入在电子邮件正文中所以不需要附带。这是我发现的>这发送文件作为附件,但我需要在身体。

 #!/usr/bin/perl

use MIME::Lite;

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

my $from_address = 'me@comcast.net';
my $to_address   = 'me@hotmail.com';
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 个答案:

答案 0 :(得分:1)

只需将文件内容添加到$message_body,对吧?

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

如果该文件太大,请小心。