Support the ongoing development of Laravel.io →
Laravel Mail
Last updated 2 years ago.
0

You can use simonschaufi/laravel-dkim tool with Laravel 9 compatibility. This tool use a custom mailer with dkim signature:

$signer = new DkimSigner(file_get_contents($privateKey), $domain, $selector, [], config('dkim.passphrase'));
$signedEmail = $signer->sign($message->getSymfonyMessage());
$symfonyMessage->setHeaders($signedEmail->getHeaders());
Last updated 2 years ago.

hostbrook liked this reply

1

@rafadigreruiz here is the problem: emails are signed with DKIM with the package "simonschaufi/laravel-dkim" but mail-tester.com shows "Your DKIM signature is not valid" - have you tried to test if this package is correctly signed with DKIM? Thanks!

Last updated 2 years ago.
0

@hostbrook yes, I'm tested the DKIM signature with mail-tester.com/spf-dkim-check. Must be configured in your name server previously and not be used from local environment. You can generate the signature using socketlabs.com/dkim/generator tool. In addition, I'm tested the package sending emails from Laravel application.

Last updated 2 years ago.
0

Well, this is what I found. The package 'simonschaufi/laravel-dkim' works well and DKIM is valid only in case of email was built either with HTML or PLAIN TEXT template. In my case, all my emails have both HTML and PLAIN TEXT, because this is a good practice. For example, how I build a message:

class ContactForm extends Mailable
{
    ...

    public function build()
    {
        $this->subject($this->mailData['subject'])
            ->view('mail.contact-form_html')
            ->text('mail.contact-form_plain');
        return $this;
    }
}

So, if you use both templates to build the email (what is the right way) you get an invalid DKIM signature. Actually, SWIFT mailer at Laravel 8 didn't have this issue.

Next, I have dug this issue with Symfony mailer and found the possible way on how to fix it, here: https://github.com/symfony/symfony/issues/39354#issuecomment-894379296

Hopefully authors of 'simonschaufi/laravel-dkim' package can modify it and fix the issue.

P.S. Actually, if DKIM valid or not - you can check it even from the local environment. I used mailtrap.io to check outgoing emails for local environment and in case if email built from either one HTML or TEXT template - there no problem, it shows DKIM is valid.

rafadigreruiz liked this reply

1

Hi @hostbrook there is a way to make dkim signature in the emails without using any tool as your first example:

class ContactForm extends Mailable
{
    use Queueable, SerializesModels;

    public $mailData;

    public function __construct($mailData)
    {
        $this->mailData = $mailData;
    }

    public function build()
    {
        $mailData = $this->mailData;

        $htmlView = 'mail.contact-form';
        $plainView = 'mail.contact-form_plain';

        $htmlEmail = view($htmlView, compact('mailData'))->render();
        $plainEmail = view($plainView, compact('mailData'))->render();

        return $this->subject($this->mailData['subject'])
            ->view($htmlView)
            ->text($plainView)
            ->withSymfonyMessage(function (Email $message) use ($htmlEmail, $plainEmail) {
                $message
                    ->html($htmlEmail)
                    ->text($plainEmail);

                $signer = new DkimSigner(
                    config('mail.dkim_private_key'),
                    config('mail.dkim_domain'),
                    config('mail.dkim_selector')
                );

                $signedEmail = $signer->sign($message);

                $message->setHeaders($signedEmail->getHeaders());
            });
    }
}

By this way, you can customize the dkim signature implementation as you like to prevent the Symfony mailer issue when sending with html and text parts.

hostbrook liked this reply

1
Solution selected by @hostbrook

This is exactly the answer I was looking for. But the same problem I described in this thread above: the solution doesn't work if the email is built with both HTML and plain text. In this case, the email is signed with DKIM but the signature is not valid. But if the email is built either with HTML or Plain text only - this solution works. Symfony team needs to fix the issue with the DKIM signer or Laravel team needs to find a way how to bypass the issue in the Symfony mailer.

rafadigreruiz liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.