E-Mail-Versand auf SPF/DKIM-Alignment umstellen
This commit is contained in:
parent
a0e85de7bd
commit
34304d18dd
7 changed files with 251 additions and 16 deletions
48
EMAIL_DNS.md
Normal file
48
EMAIL_DNS.md
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# E-Mail-DNS für motorrad-ig.de
|
||||
|
||||
Der Versand erfolgt jetzt über `info@motorrad-ig.de` mit DKIM-Signatur (`mail._domainkey`).
|
||||
|
||||
## SPF (bereits vorhanden)
|
||||
|
||||
```
|
||||
v=spf1 a mx include:spf.kasserver.com ~all
|
||||
```
|
||||
|
||||
## DKIM
|
||||
|
||||
Name:
|
||||
```
|
||||
mail._domainkey.motorrad-ig.de
|
||||
```
|
||||
|
||||
Typ: `TXT`
|
||||
|
||||
Wert (eine Zeile):
|
||||
|
||||
```
|
||||
v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr3BP70f7TTqmayHg4CJjVYIY/yqdJUfinZ3LkxHaSjgscb5dj3WSmT3HxcQZwKhNhyzQyD+GVF/ekQLb9G0SXBrP7ZU00WvQWqvYiXikELmcH/a0I7KLSlk2vsLjotE0mWuyZZhNWpGG7CTEH5CL39MIGAl6uSGOtd0yyYaIvTtBsF3O9/dxMWsWF3GPSf7pA17yQA8tdMO3i5TiGhAkvDPmP4gNzG7kku46WX8Ek26TWNOhFt7QsL+Rht7npK1Jjb1jgkAoT5mM5s7zrgqNd97mxegTOJyZ0fjEjv5QfMk3Ao1jJpZf67zouYx+cm1KR46Nj1DnSqwJWiOoiqZe9wIDAQAB
|
||||
```
|
||||
|
||||
Alternativ in 255-Zeichen-Blöcken (KAS kann den Wert automatisch splitten):
|
||||
|
||||
```
|
||||
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr3BP70f7TTqmayHg4CJjVYIY/yqdJUfinZ3LkxHaSjgscb5dj3WSmT3HxcQZwKhNhyzQyD+GVF/ekQLb9G0SXBrP7ZU00WvQWqvYiXikELmcH/a0I7KLSlk2vsLjotE0mWuyZZhNWpGG7CTEH5CL39MIGAl6uSGOtd0yyYaIvTtBsF3O9/dxMWsWF3GPSf7pA"
|
||||
"17yQA8tdMO3i5TiGhAkvDPmP4gNzG7kku46WX8Ek26TWNOhFt7QsL+Rht7npK1Jjb1jgkAoT5mM5s7zrgqNd97mxegTOJyZ0fjEjv5QfMk3Ao1jJpZf67zouYx+cm1KR46Nj1DnSqwJWiOoiqZe9wIDAQAB"
|
||||
```
|
||||
|
||||
## DMARC (empfohlen, zunächst auf Monitoring)
|
||||
|
||||
Name:
|
||||
```
|
||||
_dmarc.motorrad-ig.de
|
||||
```
|
||||
|
||||
Typ: `TXT`
|
||||
|
||||
Wert:
|
||||
|
||||
```
|
||||
v=DMARC1; p=none; rua=mailto:info@motorrad-ig.de; adkim=s; aspf=s
|
||||
```
|
||||
|
||||
Nach erfolgreicher Prüfung kann `p=none` auf `p=quarantine` gesetzt werden.
|
||||
1
motorradig/.gitignore
vendored
1
motorradig/.gitignore
vendored
|
|
@ -5,6 +5,7 @@
|
|||
/storage/img_storage/*
|
||||
!/storage/img_storage/.gitkeep
|
||||
/storage/logs/
|
||||
/storage/dkim/
|
||||
/storage/*.log
|
||||
/public/mix-manifest.json
|
||||
.DS_Store
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ class AdminController
|
|||
. "Viele Grüße\n"
|
||||
. "Motorrad-IG\n";
|
||||
|
||||
if (!send_mail($applicantEmail, 'Dein Mitgliedsantrag wurde angenommen', $body, 'Motorrad-IG <motorradig@aol.com>')) {
|
||||
if (!send_mail($applicantEmail, 'Dein Mitgliedsantrag wurde angenommen', $body, (string) config('mail.reply_to'))) {
|
||||
app_log('warning', 'Annahme-E-Mail an Antragsteller fehlgeschlagen', [
|
||||
'application_id' => (int) $id,
|
||||
'email' => $applicantEmail,
|
||||
|
|
@ -172,7 +172,7 @@ class AdminController
|
|||
. "Viele Grüße\n"
|
||||
. "Motorrad-IG\n";
|
||||
|
||||
if (!send_mail($applicantEmail, 'Dein Mitgliedsantrag wurde abgelehnt/gelöscht', $body, 'Motorrad-IG <motorradig@aol.com>')) {
|
||||
if (!send_mail($applicantEmail, 'Dein Mitgliedsantrag wurde abgelehnt/gelöscht', $body, (string) config('mail.reply_to'))) {
|
||||
app_log('warning', 'Ablehnungs-E-Mail an Antragsteller fehlgeschlagen', [
|
||||
'application_id' => (int) $id,
|
||||
'email' => $applicantEmail,
|
||||
|
|
|
|||
|
|
@ -174,13 +174,22 @@ class HomeController
|
|||
. "Antrag im Admin-Bereich ansehen:\n"
|
||||
. " " . $adminUrl . "\n";
|
||||
|
||||
send_mail(
|
||||
if (!send_mail(
|
||||
(string) config('app.membership_notify_email'),
|
||||
'Neuer Mitgliedsantrag: ' . $vorname . ' ' . $nachname,
|
||||
$body,
|
||||
'Motorrad-IG <motorradig@aol.com>',
|
||||
$email
|
||||
);
|
||||
)) {
|
||||
app_log('warning', 'Admin-Benachrichtigung über neuen Mitgliedsantrag fehlgeschlagen', [
|
||||
'application_id' => $application->id,
|
||||
'email' => (string) config('app.membership_notify_email'),
|
||||
]);
|
||||
} else {
|
||||
app_log('info', 'Admin-Benachrichtigung über neuen Mitgliedsantrag gesendet', [
|
||||
'application_id' => $application->id,
|
||||
'email' => (string) config('app.membership_notify_email'),
|
||||
]);
|
||||
}
|
||||
|
||||
$applicantBody = "Hallo " . trim($vorname . ' ' . $nachname) . ",\n\n"
|
||||
. "vielen Dank für deinen Mitgliedsantrag. Wir haben deinen Antrag am "
|
||||
|
|
@ -192,7 +201,7 @@ class HomeController
|
|||
. "Motorrad-IG\n"
|
||||
. $baseUrl . "\n";
|
||||
|
||||
if (!send_mail($email, 'Dein Mitgliedsantrag bei der Motorrad-IG', $applicantBody, 'Motorrad-IG <motorradig@aol.com>')) {
|
||||
if (!send_mail($email, 'Dein Mitgliedsantrag bei der Motorrad-IG', $applicantBody, (string) config('mail.reply_to'))) {
|
||||
app_log('warning', 'Bestätigungs-E-Mail an Antragsteller fehlgeschlagen', [
|
||||
'application_id' => $application->id,
|
||||
'email' => $email,
|
||||
|
|
|
|||
136
motorradig/src/Core/DkimSigner.php
Normal file
136
motorradig/src/Core/DkimSigner.php
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* Minimaler DKIM-Signer für text/plain-E-Mails (relaxed/relaxed, RSA-SHA256).
|
||||
*
|
||||
* Der private Schlüssel liegt außerhalb des Web-Roots unter storage/dkim/.
|
||||
* Der passende öffentliche Schlüssel muss als TXT-Record
|
||||
* <selector>._domainkey.<domain> veröffentlicht werden.
|
||||
*/
|
||||
class DkimSigner
|
||||
{
|
||||
/** @return string|null Vollständiger DKIM-Signature-Header oder null bei Fehler. */
|
||||
public static function sign(
|
||||
string $headers,
|
||||
string $body,
|
||||
string $domain,
|
||||
string $selector,
|
||||
string $privateKeyPath
|
||||
): ?string {
|
||||
if (!is_file($privateKeyPath) || !is_readable($privateKeyPath)) {
|
||||
error_log('DkimSigner: privater Schlüssel nicht lesbar: ' . $privateKeyPath);
|
||||
return null;
|
||||
}
|
||||
|
||||
$privateKey = @file_get_contents($privateKeyPath);
|
||||
|
||||
if ($privateKey === false) {
|
||||
error_log('DkimSigner: privater Schlüssel konnte nicht gelesen werden: ' . $privateKeyPath);
|
||||
return null;
|
||||
}
|
||||
|
||||
$headerMap = self::parseHeaders($headers);
|
||||
|
||||
if (!isset($headerMap['from'])) {
|
||||
error_log('DkimSigner: From-Header fehlt.');
|
||||
return null;
|
||||
}
|
||||
|
||||
$signedNames = [];
|
||||
|
||||
foreach ([
|
||||
'from',
|
||||
'reply-to',
|
||||
'date',
|
||||
'message-id',
|
||||
'mime-version',
|
||||
'content-type',
|
||||
'content-transfer-encoding',
|
||||
] as $name) {
|
||||
if (isset($headerMap[$name])) {
|
||||
$signedNames[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
$bodyHash = base64_encode(hash('sha256', self::canonicalBody($body), true));
|
||||
|
||||
$dkimValue = 'v=1; a=rsa-sha256; c=relaxed/relaxed'
|
||||
. '; d=' . $domain
|
||||
. '; s=' . $selector
|
||||
. '; t=' . time()
|
||||
. '; h=' . implode(':', $signedNames)
|
||||
. '; bh=' . $bodyHash
|
||||
. '; b=';
|
||||
|
||||
$signingData = '';
|
||||
|
||||
foreach ($signedNames as $name) {
|
||||
$signingData .= $name . ':' . self::canonicalHeaderValue($headerMap[$name]) . "\r\n";
|
||||
}
|
||||
|
||||
$signingData .= 'dkim-signature:' . self::canonicalHeaderValue($dkimValue) . "\r\n";
|
||||
|
||||
$signature = '';
|
||||
|
||||
if (!openssl_sign($signingData, $signature, $privateKey, OPENSSL_ALGO_SHA256)) {
|
||||
error_log('DkimSigner: openssl_sign() fehlgeschlagen.');
|
||||
return null;
|
||||
}
|
||||
|
||||
return 'DKIM-Signature: ' . $dkimValue . base64_encode($signature);
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private static function parseHeaders(string $headers): array
|
||||
{
|
||||
$map = [];
|
||||
$current = null;
|
||||
|
||||
foreach (preg_split('/\r\n|\n|\r/', $headers) ?: [] as $line) {
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($line[0] === ' ' || $line[0] === "\t") && $current !== null) {
|
||||
$map[$current] .= ' ' . trim($line);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^([A-Za-z0-9-]+):\s*(.*)$/', $line, $matches)) {
|
||||
$current = strtolower($matches[1]);
|
||||
$map[$current] = $matches[2];
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
private static function canonicalHeaderValue(string $value): string
|
||||
{
|
||||
$value = preg_replace('/\r\n|\n|\r/', ' ', $value) ?? '';
|
||||
$value = preg_replace('/[ \t]+/', ' ', $value) ?? '';
|
||||
|
||||
return trim($value);
|
||||
}
|
||||
|
||||
private static function canonicalBody(string $body): string
|
||||
{
|
||||
$body = str_replace(["\r\n", "\r"], "\n", $body);
|
||||
|
||||
$lines = array_map(static function (string $line): string {
|
||||
$line = preg_replace('/[ \t]+/', ' ', $line) ?? $line;
|
||||
|
||||
return rtrim($line);
|
||||
}, explode("\n", $body));
|
||||
|
||||
while ($lines !== [] && end($lines) === '') {
|
||||
array_pop($lines);
|
||||
}
|
||||
|
||||
return implode("\r\n", $lines) . "\r\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -23,6 +23,17 @@ return [
|
|||
'membership_notify_email' => getenv('MEMBERSHIP_NOTIFY_EMAIL') ?: 'dirk@fischergruppe.de',
|
||||
],
|
||||
|
||||
'mail' => [
|
||||
'from_address' => getenv('MAIL_FROM_ADDRESS') ?: 'info@motorrad-ig.de',
|
||||
'from_name' => getenv('MAIL_FROM_NAME') ?: 'Motorrad-IG',
|
||||
'reply_to' => getenv('MAIL_REPLY_TO') ?: 'motorradig@aol.com',
|
||||
|
||||
// DKIM: privater Schlüssel liegt außerhalb des Web-Roots.
|
||||
'dkim_domain' => getenv('MAIL_DKIM_DOMAIN') ?: 'motorrad-ig.de',
|
||||
'dkim_selector' => getenv('MAIL_DKIM_SELECTOR') ?: 'mail',
|
||||
'dkim_private_key' => $appRoot . '/storage/dkim/motorrad-ig.de.private',
|
||||
],
|
||||
|
||||
'db' => [
|
||||
'host' => getenv('DB_HOST') ?: '127.0.0.1',
|
||||
'port' => getenv('DB_PORT') ?: '3306',
|
||||
|
|
|
|||
|
|
@ -320,26 +320,56 @@ function today(): string
|
|||
}
|
||||
|
||||
/**
|
||||
* Versendet eine Text-E-Mail über die PHP-Mailfunktion (sendmail/SMTP des Servers).
|
||||
* Liefert true bei Erfolg. Schlägt der Versand fehl, bleibt die Rückgabe false,
|
||||
* ohne die laufende Anfrage zu unterbrechen.
|
||||
* Versendet eine Text-E-Mail über die PHP-Mailfunktion (sendmail des Servers).
|
||||
* Absender und Envelope-Absender kommen aus der Konfiguration, damit SPF-
|
||||
* Alignment für motorrad-ig.de gegeben ist. Zusätzlich wird DKIM signiert.
|
||||
*/
|
||||
function send_mail(string $to, string $subject, string $body, string $from = '', string $replyTo = ''): bool
|
||||
function send_mail(string $to, string $subject, string $body, string $replyTo = ''): bool
|
||||
{
|
||||
$headers = [];
|
||||
if ($from !== '') {
|
||||
$headers[] = 'From: ' . $from;
|
||||
$fromAddress = (string) config('mail.from_address');
|
||||
$fromName = (string) config('mail.from_name');
|
||||
$dkimDomain = (string) config('mail.dkim_domain');
|
||||
|
||||
if (!filter_var($fromAddress, FILTER_VALIDATE_EMAIL)) {
|
||||
error_log('send_mail: ungültige Absenderadresse in config(mail.from_address)');
|
||||
return false;
|
||||
}
|
||||
if ($replyTo !== '') {
|
||||
|
||||
$headers = [];
|
||||
$headers[] = 'From: ' . ($fromName !== '' ? $fromName . ' <' . $fromAddress . '>' : $fromAddress);
|
||||
|
||||
if ($replyTo !== '' && filter_var($replyTo, FILTER_VALIDATE_EMAIL)) {
|
||||
$headers[] = 'Reply-To: ' . $replyTo;
|
||||
}
|
||||
|
||||
$headers[] = 'Date: ' . date('r');
|
||||
$headers[] = 'Message-ID: <' . bin2hex(random_bytes(16)) . '@' . $dkimDomain . '>';
|
||||
$headers[] = 'MIME-Version: 1.0';
|
||||
$headers[] = 'Content-Type: text/plain; charset=UTF-8';
|
||||
$headers[] = 'Content-Transfer-Encoding: 8bit';
|
||||
|
||||
$headerString = implode("\r\n", $headers);
|
||||
|
||||
$dkimHeader = \App\Core\DkimSigner::sign(
|
||||
$headerString,
|
||||
$body,
|
||||
$dkimDomain,
|
||||
(string) config('mail.dkim_selector'),
|
||||
(string) config('mail.dkim_private_key')
|
||||
);
|
||||
|
||||
if ($dkimHeader !== null) {
|
||||
$headerString .= "\r\n" . $dkimHeader;
|
||||
} else {
|
||||
app_log('warning', 'E-Mail ohne DKIM-Signatur versendet', [
|
||||
'to' => $to,
|
||||
]);
|
||||
}
|
||||
|
||||
// Betreff als RFC 2047 (UTF-8/Base64) kodieren, damit Umlaute korrekt ankommen.
|
||||
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
|
||||
$subject = mb_encode_mimeheader($subject, 'UTF-8', 'B', "\r\n");
|
||||
|
||||
return @mail($to, $subject, $body, implode("\r\n", $headers));
|
||||
return @mail($to, $subject, $body, $headerString, '-f' . $fromAddress);
|
||||
}
|
||||
|
||||
/** Asset-Pfad unter /. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue