2

我正在尝试在多部分内容电子邮件上附加 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 文件缓冲到附件中,使其按原样发送出去?

4

3 回答 3

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;
于 2014-02-25T04:30:03.260 回答
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;
于 2012-05-17T23:06:05.837 回答
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);

或类似的东西。

于 2012-05-17T23:03:34.963 回答