首页 > 解决方案 > 生成带有 Excel XLSX 附件的电子邮件

问题描述

当我尝试打印整个字符串或编码部分时,以下 Perl 程序在第 44 行给出拒绝访问错误。如果我只打印标题使用$msg->print_header(\*STDOUT).

我要做的是生成一个文本文件,其中包含可用于 telnet 命令的所有信息,以通过发送带有附件的电子邮件来测试消息传输代理(MTA)。

use MIME::Lite;
use Net::SMTP;

### Add the sender, recipient and your SMTP mailhost
my $from_address = 'temp999 at gmail.com';
my $to_address   = 'test05@gmail.com';
my $mail_host    = 'gmail.com';

### Adjust subject and body message
my $subject      = 'Testing script';
my $message_body = "I am sending an email with an attachment";

### Adjust the filenames
my $my_file_xlsx   = 'c:/temp';
my $your_file_xlsx = 'count.xlsx';

### Create the multipart container
$msg = MIME::Lite->new(
    From    => $from_address,
    To      => $to_address,
    Subject => $subject,
    Type    => 'multipart/mixed'
) or die "Error creating multipart container: $!\n";

### Add the text for the message body
$msg->attach(
    Type => 'TEXT',
    Data => $message_body
) or die "Error adding the text message part: $!\n";

### Adding an Excel file
$msg->attach(
    Type        => 'application/octet-stream',
    Path        => $my_file_xlsx,
    Filename    => $your_file_xlsx,
    Disposition => 'attachment'
) or die "Error adding $file_xls: $!\n";

### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout => 60);
#$msg->send;
$msg->print(\*STDOUT); # Write to a file handle ### LINE 44 ###
#$msg->print_header(\*STDOUT); # Write the header
#$msg->print_body(\*STDOUT); # Write the encoded body

我没有找到任何与我想要做的完全匹配的东西,但我在搜索时可能没有使用正确的术语。

标签: perlemail-attachmentsmime

解决方案


您正在尝试附加文件c:/temp,它(可能)是一个目录,而不是一个文件。当 MIME::Lite 尝试将其作为文件打开以读取内容时,它会因该错误而失败。您可能打算c:/temp/count.xlsx通过Path.


推荐阅读