Skip to content

Commit

Permalink
Fix: #1238 - add support for DKIM mail signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Roach committed Sep 16, 2019
1 parent 6f0c730 commit 4d99955
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/AdminSiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ public function mailSave(ServerRequestInterface $request): ResponseInterface
Site::setPreference('SMTP_AUTH_USER', $params['SMTP_AUTH_USER']);
Site::setPreference('SMTP_SSL', $params['SMTP_SSL']);
Site::setPreference('SMTP_HELO', $params['SMTP_HELO']);
Site::setPreference('DKIM_DOMAIN', $params['DKIM_DOMAIN']);
Site::setPreference('DKIM_SELECTOR', $params['DKIM_SELECTOR']);
Site::setPreference('DKIM_KEY', $params['DKIM_KEY']);

if ($params['SMTP_AUTH_PASS'] !== '') {
Site::setPreference('SMTP_AUTH_PASS', $params['SMTP_AUTH_PASS']);
}
Expand Down
21 changes: 19 additions & 2 deletions app/Services/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Swift_Message;
use Swift_NullTransport;
use Swift_SendmailTransport;
use Swift_Signers_DKIMSigner;
use Swift_SmtpTransport;
use Swift_Transport;
use function str_replace;
Expand Down Expand Up @@ -59,10 +60,26 @@ public function send(UserInterface $from, UserInterface $to, UserInterface $repl
->setFrom($from->email(), $from->realName())
->setTo($to->email(), $to->realName())
->setReplyTo($reply_to->email(), $reply_to->realName())
->setBody($message_html, 'text/html')
->addPart($message_text, 'text/plain');
->setBody($message_html, 'text/html');

$dkim_domain = Site::getPreference('DKIM_DOMAIN');
$dkim_selector = Site::getPreference('DKIM_SELECTOR');
$dkim_key = Site::getPreference('DKIM_KEY');

if ($dkim_domain !== '' && $dkim_selector !== '' && $dkim_key !== '') {
$signer = new Swift_Signers_DKIMSigner($dkim_key, $dkim_domain, $dkim_selector);
$signer
->setHeaderCanon('relaxed')
->setBodyCanon('relaxed');

$message->attachSigner($signer);
} else {
// DKIM body hashes don't work with multipart/alternative content.
$message->addPart($message_text, 'text/plain');
}

$mailer = new Swift_Mailer($this->transport());

$mailer->send($message);
} catch (Exception $ex) {
Log::addErrorLog('MailService: ' . $ex->getMessage());
Expand Down
40 changes: 31 additions & 9 deletions resources/views/admin/site-mail.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<form method="post" class="form-horizontal">
<?= csrf_field() ?>

<!-- SMTP_ACTIVE -->
<div class="row form-group">
<label for="SMTP_ACTIVE" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Messages') ?>
Expand All @@ -26,7 +25,6 @@
</div>
</div>

<!-- SMTP_FROM_NAME -->
<div class="row form-group">
<label for="SMTP_FROM_NAME" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Sender name') ?>
Expand All @@ -41,7 +39,6 @@

<h2><?= I18N::translate('SMTP mail server') ?></h2>

<!-- SMTP_HOST -->
<div class="row form-group">
<label for="SMTP_HOST" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Server name') ?>
Expand All @@ -54,7 +51,6 @@
</div>
</div>

<!-- SMTP_PORT -->
<div class="row form-group">
<label for="SMTP_PORT" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Port number') ?>
Expand All @@ -67,7 +63,6 @@
</div>
</div>

<!-- SMTP_AUTH -->
<fieldset class="form-group">
<div class="row">
<legend class="col-form-label col-sm-3">
Expand All @@ -82,7 +77,6 @@
</div>
</fieldset>

<!-- SMTP_AUTH_USER -->
<div class="row form-group">
<label for="SMTP_AUTH_USER" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Username') ?>
Expand All @@ -95,7 +89,6 @@
</div>
</div>

<!-- SMTP_AUTH_PASS -->
<div class="row form-group">
<label for="SMTP_AUTH_PASS" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Password') ?>
Expand All @@ -108,7 +101,6 @@
</div>
</div>

<!-- SMTP_SSL -->
<div class="row form-group">
<label for="SMTP_SSL" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Secure connection') ?>
Expand All @@ -121,7 +113,6 @@
</div>
</div>

<!-- SMTP_HELO -->
<div class="row form-group">
<label for="SMTP_HELO" class="col-sm-3 col-form-label">
<?= /* I18N: A configuration setting */ I18N::translate('Sending server name') ?>
Expand All @@ -134,6 +125,37 @@
</div>
</div>

<h2>
<?= /* I18N: https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail */ I18N::translate('DKIM digital signature') ?>
</h2>

<div class="row form-group">
<label for="DKIM_DOMAIN" class="col-sm-3 col-form-label">
<?= I18N::translate('Domain name') ?>
</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="DKIM_DOMAIN" name="DKIM_DOMAIN" value="<?= e(Site::getPreference('DKIM_DOMAIN')) ?>" maxlength="255">
</div>
</div>

<div class="row form-group">
<label for="DKIM_SELECTOR" class="col-sm-3 col-form-label">
<?= I18N::translate('Selector') ?>
</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="DKIM_SELECTOR" name="DKIM_SELECTOR" value="<?= e(Site::getPreference('DKIM_SELECTOR')) ?>" maxlength="255">
</div>
</div>

<div class="row form-group">
<label for="DKIM_KEY" class="col-sm-3 col-form-label">
<?= I18N::translate('Private key') ?>
</label>
<div class="col-sm-9">
<textarea class="form-control" id="DKIM_KEY" name="DKIM_KEY"><?= e(Site::getPreference('DKIM_KEY')) ?></textarea>
</div>
</div>

<div class="row form-group">
<div class="offset-sm-3 col-sm-9">
<button type="submit" class="btn btn-primary">
Expand Down

0 comments on commit 4d99955

Please sign in to comment.