Skip to content

Commit 75073f3

Browse files
committed
wip
1 parent fd6b7b6 commit 75073f3

10 files changed

+92
-173
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
<p align="center">
2-
<img src="/logo.png">
3-
</p>
41
<p align="center">
52
<img src="https://github.com/stackkit/laravel-database-emails/workflows/Run%20tests/badge.svg?branch=master" alt="Build Status">
63
<a href="https://packagist.org/packages/stackkit/laravel-database-emails"><img src="https://poser.pugx.org/stackkit/laravel-database-emails/v/stable.svg" alt="Latest Stable Version"></a>
74
<a href="https://packagist.org/packages/stackkit/laravel-database-emails"><img src="https://poser.pugx.org/stackkit/laravel-database-emails/license.svg" alt="License"></a>
85
</p>
96

10-
# Package Documentation
7+
# Introduction
118

129
This package allows you to store and send e-mails using a database.
1310

14-
## Contribution
15-
16-
The package is MIT licenced, meaning it's open source and you are free to copy or fork it and modify it any way you wish.
17-
18-
We feel the package is currently feature complete, but feel free to send a pull request or help improve existing code.
19-
2011
# Requirements
2112

2213
This package requires Laravel 10 or 11.
@@ -188,6 +179,17 @@ Email::compose()
188179
Note: `fromData()` and `fromStorage()` are not supported as the work with raw data.
189180
</small>
190181

182+
### Attaching models to e-mails
183+
184+
You may attach models to your e-mails.
185+
186+
```php
187+
188+
Email::compose()
189+
->model(User::find(1));
190+
191+
```
192+
191193
### Scheduling
192194

193195
You may schedule an e-mail by calling `later` instead of `send`. You must provide a Carbon instance or a strtotime valid date.

database/migrations/2024_03_16_151608_change_binary_to_text.php renamed to database/migrations/2024_03_16_151608_create_new_table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class ChangeBinaryToText extends Migration
7+
class CreateNewTable extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -31,6 +31,7 @@ public function up()
3131
$table->text('error')->nullable();
3232
$table->json('attachments')->nullable();
3333
$table->json('from')->nullable();
34+
$table->nullableMorphs('model');
3435
$table->json('reply_to')->nullable();
3536
$table->timestamp('queued_at')->nullable();
3637
$table->timestamp('scheduled_at')->nullable();

logo.png

-15.8 KB
Binary file not shown.

src/Email.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Builder;
1010
use Illuminate\Database\Eloquent\MassPrunable;
1111
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Database\Eloquent\Relations\MorphTo;
1213
use Throwable;
1314

1415
/**
@@ -79,11 +80,9 @@ public function markAsSending(): void
7980

8081
public function markAsSent(): void
8182
{
82-
$now = Carbon::now()->toDateTimeString();
83-
8483
$this->update([
8584
'sending' => 0,
86-
'sent_at' => $now,
85+
'sent_at' => now(),
8786
'failed' => 0,
8887
'error' => '',
8988
]);
@@ -116,4 +115,9 @@ public function prunable(): Builder
116115

117116
return $this->where('created_at', '<', now()->subMonths(6));
118117
}
118+
119+
public function model(): MorphTo
120+
{
121+
return $this->morphTo();
122+
}
119123
}

src/EmailComposer.php

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use Illuminate\Contracts\Translation\HasLocalePreference;
9+
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Foundation\Auth\User;
1011
use Illuminate\Mail\Mailable;
1112
use Illuminate\Mail\Mailables\Content;
@@ -102,6 +103,13 @@ public function user(User $user)
102103
});
103104
}
104105

106+
public function model(Model $model)
107+
{
108+
$this->setData('model', $model);
109+
110+
return $this;
111+
}
112+
105113
/**
106114
* Get the e-mail that is being composed.
107115
*/
@@ -110,24 +118,13 @@ public function getEmail(): Email
110118
return $this->email;
111119
}
112120

113-
/**
114-
* Set a data value.
115-
*
116-
* @param mixed $value
117-
*/
118121
public function setData(string $key, $value): self
119122
{
120123
$this->data[$key] = $value;
121124

122125
return $this;
123126
}
124127

125-
/**
126-
* Get a data value.
127-
*
128-
* @param mixed $default
129-
* @return mixed
130-
*/
131128
public function getData(string $key, $default = null)
132129
{
133130
if (! is_null($default) && ! $this->hasData($key)) {
@@ -137,41 +134,25 @@ public function getData(string $key, $default = null)
137134
return $this->data[$key];
138135
}
139136

140-
/**
141-
* Determine if the given data value was set.
142-
*/
143137
public function hasData(string $key): bool
144138
{
145139
return isset($this->data[$key]);
146140
}
147141

148-
/**
149-
* Set the e-mail label.
150-
*/
151142
public function label(string $label): self
152143
{
153144
$this->email->label = $label;
154145

155146
return $this;
156147
}
157148

158-
/**
159-
* Schedule the e-mail.
160-
*
161-
* @param mixed $scheduledAt
162-
*/
163149
public function later($scheduledAt): Email
164150
{
165151
$this->email->scheduled_at = Carbon::parse($scheduledAt);
166152

167153
return $this->send();
168154
}
169155

170-
/**
171-
* Queue the e-mail.
172-
*
173-
* @param \DateTimeInterface|\DateInterval|int|null $delay
174-
*/
175156
public function queue(?string $connection = null, ?string $queue = null, $delay = null): Email
176157
{
177158
$connection = $connection ?: config('queue.default');
@@ -187,9 +168,6 @@ public function queue(?string $connection = null, ?string $queue = null, $delay
187168
return $this->send();
188169
}
189170

190-
/**
191-
* Set the Mailable.
192-
*/
193171
public function mailable(Mailable $mailable): self
194172
{
195173
$this->setData('mailable', $mailable);
@@ -199,33 +177,6 @@ public function mailable(Mailable $mailable): self
199177
return $this;
200178
}
201179

202-
/**
203-
* Attach a file to the e-mail.
204-
*/
205-
// public function attach(string $file, array $options = []): self
206-
// {
207-
// $attachments = $this->hasData('attachments') ? $this->getData('attachments') : [];
208-
//
209-
// $attachments[] = compact('file', 'options');
210-
//
211-
// return $this->setData('attachments', $attachments);
212-
// }
213-
//
214-
// /**
215-
// * Attach in-memory data as an attachment.
216-
// */
217-
// public function attachData(string $data, string $name, array $options = []): self
218-
// {
219-
// $attachments = $this->hasData('rawAttachments') ? $this->getData('rawAttachments') : [];
220-
//
221-
// $attachments[] = compact('data', 'name', 'options');
222-
//
223-
// return $this->setData('rawAttachments', $attachments);
224-
// }
225-
226-
/**
227-
* Send the e-mail.
228-
*/
229180
public function send(): Email
230181
{
231182
if ($this->envelope && $this->content) {

src/HasEncryptedAttributes.php

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

src/MailableReader.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public function attachments(): array
6565
$this->readBody($composer);
6666

6767
$this->readAttachments($composer);
68+
69+
$this->readModel($composer);
6870
}
6971

7072
/**
@@ -176,6 +178,13 @@ private function readAttachments(EmailComposer $composer): void
176178
}, $mailable->attachments);
177179
}
178180

181+
public function readModel(EmailComposer $composer): void
182+
{
183+
if ($composer->hasData('model')) {
184+
$composer->getEmail()->model()->associate($composer->getData('model'));
185+
}
186+
}
187+
179188
private function prepareAddressForDatabaseStorage(array $addresses): array
180189
{
181190
return collect($addresses)->mapWithKeys(function ($recipient) {

src/Sender.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
class Sender
1212
{
13-
/**
14-
* Send the given e-mail.
15-
*/
1613
public function send(Email $email): void
1714
{
1815
if ($email->isSent()) {
@@ -25,17 +22,11 @@ public function send(Email $email): void
2522
$this->buildMessage($message, $email);
2623
});
2724

28-
// $sentMessage is null when mocking (Mail::shouldReceive('send')->once())
29-
if (! is_null($sentMessage)) {
30-
event(new MessageSent($sentMessage));
31-
}
25+
event(new MessageSent($sentMessage));
3226

3327
$email->markAsSent();
3428
}
3529

36-
/**
37-
* Build the e-mail message.
38-
*/
3930
private function buildMessage(Message $message, Email $email): void
4031
{
4132
$message->to($email->recipient)

test

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)