我正在尝试在多部分内容电子邮件上附加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文件缓冲到附件中,因为它将其发送出来是吗?
答案 0 :(得分:2)
以下是如何使用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;
答案 1 :(得分:1)
最终手动将MIME:Lite添加到我的用户目录
use lib '/xx/sas/xx/perl';
use MIME::Lite;
open(SMTP,'/xx/sas/xx/perl/MIME/srv.txt') || die("Could not open the file");
my $mail_host = <SMTP>;
close(SMTP);
open(DATA, $ARGV[3] ) || die("Could not open the file");
my @csv = <DATA>;
close(DATA);
foreach (@csv){
$textStr.= $_;
}
$msg = MIME::Lite->new (
From => $ARGV[0],
To => $ARGV[1],
Subject => $ARGV[2],
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
$msg->attach (
Type => 'TEXT',
Data => $textStr
) or die "Error adding the text message part: $!\n";
foreach my $file (split(',', $ARGV[4])) {
$content_type='TEXT';
if ( $file =~ /\.gif$/i ){ $content_type ='image/gif'}
if ( $file =~ /\.jpg$/i ){ $content_type ='image/jpeg'}
if ( $file =~ /\.zip$/i ){ $content_type ='application/zip'}
if ( $file =~ /\.html$/i ){ $content_type ='text/html'}
if ( $file =~ /\.pdf$/i ){ $content_type ='application/pdf'}
if ( $file =~ /\.xls$/i ){ $content_type ='application/vnd.ms-excel'}
if ( $file =~ /\.log$/i ){ $content_type ='application/octet-stream'}
$msg->attach (
Type => $content_type,
Path => $file,
Filename => $file,
Disposition => 'attachment'
) or die "Error adding $file: $!\n";
}
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
答案 2 :(得分:0)
这......看起来不对:
open FILE, "summary.pdf" or die $!;
my @txt = <FILE>;
close FILE;
...
$smtp->datasend("@txt\n");
我相信你所寻求的问题在于这两行代码。您正在使用字符串阅读器读取二进制格式,然后将该二进制内插到字符串中。
尝试:
...
open FILE, "summary.pdf" or die $!;
binmode(FILE);
my $data = do { local $/; <FILE> };
close FILE;
...
$smtp->datasend($data);
或沿着这些方向的东西。