【发布时间】:2018-12-30 09:30:09
【问题描述】:
当我尝试打印整个字符串或编码部分时,以下 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
我没有找到任何与我想要做的完全匹配的东西,但我在搜索时可能没有使用正确的术语。
【问题讨论】:
-
我声明了错误的行号导致错误。它应该是第 44 行。下面是我得到的确切错误。打开 c:/temp: 在 send_attachment.pl 第 44 行拒绝权限。
-
请edit您的问题更正任何错误并提供更多信息。
-
XLSX 数据的正确 MIME 类型是
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet -
您必须始终在您编写的每个 Perl 程序的顶部添加
use strict和use warnings 'all'。 -
当Email::Stuffer 等更好的工具存在时,仍然看到人们使用 MIME::Lite 和 Net::SMTP 等旧模块令人沮丧。
标签: perl email-attachments mime