perl - 在 perl 中发送多部分邮件

标签 perl email

我正在尝试使用 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;

但是如果我在其他机器上尝试相同的代码,它就会工作文件。 为什么相同的代码在我的机器上不起作用,而在其他机器上却运行良好。

请大家帮忙。

最佳答案

您正在使用一些相当低级的工具来构建您的消息。这可能会起作用,但您需要实现构建 MIME 消息的所有规则 - 这听起来太辛苦了。

每当我想用电子邮件和 Perl 做某事时,我都会在 Email::* 命名空间中寻找合适的模块。我可能会从 Email::MIME 开始,但我注意到现在包含一个指向 Email::Stuffer 的指针,这可能会更简单。

关于perl - 在 perl 中发送多部分邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400923/

相关文章:

regex - 确定与 perl 中的正则表达式匹配的部分

perl - 日期::Manip 未安装

perl - 如何正确处理Perl中的错误

email - 防止电子邮件扫描器激活 "unsubscribe"链接的策略

python - 为什么我必须将 "from module import"与 python 中的电子邮件模块一起使用?

email - nodejs、redis 和 mailparser 无法解析电子邮件

android - Android 应用程序中的 Context.io

perl - 在 Perl 中找不到导出的子例程

php - 从共享主机发送的电子邮件,已阻止。有没有办法避免这种情况?

Perl:latin1 模块中的字符串文字 - 我想要 utf8