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

问题描述 投票:0回答:3

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

$NET_SMTP_MODUL->datasend("Content-Disposition: attachment; filename=". $MAILPARAM_ATTACHMENT ."\n");
$NET_SMTP_MODUL->datasend("Content-Type: application/text; name=Attachment.txt");
perl email smtp attachment
3个回答
2
投票

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

我建议改为查看Email::Stuffer


1
投票

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


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       = '[email protected]';
my $cc       = join(',', ('[email protected]','[email protected]'));
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
}
© www.soinside.com 2019 - 2024. All rights reserved.