【问题标题】:Perl, read file and insert content into email's bodyPerl,读取文件并将内容插入电子邮件正文
【发布时间】:2012-11-02 06:26:17
【问题描述】:

我面临着小挑战,但时间在流逝,并不是一个真正的 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";

请帮忙

【问题讨论】:

    标签: perl


    【解决方案1】:

    只需将文件内容添加到您的$message_body,对吗?

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

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

    【讨论】:

    • 谢谢,打开文件,“/tmp/yourmsg.txt”或死“打开文件时出错:$!”;
    猜你喜欢
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多