Halon plugin for signing and verifying email with DKIM2 (using the turscar/dkim2 library). It provides HSL functions for generating Message-Instance and DKIM2-Signature headers and validating DKIM2-signed messages.
Follow the instructions in our manual to add our package repository and then run the below command.
apt-get install halon-extras-dkim2
yum install halon-extras-dkim2
tdnf install -y halon-extras-dkim2
These functions needs to be imported from the extras://dkim2 module path.
Creates the Message-Instance and DKIM2-Signature headers for a message.
Params
- mail
File- email file - mailfrom
string- envelope sender - rcptto
array- envelope recipients - selector
string- DNS key selector - domain
string- signing domain - privatekey
PrivateKey|string- PEM-encoded PKCS#8 Ed25519/RSA or PKCS#1 RSA private key - dkim2options
array- Optional signing options
The following options are available in the dkim2options array:
noncestring - Optional signer-defined value placed in then=tag. Limited to 64 printable ASCII characters and must not contain a semicolontimestampnumber - Unix timestamp; the current time is used when omittedexplodedboolean - Addsexplodedto thef=tag, reporting that the message is being sent to more than one recipient. Defaults tofalsedonotexplodeboolean - Addsdonotexplodeto thef=tag, requesting that the message not be sent to more than one recipient. Defaults tofalsedonotmodifyboolean - Addsdonotmodifyto thef=tag, requesting that the message body and existing headers not be modified. Defaults tofalsefeedbackboolean - Addsfeedbackto thef=tag, requesting feedback about how the message is handled during and after delivery. Defaults tofalsemodificationsarray - Optional modification recipe in the dkim2 package's format, containingh(header recipes) and/orb(body steps). The recipe reconstructs the previous message from the already modified message passed todkim2_sign. It is encoded in theMessage-Instanceheader'sr=tag and protected by the signature. Omit it when no recipe is needed
The other options are encoded into and protected by the generated DKIM2-Signature header. See sections 7.3 and 7.9 of the DKIM2 specification.
Returns
An associative array with result, error, and headers properties. On success, headers contains complete header fields, including their trailing CRLF, in the order in which they must be prepended to the original message.
Example
import { dkim2_sign } from "extras://dkim2";
$mail = MailMessage::File(File("simple_email.eml"));
$privatekey = File::read("dkim2_ed25519_test_key.private");
$rcptto = ["recipient@example.com"];
$dkim2options = [
"timestamp" => time(),
];
$signed = dkim2_sign(
$mail->toFile(),
"sender@test1.dkim2.com",
$rcptto,
"ed25519",
"test1.dkim2.com",
$privatekey,
$dkim2options
);
if (!$signed["result"])
{
throw Exception($signed["error"]);
}
foreach ($signed["headers"] as $header)
$mail->modifyContent(0, 0, $header);
echo $mail->toString();
Verify a DKIM2-Signature.
Params
- mail
File- email file - mailfrom
string- Sender of the email - rcptto
array- Array of recievers - dkim2options
array- Optional DKIM2 options
The following options are available in the dkim2options array
ignoretimestamp- Reject anything with a timestamp more than 14 days old. Defaults tofalsetimeout- Maximum time in seconds allowed for DNS key lookups during verification. Defaults to5
Returns
An associative array with a result and error property. error is set if an error occurs.
Example
import { dkim2_verify } from "extras://dkim2";
$mail = MailMessage::File(File("simple_email.eml"));
$rcptto = ["recipient@example.com"];
$dkim2options = [
"ignoretimestamp" => true,
];
$verified = dkim2_verify($mail->toFile(), "sender@test1.dkim2.com", $rcptto, $dkim2options);
if (!$verified["result"])
{
echo "dkim2 signature not found";
}
else if ($verified["result"] != "pass")
{
throw $verified["error"];
}
else
{
echo "verfied ok!";
}