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
This commit is contained in:
Ruben 2026-02-01 20:10:10 +01:00
parent 609bd5dd34
commit 798bf268aa
3 changed files with 45 additions and 18 deletions

View file

@ -266,8 +266,14 @@ function sendEmail(string $to, string $toName, string $subject, string $body): a
$fromEmail = $config['petition']['from_email'] ?? $config['from_email']; $fromEmail = $config['petition']['from_email'] ?? $config['from_email'];
$fromName = $config['petition']['from_name'] ?? $config['from_name']; $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 // Pre-flight check
$fp = @fsockopen($config['host'], $config['port'], $errno, $errstr, 10); $fp = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10);
if (!$fp) { if (!$fp) {
return ['success' => false, 'error' => "Tilkobling feilet: {$errno} - {$errstr}"]; 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 = new \codeworxtech\PHPMailerLite\PHPMailerLite();
$mail->SetSMTPhost($config['host']); $mail->SetSMTPhost($smtpHost);
$mail->SetSMTPport($config['port']); $mail->SetSMTPport($smtpPort);
$mail->SetSMTPuser($config['username']); $mail->SetSMTPuser($smtpUser);
$mail->SetSMTPpass($config['password']); $mail->SetSMTPpass($smtpPass);
$mail->SetSender([$fromEmail => $fromName]); $mail->SetSender([$fromEmail => $fromName]);
$mail->AddRecipient([$to => $toName]); $mail->AddRecipient([$to => $toName]);

View file

@ -713,11 +713,17 @@ function petitionSendConfirmationEmailInternal(array $data, string $confirmUrl,
$subject = petitionT($ctx, 'petition', 'email_subject'); $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 // Pre-flight check
$fp = @fsockopen($config['host'], $config['port'], $errno, $errstr, 10); $fp = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10);
if (!$fp) { if (!$fp) {
$errorMessage = "Connection failed: {$errno} - {$errstr}"; $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; return false;
} }
fclose($fp); fclose($fp);
@ -727,10 +733,10 @@ function petitionSendConfirmationEmailInternal(array $data, string $confirmUrl,
$mail = new \codeworxtech\PHPMailerLite\PHPMailerLite(); $mail = new \codeworxtech\PHPMailerLite\PHPMailerLite();
$mail->SetSMTPhost($config['host']); $mail->SetSMTPhost($smtpHost);
$mail->SetSMTPport($config['port']); $mail->SetSMTPport($smtpPort);
$mail->SetSMTPuser($config['username']); $mail->SetSMTPuser($smtpUser);
$mail->SetSMTPpass($config['password']); $mail->SetSMTPpass($smtpPass);
$recipientName = $data['firstname'] . ' ' . $data['surname']; $recipientName = $data['firstname'] . ' ' . $data['surname'];
$mail->SetSender([$fromEmail => $fromName]); $mail->SetSender([$fromEmail => $fromName]);
@ -859,11 +865,17 @@ function petitionSendThankYouEmailInternal(string $token, string $deleteUrl, str
$subject = petitionT($ctx, 'petition', 'email_thankyou_subject'); $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 // Pre-flight check
$fp = @fsockopen($config['host'], $config['port'], $errno, $errstr, 10); $fp = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10);
if (!$fp) { if (!$fp) {
$errorMessage = "Connection failed: {$errno} - {$errstr}"; $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; return false;
} }
fclose($fp); fclose($fp);
@ -873,10 +885,10 @@ function petitionSendThankYouEmailInternal(string $token, string $deleteUrl, str
$mail = new \codeworxtech\PHPMailerLite\PHPMailerLite(); $mail = new \codeworxtech\PHPMailerLite\PHPMailerLite();
$mail->SetSMTPhost($config['host']); $mail->SetSMTPhost($smtpHost);
$mail->SetSMTPport($config['port']); $mail->SetSMTPport($smtpPort);
$mail->SetSMTPuser($config['username']); $mail->SetSMTPuser($smtpUser);
$mail->SetSMTPpass($config['password']); $mail->SetSMTPpass($smtpPass);
$recipientName = $signature['firstname'] . ' ' . $signature['surname']; $recipientName = $signature['firstname'] . ' ' . $signature['surname'];
$mail->SetSender([$fromEmail => $fromName]); $mail->SetSender([$fromEmail => $fromName]);

View file

@ -17,9 +17,18 @@ return [
'to_email' => 'email@example.com', 'to_email' => 'email@example.com',
'to_name' => 'Stopp Lidelsen', 'to_name' => 'Stopp Lidelsen',
// Petition-specific overrides (optional) // Petition-specific settings (optional)
// If not set, the default values above will be used // 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' => [ '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_email' => 'underskrifter@example.com',
// 'from_name' => 'Underskriftskampanje', // 'from_name' => 'Underskriftskampanje',
], ],