emailsignspam-preventiondkimnette

DKIM with Nette Mail


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?


Solution

  • 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();
        }
    }