gryphonshafer / Email-Mailer

Multi-purpose emailer for HTML, auto-text, attachments, and templates
1 stars 1 forks source link

Missing Content-Type and Content-Transfer-Encoding when text only #6

Closed ldidry closed 3 years ago

ldidry commented 3 years ago

Hello,

I’m working on a Mojolicious plugin to send mail with Email::Mailer and my anti-spam, rspamd, complains about missing charset (symbol R_MISSING_CHARSET) and missing content-type (symbol BROKEN_CONTENT_TYPE) when sending a text-only mail.

My workaround is quite simple:

if (defined($args{text}) && !defined($args{html})) {
    $args{'Content-Transfer-Encoding'}     = 'quoted-printable'         unless defined $args{'Content-Transfer-Encoding'};
    $args{'Content-Type'}                  = 'text/plain; charset=utf8' unless defined $args{'Content-Type'};
    (my $encoding = $args{'Content-Type'}) =~ s/.*charset=([^;]+);?.*/$1/;
    $args{text}                            = encode($encoding, $args{text});
}
Email::Mailer->send(%args);

I think that should be done at Email::Mailer’s level, don’t you?

For html + text mails, this could be done with text_body_attributes.

ldidry commented 3 years ago

I wonder if the subject shouldn’t be mimeword encoded by Email::Mailer too :thinking:

gryphonshafer commented 3 years ago

Good suggestions. Thanks. I've added both via 8643e84. Let me know if you want any help with your plugin. I use Email::Mailer inside Log::Dispatch::Email::Mailer, so I'd love to learn about what you're doing and see if there's anything I should copy.

ldidry commented 3 years ago

My plugin, still unpublished, is on https://framagit.org/fiat-tux/mojolicious/mojolicious-plugin-emailmailer.

I think it may be a good idea to have Email::Mailer automatically creating the Message-ID header too.

This is how I do it on my plugin (it may not be the best way to do it, but it works):

    $args{'Message-ID'} = md5_sum(''.\%args).'@mojo-plugin-emailmailer'
        unless defined _header_key('Message-ID', %args);
gryphonshafer commented 3 years ago

Message-ID is being generated by Email::MIME below Email::Mailer. But of course, users can specify their own like you have if they want something different.

ldidry commented 3 years ago

I’ll retest that, but I remember having no Message-ID :thinking:

ldidry commented 3 years ago

Email::MIME does not generate a Message-ID header.

My test script:

use Email::Mailer;
use Email::Sender::Transport::SMTP;

Email::Mailer->send(
    from     => 'foo@1.example.org',
    to         => 'bar@2.example.org',
    subject => 'test',
    text      => 'voyons s’il y a un message-id'
);

And here the result of

        use Data::Dumper;
        print Dumper $email_mime;

inserted in Email::Mailer just before the sendmail call:

Click to see the dump ```perl $VAR1 = bless( { 'body' => \'voyons s=E2=80=99il y a un message-id= ', 'encode_check' => 1, 'header' => bless( { 'mycrlf' => ' ', 'headers' => [ 'Content-Transfer-Encoding', [ 'quoted-printable', 'Content-Transfer-Encoding: quoted-printable' ], 'Content-Type', [ 'text/plain; charset=us-ascii', 'Content-Type: text/plain; charset=us-ascii' ], 'From', [ 'foo@1.example.org', 'From: foo@1.example.org' ], 'Subject', [ 'test', 'Subject: test' ], 'To', [ 'foo@1.example.org', 'To: foo@1.example.org' ], 'Date', [ 'Tue, 15 Sep 2020 18:03:37 +0200', 'Date: Tue, 15 Sep 2020 18:03:37 +0200' ], 'MIME-Version', [ '1.0', 'MIME-Version: 1.0' ] ] }, 'Email::MIME::Header' ), 'mycrlf' => ' ', 'ct' => { 'subtype' => 'plain', 'attributes' => { 'charset' => 'us-ascii' }, 'discrete' => 'text', 'type' => 'text', 'composite' => 'plain' }, 'parts' => [], 'body_raw' => 'voyons s=E2=80=99il y a un message-id= ' }, 'Email::MIME' ); ```

When sending the mail with sendmail transport, the local mail agent adds a Message-ID before sending the mail to the recipient server, but if I send the mail with a SMTP, only the recipient server adds a Message-ID (not the SMTP server), and that’s added after the antispam scan (and the lacks of Message-ID increases the spam score (MISSING_MID on rspamd)).

Could you run some tests and confirm (or infirm) my diagnostic, please?

gryphonshafer commented 3 years ago

Hm. OK. I've included Message-Id auto-generation in Email::Mailer (if it's not explicitly set).

ldidry commented 3 years ago

:+1:

ldidry commented 3 years ago

My plugin is now published: https://metacpan.org/pod/Mojolicious::Plugin::EmailMailer