Skip to content

[MAILER] DKIM signing not properly working with text part ? #40131

@geoffrey-brier

Description

@geoffrey-brier

Symfony version(s) affected: At least 5.2.3

Description
I am trying to send emails and sign them using the DkimSigner but for some reason, when I add a text() part, Gmail cannot check the signature and I end up with the following error : dkim=neutral (body hash did not verify) .

How to reproduce
Here is how I create the Email.

    public function email(string $subject, string $from, $to, string $body, string $contentType = 'text/html'): Email
    {
        $email = (new Email())
            ->subject($subject)
            ->from($from)
            ->to($to)
        ;

        if ('text/html' === $contentType) {
            $email->html($body);
            // If I uncomment this, DKIM signature check fails
            // $email->text($this->htmlToText($body));
        } else {
            $email->text($body);
        }

        return $email;
    }

Here is the method that signs and sends the email (pretty straightforward).

    public function send(Email $email): bool
    {
        try {
            $privateKeyPath = __DIR__.'/../../dkim.pkey';

            if (file_exists($privateKeyPath)) {
                $signer = new DkimSigner('file://'.$privateKeyPath, 'mydomain.com', 'mydkimselector');

                $email = $signer->sign($email);
            }

            $this->mailer->send($email);
        } catch (\Exception $e) {
            return false;
        }

        return true;
    }

Additional context
I have added 2 mails which I received : one which passes and the other one which doesn't.

dkim-fail.txt
dkim-pass.txt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions