简体   繁体   English

在Perl中发送多部分邮件

[英]sending multipart mail in perl

i am trying to send a mail through Perl script using net::smtp module.It works fine when i send the normal mail without any attachment.i wont receive any mail. 我正在尝试使用net::smtp模块通过Perl脚本发送邮件。当我发送不带任何附件的普通邮件时,它工作正常。我将不会收到任何邮件。

    use Net::SMTP;
    use MIME::Base64;
    use File::Basename;  
    use MIME::Base64 qw( encode_base64 );
    use MIME::Base64 qw( decode_base64 );
    @attachments = 'C:\Users\ups7kor\Desktop\scripts\commadnline\appending.pl';
    $toAddress = '***';
    $fromAddress = '***';
    $ServerName = '***';
    my $boundary = 'End Of mail';

    my $smtp = Net::SMTP->new($ServerName, Timeout => 60) or print $failureLogHandler  ++$errrorCount.")ERROR:Could not create SMTP object . \n\t please check SMPT Adress in $iniFileData{INI_SMTP_SERVER_NAME} of $iniFileSection{INI_EMAIL} section ";

    $smtp->mail($fromAddress);
    $smtp->recipient($toAddress, { SkipBad => 1 });
    $smtp->data();
    $smtp->datasend("To: $toAddress\n");
    $smtp->datasend("From: $fromAddress\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("MIME-Version: 1.0\n");
    $smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
    $smtp->datasend("--$boundary\n");
    $smtp->datasend("Content-type: text/plain\n");
    $smtp->datasend("Content-Disposition: quoted-printable\n");
    $smtp->datasend("\n $messageBody\n");

    if(@attachments)
    {
    $smtp->datasend("--$boundary\n");
    foreach $attachment (@attachments)
    {
        open(DAT, $attachment) || die("Could not open text file!");
        my @textFile = <DAT>;
        close(DAT); 
        my $filename = basename($attachment);
        $smtp->datasend("Content-Type: application/text; name=\"$filename\"\n");
        $smtp->datasend("Content-Disposition: attachment; filename=\"$filename\"\n");
        $smtp->datasend("\n");
        $smtp->datasend("@textFile\n");
   }
   }



    $smtp->datasend("--$boundary --\n");
    $smtp->dataend();
    $smtp->quit;

But if i try the same code in other machine it works file. 但是,如果我在其他机器上尝试相同的代码,它将可以正常工作。 Why the same code is not working in my machine and working fine in other machine. 为什么相同的代码在我的机器上不起作用而在其他机器上不能正常工作。

Please help out. 请帮忙。

You could use MIME::Lite module. 您可以使用MIME :: Lite模块。

See: https://metacpan.org/pod/MIME::Lite#Create-a-multipart-message 参见: https : //metacpan.org/pod/MIME :: Lite#Create-a-multipart-message

Synopsis: 概要:

### Create the multipart "container":
$msg = MIME::Lite->new(
    From    =>'me@myhost.com',
    To      =>'you@yourhost.com',
    Cc      =>'some@other.com, some@more.com',
    Subject =>'A message with 2 parts...',
    Type    =>'multipart/mixed'
);

### Add the text message part:
### (Note that "attach" has same arguments as "new"):
$msg->attach(
    Type     =>'TEXT',
    Data     =>"Here's the GIF file you wanted"
);

### Add the image part:
$msg->attach(
    Type        =>'image/gif',
    Path        =>'aaa000123.gif',
    Filename    =>'logo.gif',
    Disposition => 'attachment'
);

Update: As per Dave's comment: 更新:根据戴夫的评论:

Check out Email::Stuffer module. 查看电子邮件::填充模块。 Creating multipart message with it is really simple. 使用它创建多部分消息非常简单。

Email::Stuffer->to('Simon Cozens<simon@somewhere.jp>')
              ->from('Santa@northpole.org')
              ->text_body("You've been good this year. No coal for you.")
              ->attach_file('choochoo.gif')
              ->send;

You're using some rather low-level tools for building your message. 您正在使用一些相当底层的工具来构建消息。 That would probably work, but you'd need to implement all of the rules for building MIME messages - which sounds far too much like hard work. 那可能行得通,但是您需要实施所有构建MIME消息的规则-听起来太像是艰苦的工作。

Whenever I want to do something with email and Perl, I look for the appropriate module in the Email::* namespace. 每当我想对email和Perl进行处理时,我都会在Email :: *名称空间中寻找适当的模块。 I'd probably start with Email::MIME , but I note that now includes a pointer to Email::Stuffer , which might well be even simpler. 我可能会从Email :: MIME开始,但是我注意到现在包含了一个指向Email :: Stuffer的指针,它甚至可能更简单。

You can use Mail::Sender module to send mails with attachment with body included. 您可以使用Mail :: Sender模块发送带有附件的邮件,邮件中包含正文。 Just a small example of how to implement if you have this module in place. 这只是有关如何安装此模块的一个小例子。

my $sender = new Mail::Sender {smtp => 'server name', from => 'emailId'}; 我的$ sender =新Mail :: Sender {smtp =>'服务器名称',来自=>'emailId'}; $sender->MailFile( {to => 'xxx.gmail.com,yyy.gmail.com', subject => 'some subject that you want to put', msg => "Body of the mail", file => 'path for the attachment that you need to send'} ); $ sender-> MailFile({to =>'xxx.gmail.com,yyy.gmail.com',主题=>'您要输入的某些主题',msg =>“邮件正文”,文件=> “您需要发送附件的路径”});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM