【问题标题】:Perl sends email as attachmentPerl 将电子邮件作为附件发送
【发布时间】:2022-01-05 07:41:11
【问题描述】:

我的问题与mailx sending message as bin attachment 类似,但其中列出的解决方案均不适合我。

相反,电子邮件始终作为附件发送,即使我删除了所有换行符。

下面是我的 Perl 代码:

my $cmd = "/usr/bin/mailx $Email_SendCharsets -s \"$hostname: $subject\" $Email_Auth -r $retAddr $addresses";
my $rc = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, $cmd);
if ($rc != 0)
{
    print CHLD_IN $message;
    close CHLD_IN;
    ... other things here ...
}
... other things here ...

知道为什么会这样吗?

PS:我在 RHEL 8.x 上使用 Perl 5.10 运行代码

【问题讨论】:

  • 我猜open3 来自IPC::Open3,对吧?
  • @choroba 是的
  • 邮件是否包含\x20(空格)下方的任何字符?
  • 我也尝试在打印之前从邮件中删除所有不可打印的字符,但没有成功。
  • 信息不足,无法继续。

标签: perl mailx


【解决方案1】:

最后我只用Net::SMTP 模块实现了电子邮件的发送。这里是代码,如果有人需要的话:

if (my $smtp = Net::SMTP->new($smtpHost, Debug => $Debug)) {
    if($smtp->auth($username, $password)) {
        $smtp->mail($retAddr);
        $smtp->to(@recipients);

        $smtp->data();
        $smtp->datasend("To: $recipientsString\n");
        $smtp->datasend("Subject: $hostname: $subject\n");
        $smtp->datasend("Content-Type: text/plain; charset=$charset\n");
        $smtp->datasend("\n");
        $smtp->datasend("$message\n");
        $smtp->dataend();
    }

    my $rc = $smtp->ok();
    if($rc == 0) {
        my $errorMessage = $smtp->message();
        LogError("Mail error: $errorMessage. Msg subject: $subject.");
    }
    $smtp->quit();

    return $rc;
} else {
    LogError("SMTP connection failed. Msg subject: $subject.");
}

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2015-09-09
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多