Skip to content

Instantly share code, notes, and snippets.

@yebt
Last active June 20, 2023 21:46
Show Gist options
  • Save yebt/06f340af4431d9dd2223f24a91923bf6 to your computer and use it in GitHub Desktop.
Save yebt/06f340af4431d9dd2223f24a91923bf6 to your computer and use it in GitHub Desktop.
Laravel 7 swift dkim usage; on new versions of laravel 9^, swift Mailer is remplaced with Symfony Mailer
<?php
# this file is in `config/manualdkim.php`
return [
'private_key' => env('DKIM_PRIVATE_KEY', storage_path('app/dkim/private_key.txt')),
'selector' => env('DKIM_SELECTOR', 'default'),
'domain' => env('DKIM_DOMAIN', null),
];
<?php
#...
# app/Mail/ResetPassword.php
#...
class GeneralMail extends Mailable{
#...
public function build()
{
$this->subject(__('General Mail'))
->view('mails.resetpassword')
->text('mails.resetpassword_plain');
add_dkim_to_mailable($this); // this call the helper
return $this;
}
}
<?php
#-...
# app/Helpers/Helper.php
if (!function_exists('add_dkim_to_mailable')) {
/**
* This function add a Dim sign to mailable entities.
* @param Mailable $mailable the mailable entity
* @return void
*/
function add_dkim_to_mailable(Illuminate\Mail\Mailable $mailable)
{
$mailable->withSwiftMessage(function (\Swift_Message $message) {
// DKIM INFO
$dkim_privatekey = config('manualdkim.private_key');
$dkim_selector = config('manualdkim.selector');
$dkim_domain = config('manualdkim.domain');
if (
empty($dkim_privatekey) &&
(!file_exists($dkim_privatekey) &&
(empty($dkim_selector) && empty($dkim_domain)))
) {
return;
}
$signer = new \Swift_Signers_DKIMSigner(file_get_contents($dkim_privatekey), $dkim_domain, $dkim_selector);
$message->attachSigner($signer);
});
}
}
# storage/app/dkim/private_key.txt
#here write the private key generated from the dkim support on your host
#or from this great tool: https://tools.socketlabs.com/dkim/generator
# in the host manage go to cpanel zone editor and search dkim._domainkey.you_domain.com or default._domainkey.you_domain.com
# CPANEL
# Email Deliverability > [press "Manage" button of select domain] > see the dkim menu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment