Home > Net >  Perl sends email as attachment
Perl sends email as attachment

Time:01-24

My question is similar to mailx sending message as bin attachment, but none of the solutions listed there worked for me.

On the contrary, the email is always sent as attachment, even if I remove all the line breaks.

Below, there is my Perl code:

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 ...

Any idea why this is happening?

PS: I'm running the code with Perl 5.10 on RHEL 8.x

CodePudding user response:

In the end I ended up implementing the sending of the e-mail with just the Net::SMTP module. Here the code, if somebody needs it:

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.");
}
  •  Tags:  
  • Related