Perl使用net :: smtp发送带附件的电子邮件

时间:2015-07-29 07:25:18

标签: perl email smtp attachment

我有问题!我发送带有smtp的邮件,但我无法发送附件。我需要发送附件! 我有这两行,但它不起作用。

$NET_SMTP_MODUL->datasend("Content-Disposition: attachment; filename=". $MAILPARAM_ATTACHMENT ."\n");
$NET_SMTP_MODUL->datasend("Content-Type: application/text; name=Attachment.txt");

3 个答案:

答案 0 :(得分:2)

使用Net :: SMTP会让您的生活变得更加艰难。谁有时间手工制作SMTP交易?

我建议改为查看Email::Stuffer

答案 1 :(得分:1)

检查以下网址以发送带附件的电子邮件

答案 2 :(得分:0)

那么......做什么?

以下是使用Email::MIMEIO::AllEmail::Sender::Simple的示例。

use Email::MIME; # yum install perl-Email-MIME
use IO::All; # yum install perl-IO-All
use Email::Sender::Simple qw(sendmail); # yum install perl-Email-Sender;
use Sys::Hostname;

my $hostname = hostname;
my $pwuid    = getpwuid( $< );
my $to       = 'someone@example.com';
my $cc       = join(',', ('foo@example.com','bar@example.com'));
my $from     = "$pwuid\@$hostname";

# We have a text part and an CSV part
# Send a file identified by `$filename` (a temporary file, actually) that
# should appear to be named `$finalFilename`.

my @parts;

push @parts, Email::MIME->create(
   attributes => {
      filename     => $finalFilename,
      content_type => 'text/csv',
      disposition  => 'attachment',
      encoding     => 'base64',
      name         => $finalFilename
   },
   body => io($filename)->all
);

# Create an inline text part

my $txtmsg = <<"TXTMSG";
File automatically generated on machine '$hostname' by user '$pwuid'.
TXTMSG

push @parts, Email::MIME->create(
   attributes => {
      content_type => 'text/plain',
      disposition  => 'inline',
      encoding     => 'quoted-printable',
      charset      => 'UTF-8',
   },
   body_str => $txtmsg
);

# Pack it into an email

my $email = Email::MIME->create(
   header_str => [ To      => $to,
                   Cc      => $cc,
                   From    => $from,
                   Subject => $finalFilename ],
   parts => [ @parts ]
);

# Debugging; this should be controlled by a "verbose" flag

print STDERR $email->as_string;

# Send to the local MTA which will forward, hopefully.
# This is the sendmail() of Email::Sender::Simple

my $response = sendmail($email);

# Tell the user

if (!$response) {
   print STDERR "An error occurred, got $response as response from 'sendmail: $Mail::Sendmail::error'\n";
   print STDERR $Mail::Sendmail::log;
   exit 1
}
else {
   print STDERR "Mail given to MTA!\n";
   exit 0
}