1

I planned to sign my mails with php-mail-signature (https://github.com/louisameline/php-mail-signature), but I can't find a method in the Nette-Mail Message class (https://api.nette.org/2.4/Nette.Mail.Message.html) to set the DKIM-value.

Since I thought that would be more or less basic functionality, I'm wondering if it really isn't possible to use DKIM with Nette Mail.

Is it possible, and if so, how do I do it?

Tom Dörr
  • 859
  • 10
  • 22
  • What DKIM value do you want to set? When an email is DKIM-signed, the signature is added as a header to that email. You're advised to not modify neither the email nor its headers after signing as you're likely to break the signature. I don't know Nette, but my guess is that you can just send the signed message as any other message. – Robert Mar 18 '18 at 22:13
  • The issue is that php-mail-signature is giving me the signature and I have to hand it over to Nette-Mail. If I send it like I normally would then the mail is not signed. – Tom Dörr Mar 20 '18 at 06:29

1 Answers1

2

Problem is, php-mail-signature is too much low-level. It returns signed headers as string. You need to parse $signature->get_signed_headers() output and call $message->setHeader() for each of them. If you're looking for some magic method $message->setDkimSignature(), you won't find it. But you can always inherit from Message class and write your own.

This is only non-tested example:

<?php

use mail_signature;
use Nette\Mail\Message;

final class DkimSignedMessage extends Message
{
    /**
     * @var mail_signature
     */
    private $signature;

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

    public function generateMessage(): string
    {
        $message = $this->build();
        $signedHeaders = $this->signature->get_signed_headers(
            $message->getTo(),
            $message->getSubject(),
            $message->getBody(),
            implode("\r\n", $message->getHeaders())
        );

        foreach (explode("\r\n", trim($signedHeaders)) as $header) {
            [$name, $value] = explode(': ', $header);
            $message->setHeader($name, trim($value))
        }

        return $message->getEncodedMessage();
    }
}
Tomáš Jacík
  • 645
  • 4
  • 12