From 798bf268aae4a10cf5cdc5f552de5a3e91d4f0c4 Mon Sep 17 00:00:00 2001 From: Ruben Date: Sun, 1 Feb 2026 20:10:10 +0100 Subject: [PATCH] Add petition-specific SMTP configuration support Allow separate SMTP account for petition emails to improve deliverability through proper SPF/DKIM configuration matching the from address --- custom/petition-cli.php | 16 ++++++++---- custom/plugins/page/petition-form.php | 36 ++++++++++++++++++--------- custom/smtp-config.php.example | 11 +++++++- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/custom/petition-cli.php b/custom/petition-cli.php index 7a746f8..f409ca5 100755 --- a/custom/petition-cli.php +++ b/custom/petition-cli.php @@ -266,8 +266,14 @@ function sendEmail(string $to, string $toName, string $subject, string $body): a $fromEmail = $config['petition']['from_email'] ?? $config['from_email']; $fromName = $config['petition']['from_name'] ?? $config['from_name']; + // Get petition-specific SMTP settings (allows separate SMTP account for better deliverability) + $smtpHost = $config['petition']['host'] ?? $config['host']; + $smtpPort = $config['petition']['port'] ?? $config['port']; + $smtpUser = $config['petition']['username'] ?? $config['username']; + $smtpPass = $config['petition']['password'] ?? $config['password']; + // Pre-flight check - $fp = @fsockopen($config['host'], $config['port'], $errno, $errstr, 10); + $fp = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10); if (!$fp) { return ['success' => false, 'error' => "Tilkobling feilet: {$errno} - {$errstr}"]; } @@ -278,10 +284,10 @@ function sendEmail(string $to, string $toName, string $subject, string $body): a $mail = new \codeworxtech\PHPMailerLite\PHPMailerLite(); - $mail->SetSMTPhost($config['host']); - $mail->SetSMTPport($config['port']); - $mail->SetSMTPuser($config['username']); - $mail->SetSMTPpass($config['password']); + $mail->SetSMTPhost($smtpHost); + $mail->SetSMTPport($smtpPort); + $mail->SetSMTPuser($smtpUser); + $mail->SetSMTPpass($smtpPass); $mail->SetSender([$fromEmail => $fromName]); $mail->AddRecipient([$to => $toName]); diff --git a/custom/plugins/page/petition-form.php b/custom/plugins/page/petition-form.php index 6ece4dd..f650744 100644 --- a/custom/plugins/page/petition-form.php +++ b/custom/plugins/page/petition-form.php @@ -713,11 +713,17 @@ function petitionSendConfirmationEmailInternal(array $data, string $confirmUrl, $subject = petitionT($ctx, 'petition', 'email_subject'); + // Get petition-specific SMTP settings (allows separate SMTP account for better deliverability) + $smtpHost = $config['petition']['host'] ?? $config['host']; + $smtpPort = $config['petition']['port'] ?? $config['port']; + $smtpUser = $config['petition']['username'] ?? $config['username']; + $smtpPass = $config['petition']['password'] ?? $config['password']; + // Pre-flight check - $fp = @fsockopen($config['host'], $config['port'], $errno, $errstr, 10); + $fp = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10); if (!$fp) { $errorMessage = "Connection failed: {$errno} - {$errstr}"; - error_log("Petition SMTP pre-flight failed: {$config['host']}:{$config['port']} - {$errno} - {$errstr}"); + error_log("Petition SMTP pre-flight failed: {$smtpHost}:{$smtpPort} - {$errno} - {$errstr}"); return false; } fclose($fp); @@ -727,10 +733,10 @@ function petitionSendConfirmationEmailInternal(array $data, string $confirmUrl, $mail = new \codeworxtech\PHPMailerLite\PHPMailerLite(); - $mail->SetSMTPhost($config['host']); - $mail->SetSMTPport($config['port']); - $mail->SetSMTPuser($config['username']); - $mail->SetSMTPpass($config['password']); + $mail->SetSMTPhost($smtpHost); + $mail->SetSMTPport($smtpPort); + $mail->SetSMTPuser($smtpUser); + $mail->SetSMTPpass($smtpPass); $recipientName = $data['firstname'] . ' ' . $data['surname']; $mail->SetSender([$fromEmail => $fromName]); @@ -859,11 +865,17 @@ function petitionSendThankYouEmailInternal(string $token, string $deleteUrl, str $subject = petitionT($ctx, 'petition', 'email_thankyou_subject'); + // Get petition-specific SMTP settings (allows separate SMTP account for better deliverability) + $smtpHost = $config['petition']['host'] ?? $config['host']; + $smtpPort = $config['petition']['port'] ?? $config['port']; + $smtpUser = $config['petition']['username'] ?? $config['username']; + $smtpPass = $config['petition']['password'] ?? $config['password']; + // Pre-flight check - $fp = @fsockopen($config['host'], $config['port'], $errno, $errstr, 10); + $fp = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10); if (!$fp) { $errorMessage = "Connection failed: {$errno} - {$errstr}"; - error_log("Petition SMTP pre-flight failed: {$config['host']}:{$config['port']} - {$errno} - {$errstr}"); + error_log("Petition SMTP pre-flight failed: {$smtpHost}:{$smtpPort} - {$errno} - {$errstr}"); return false; } fclose($fp); @@ -873,10 +885,10 @@ function petitionSendThankYouEmailInternal(string $token, string $deleteUrl, str $mail = new \codeworxtech\PHPMailerLite\PHPMailerLite(); - $mail->SetSMTPhost($config['host']); - $mail->SetSMTPport($config['port']); - $mail->SetSMTPuser($config['username']); - $mail->SetSMTPpass($config['password']); + $mail->SetSMTPhost($smtpHost); + $mail->SetSMTPport($smtpPort); + $mail->SetSMTPuser($smtpUser); + $mail->SetSMTPpass($smtpPass); $recipientName = $signature['firstname'] . ' ' . $signature['surname']; $mail->SetSender([$fromEmail => $fromName]); diff --git a/custom/smtp-config.php.example b/custom/smtp-config.php.example index 4506108..6c7fcab 100644 --- a/custom/smtp-config.php.example +++ b/custom/smtp-config.php.example @@ -17,9 +17,18 @@ return [ 'to_email' => 'email@example.com', 'to_name' => 'Stopp Lidelsen', - // Petition-specific overrides (optional) + // Petition-specific settings (optional) // If not set, the default values above will be used + // + // IMPORTANT: For better email deliverability, use a separate SMTP account + // for petitions where the from_email matches the SMTP username. + // This ensures SPF/DKIM/DMARC checks pass. 'petition' => [ + // Separate SMTP account (recommended for deliverability) + // 'host' => 'smtp.example.com', + // 'port' => 587, + // 'username' => 'underskrifter@example.com', + // 'password' => 'petition-smtp-password', // 'from_email' => 'underskrifter@example.com', // 'from_name' => 'Underskriftskampanje', ],