Skip to content

Commit 6240c0e

Browse files
mrjosesimonschaufi
authored andcommitted
Laravel 9 compatibility
1 parent d6fa0e2 commit 6240c0e

8 files changed

+64
-79
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
[![GitHub Sponsors](https://img.shields.io/github/sponsors/simonschaufi?label=GitHub%20Sponsors)](https://github.com/sponsors/simonschaufi)
55
![GitHub](https://img.shields.io/github/license/simonschaufi/laravel-dkim)
66
[![Packagist Downloads](https://img.shields.io/packagist/dt/simonschaufi/laravel-dkim)](https://packagist.org/packages/simonschaufi/laravel-dkim)
7-
[![Laravel 7](https://img.shields.io/badge/Laravel-7-ff2d20)](https://laravel.com)
8-
[![Laravel 8](https://img.shields.io/badge/Laravel-8-ff2d20)](https://laravel.com)
7+
[![Laravel 9](https://img.shields.io/badge/Laravel-9-ff2d20)](https://laravel.com)
98

109
A Laravel package, that allows signing emails with DKIM.
1110

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
}
1111
],
1212
"require": {
13-
"illuminate/mail": "^7.0 || ^8.0"
13+
"illuminate/mail": "^9.0"
1414
},
1515
"autoload": {
1616
"psr-4": {
1717
"SimonSchaufi\\LaravelDKIM\\": "src/"
1818
}
1919
}
20-
}
20+
}

config/dkim.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
return [

src/DKIMMailServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace SimonSchaufi\LaravelDKIM;

src/Exception/MissingConfigurationException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace SimonSchaufi\LaravelDKIM\Exception;

src/MailManager.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace SimonSchaufi\LaravelDKIM;
@@ -10,12 +11,10 @@ class MailManager extends \Illuminate\Mail\MailManager
1011
/**
1112
* Resolve the given mailer.
1213
*
13-
* @param string $name
14-
* @return \Illuminate\Mail\Mailer
15-
*
16-
* @throws InvalidArgumentException
14+
* @param string $name
15+
* @return Mailer
1716
*/
18-
protected function resolve($name)
17+
protected function resolve($name): Mailer
1918
{
2019
$config = $this->getConfig($name);
2120

@@ -29,7 +28,7 @@ protected function resolve($name)
2928
$mailer = new Mailer(
3029
$name,
3130
$this->app['view'],
32-
$this->createSwiftMailer($config),
31+
$this->createSymfonyTransport($config),
3332
$this->app['events']
3433
);
3534

src/Mailer.php

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,93 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace SimonSchaufi\LaravelDKIM;
56

7+
use Illuminate\Contracts\Mail\Mailable as MailableContract;
8+
use Illuminate\Mail\SentMessage;
69
use InvalidArgumentException;
7-
use SimonSchaufi\LaravelDKIM\Exception\MissingConfigurationException;
8-
use Swift_SwiftException;
10+
use Symfony\Component\Mime\Crypto\DkimSigner;
911

1012
class Mailer extends \Illuminate\Mail\Mailer
1113
{
1214
/**
13-
* Create a new message instance.
15+
* Send a new message using a view.
1416
*
15-
* @return Message
16-
* @throws MissingConfigurationException
17-
* @throws Swift_SwiftException
17+
* @param MailableContract|string|array $view
18+
* @param array $data
19+
* @param \Closure|string|null $callback
20+
* @return SentMessage|null
1821
*/
19-
protected function createMessage()
22+
public function send($view, array $data = [], $callback = null): ?SentMessage
2023
{
21-
$message = new Message($this->swift->createMessage('message'));
22-
23-
// If a global from address has been specified we will set it on every message
24-
// instances so the developer does not have to repeat themselves every time
25-
// they create a new message. We will just go ahead and push the address.
26-
if (! empty($this->from['address'])) {
27-
$message->from($this->from['address'], $this->from['name']);
24+
if ($view instanceof MailableContract) {
25+
return $this->sendMailable($view);
2826
}
2927

30-
// When a global reply address was specified we will set this on every message
31-
// instance so the developer does not have to repeat themselves every time
32-
// they create a new message. We will just go ahead and push this address.
33-
if (! empty($this->replyTo['address'])) {
34-
$message->replyTo($this->replyTo['address'], $this->replyTo['name']);
28+
// First we need to parse the view, which could either be a string or an array
29+
// containing both an HTML and plain text versions of the view which should
30+
// be used when sending an e-mail. We will extract both of them out here.
31+
[$view, $plain, $raw] = $this->parseView($view);
32+
33+
$data['message'] = $message = $this->createMessage();
34+
35+
// Once we have retrieved the view content for the e-mail we will set the body
36+
// of this message using the HTML type, which will provide a simple wrapper
37+
// to creating view based emails that are able to receive arrays of data.
38+
if (! is_null($callback)) {
39+
$callback($message);
3540
}
3641

37-
if (! empty($this->returnPath['address'])) {
38-
$message->returnPath($this->returnPath['address']);
42+
$this->addContent($message, $view, $plain, $raw, $data);
43+
44+
// If a global "to" address has been set, we will set that address on the mail
45+
// message. This is primarily useful during local development in which each
46+
// message should be delivered into a single mail address for inspection.
47+
if (isset($this->to['address'])) {
48+
$this->setGlobalToAndRemoveCcAndBcc($message);
3949
}
4050

51+
// Next we will determine if the message should be sent. We give the developer
52+
// one final chance to stop this message and then we will send it to all of
53+
// its recipients. We will then fire the sent event for the sent message.
54+
$symfonyMessage = $message->getSymfonyMessage();
55+
4156
// PATCH START
4257
$privateKey = config('dkim.private_key');
4358
$selector = config('dkim.selector');
4459
$domain = config('dkim.domain');
4560
if (in_array(strtolower(config('mail.default')), ['smtp', 'sendmail', 'log'])) {
4661
if (empty($privateKey)) {
47-
throw new MissingConfigurationException('No private key set.', 1588115551);
62+
throw new InvalidArgumentException('No private key set.', 1588115551);
4863
}
4964
if (!file_exists($privateKey)) {
5065
throw new InvalidArgumentException('Private key file does not exist.', 1588115609);
5166
}
5267

5368
if (empty($selector)) {
54-
throw new MissingConfigurationException('No selector set.', 1588115373);
69+
throw new InvalidArgumentException('No selector set.', 1588115373);
5570
}
5671
if (empty($domain)) {
57-
throw new MissingConfigurationException('No domain set.', 1588115434);
72+
throw new InvalidArgumentException('No domain set.', 1588115434);
5873
}
5974

60-
$message->attachDKIMSigner(
61-
file_get_contents($privateKey),
62-
$domain,
63-
$selector,
64-
config('dkim.passphrase')
65-
);
75+
$signer = new DkimSigner(file_get_contents($privateKey), $domain, $selector, [], config('dkim.passphrase'));
76+
$signedEmail = $signer->sign($message->getSymfonyMessage());
77+
$symfonyMessage->setHeaders($signedEmail->getHeaders());
6678
}
6779
// PATCH END
6880

69-
return $message;
81+
if ($this->shouldSendMessage($symfonyMessage, $data)) {
82+
$symfonySentMessage = $this->sendSymfonyMessage($symfonyMessage);
83+
84+
if ($symfonySentMessage) {
85+
$sentMessage = new SentMessage($symfonySentMessage);
86+
87+
$this->dispatchSentEvent($sentMessage, $data);
88+
89+
return $sentMessage;
90+
}
91+
}
7092
}
7193
}

src/Message.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)