gpt4 book ai didi

perl - 有没有办法在 Perl 中使用 Net::SMTP 附加 PDF 文件

转载 作者:行者123 更新时间:2023-12-04 05:46:14 25 4
gpt4 key购买 nike

我正在尝试在多部分内容电子邮件上附加 pdf 文件,是的,我知道我可以使用 mime lite 或十亿个 perl 模块,但到目前为止,我仅限于使用 perl 5.8.8,因为它是开箱即用的我有

#!/usr/bin/perl
use Net::SMTP;
use MIME::Base64 qw( encode_base64 );
use MIME::Base64 qw( decode_base64 );
use strict;
use warnings;

my $from = 'az@xx.com';
my $to = 'raxxxfael.xx@xxx.com';
my $to2 = 'xx.xx@xxx.com';
my $boundary = 'frontier';




open my $Initial_File, '<', "summary.pdf";
binmode $Initial_File;
open my $Initial_OutFile, '>', "temp.pdf";
my $buf;
while ( read( $Initial_File, $buf, 60 * 57 ) ) {
print $Initial_OutFile encode_base64( $buf );
}

close $Initial_OutFile;
close $Initial_File;


open INFILE, '<', "temp.pdf";
open my $final_output, '>',"summary2.pdf";
binmode $final_output;
my $buffer;
while ( $buffer = <INFILE> ) {
print $final_output decode_base64( $buffer );
}
my @pdf = $final_output;
close $final_output;
close INFILE;

my $smtp = Net::SMTP->new('xx.xxx.com');
$smtp->mail($from);
$smtp->recipient($to,$to2, { SkipBad => 1 });
$smtp->data();
$smtp->datasend("Subject: Test Email \n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=".$boundary."\n");
$smtp->datasend("\n");
$smtp->datasend("--".$boundary."\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nTest From You \n");
$smtp->datasend("--".$boundary."\n");
$smtp->datasend("Content-Disposition: attachment; filename=summary2.pdf \n");
$smtp->datasend("Content-Type: application/pdf; name=summary2.pdf ");
$smtp->datasend("\n");
$smtp->datasend("@pdf\n");
$smtp->datasend("--".$boundary."--\n");
$smtp->dataend();
# $smtp->quit;

exit;

电子邮件正确发送,但是(显然)在尝试打开 pdf 文件时说它的编码不正确,有没有办法将 PDF 文件缓冲到附件中,使其按原样发送出去?

最佳答案

这是一个如何使用 Net::SMTP 发送多部分邮件消息的工作示例。

提供了用于发送纯文本、纯文本文件和二进制图像 (jpg) 的代码。

希望这可以帮助!

亚历杭德罗

PS。上面提供的代码 RVS 列出了其他几种内容类型,包括 pdf。任何不是纯文本文件的文件都应被视为二进制文件,并以 base64 编码发送。您不需要对其进行解码,因为接收邮件的电子邮件客户端应该负责这一点。

#!/usr/bin/perl

use Net::SMTP;
use strict;
use warnings;

use MIME::Base64 qw( encode_base64 );
#use MIME::Base64 qw( decode_base64 );

my $from = 'from@email.com';
my $to = 'to@email.com';

my $attachBinaryFile= 'test.jpg';
my $attachTextFile = 'test.txt';

my $boundary = 'frontier';

open(DAT, $attachTextFile) || die("Could not open text file!");
my @textFile = <DAT>;
close(DAT);

my $smtp = Net::SMTP->new('your.smtp.com', Timeout => 60) || die("Could not create SMTP object.");
print "Sending mail\n";
$smtp->mail($from);
$smtp->recipient($to, { SkipBad => 1 });
$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Subject: Multi part test\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nToday\'s files are attached:\n");
$smtp->datasend("\nHave a nice day! :)\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Type: application/text; name=\"$attachTextFile\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachTextFile\"\n");
$smtp->datasend("\n");
$smtp->datasend("@textFile\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\n");
$smtp->datasend("Content-Transfer-Encoding: base64\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachBinaryFile\"\n");
$smtp->datasend("\n");
my $buf;
open(DAT, "../uploads/$attachBinaryFile") || die("Could not open binary file!");
binmode(DAT);
local $/=undef;
while (read(DAT, my $picture, 4096)) {
$buf = &encode_base64( $picture );
$smtp->datasend($buf);
}
close(DAT);
$smtp->datasend("\n");
$smtp->datasend("--$boundary\n");
$smtp->dataend();
$smtp->quit;
print "Mail sent\n";
exit;

关于perl - 有没有办法在 Perl 中使用 Net::SMTP 附加 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com