Skip to content

Add initial draft of ReplyTo feature #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
vendor/
.DS_Store
composer.lock
.phpunit.result.cache
.phpunit.result.cache
.phpunit.cache
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ Email::compose()
->bcc(['john@doe.com', 'jane@doe.com']);
```

### Reply-To

```php
<?php

use Stackkit\LaravelDatabaseEmails\Email;

Email::compose()
->replyTo(['john@doe.com', 'jane@doe.com']);

Email::compose()
->replyTo(new Address('john@doe.com', 'John Doe'));

Email::compose()
->replyTo([
new Address('john@doe.com', 'John Doe'),
new Address('jane@doe.com', 'Jane Doe'),
]);
```

### Using mailables

You may also pass a mailable to the e-mail composer.
Expand Down Expand Up @@ -232,3 +252,28 @@ To enable, set the following environment variable:
```
LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY=true
```

### Pruning models

```php
use Stackkit\LaravelDatabaseEmails\Email;

$schedule->command('model:prune', [
'--model' => [Email::class],
])->everyMinute();
```

By default, e-mails are pruned when they are older than 6 months.

You may change that by adding the following to the AppServiceProvider.php:

```php
use Stackkit\LaravelDatabaseEmails\Email;

public function register(): void
{
Email::pruneWhen(function (Email $email) {
return $email->where(...);
});
}
```
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"l6": [
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
"composer update --prefer-stable --prefer-dist --no-interaction"
],
"test": [
"CI_DB_DRIVER=sqlite CI_DB_DATABASE=:memory: phpunit"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddReplyToToEmailsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('emails', function (Blueprint $table) {
$table->binary('reply_to')->nullable()->after('bcc');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
58 changes: 58 additions & 0 deletions src/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Stackkit\LaravelDatabaseEmails;

use Closure;
use Exception;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;

/**
* @property $id
Expand All @@ -15,6 +18,7 @@
* @property $from
* @property $cc
* @property $bcc
* @property $reply_to
* @property $subject
* @property $view
* @property $variables
Expand All @@ -33,6 +37,7 @@
class Email extends Model
{
use HasEncryptedAttributes;
use Prunable;

/**
* The table in which the e-mails are stored.
Expand All @@ -48,6 +53,8 @@ class Email extends Model
*/
protected $guarded = [];

public static ?Closure $pruneQuery = null;

/**
* Compose a new e-mail.
*
Expand Down Expand Up @@ -190,6 +197,26 @@ public function getBccAttribute()
return $this->bcc;
}

/**
* Get the e-mail reply-to addresses.
*
* @return array|string
*/
public function getReplyTo()
{
return $this->reply_to;
}

/**
* Get the e-mail reply-to addresses.
*
* @return array|string
*/
public function getReplyToAttribute()
{
return $this->reply_to;
}

/**
* Get the e-mail subject.
*
Expand Down Expand Up @@ -388,6 +415,16 @@ public function hasBcc(): bool
return strlen($this->getRawDatabaseValue('bcc')) > 0;
}

/**
* Determine if the e-mail should sent with reply-to.
*
* @return bool
*/
public function hasReplyTo(): bool
{
return strlen($this->getRawDatabaseValue('reply_to') ?: '') > 0;
}

/**
* Determine if the e-mail is scheduled to be sent later.
*
Expand Down Expand Up @@ -520,4 +557,25 @@ public function getRawDatabaseValue(string $key = null, $default = null)

return $this->getOriginal($key, $default);
}

/**
* @param Closure $closure
* @return void
*/
public static function pruneWhen(Closure $closure)
{
static::$pruneQuery = $closure;
}

/**
* @return Builder
*/
public function prunable()
{
if (static::$pruneQuery) {
return (static::$pruneQuery)($this);
}

return $this->where('created_at', '<', now()->subMonths(6));
}
}
11 changes: 11 additions & 0 deletions src/EmailComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ public function bcc($bcc): self
return $this->setData('bcc', $bcc);
}

/**
* Define the reply-to address(es).
*
* @param string|array $replyTo
* @return self
*/
public function replyTo($replyTo): self
{
return $this->setData('reply_to', $replyTo);
}

/**
* Set the e-mail subject.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function encrypt(EmailComposer $composer): void

$this->encryptRecipients($composer);

$this->encryptReplyTo($composer);

$this->encryptFrom($composer);

$this->encryptSubject($composer);
Expand All @@ -36,6 +38,20 @@ private function setEncrypted(EmailComposer $composer): void
$composer->getEmail()->setAttribute('encrypted', 1);
}

/**
* Encrypt the e-mail reply-to.
*
* @param EmailComposer $composer
*/
private function encryptReplyTo(EmailComposer $composer): void
{
$email = $composer->getEmail();

$email->fill([
'reply_to' => $composer->hasData('reply_to') ? encrypt($email->reply_to) : '',
]);
}

/**
* Encrypt the e-mail addresses of the recipients.
*
Expand Down
2 changes: 2 additions & 0 deletions src/HasEncryptedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait HasEncryptedAttributes
'from',
'cc',
'bcc',
'reply_to',
'subject',
'variables',
'body',
Expand All @@ -33,6 +34,7 @@ trait HasEncryptedAttributes
'from',
'cc',
'bcc',
'reply_to',
'variables',
];

Expand Down
18 changes: 17 additions & 1 deletion src/MailableReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function read(EmailComposer $composer): void

$this->readBcc($composer);

$this->readReplyTo($composer);

$this->readSubject($composer);

$this->readBody($composer);
Expand Down Expand Up @@ -115,6 +117,20 @@ private function readBcc(EmailComposer $composer): void
$composer->bcc($bcc);
}

/**
* Read the mailable reply-to to the email composer.
*
* @param EmailComposer $composer
*/
private function readReplyTo(EmailComposer $composer): void
{
$replyTo = $this->convertMailableAddresses(
$composer->getData('mailable')->replyTo
);

$composer->replyTo($replyTo);
}

/**
* Read the mailable subject to the email composer.
*
Expand All @@ -137,7 +153,7 @@ private function readBody(EmailComposer $composer): void

$mailable = $composer->getData('mailable');

$composer->setData('body', view($mailable->view, $mailable->buildViewData()));
$composer->setData('body', view($mailable->view, $mailable->buildViewData())->render());
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Stackkit\LaravelDatabaseEmails;

use Carbon\Carbon;
use Illuminate\Mail\Mailables\Address;

class Preparer
{
Expand All @@ -25,6 +26,8 @@ public function prepare(EmailComposer $composer): void

$this->prepareBcc($composer);

$this->prepareReplyTo($composer);

$this->prepareSubject($composer);

$this->prepareView($composer);
Expand Down Expand Up @@ -118,6 +121,33 @@ private function prepareBcc(EmailComposer $composer): void
]);
}

/**
* Prepare the reply-to for database storage.
*
* @param EmailComposer $composer
*/
private function prepareReplyTo(EmailComposer $composer): void
{
$value = $composer->getData('reply_to', []);

if (! is_array($value)) {
$value = [$value];
}

foreach ($value as $i => $v) {
if ($v instanceof Address) {
$value[$i] = [
'address' => $v->address,
'name' => $v->name,
];
}
}

$composer->getEmail()->fill([
'reply_to' => json_encode($value),
]);
}

/**
* Prepare the subject for database storage.
*
Expand Down
1 change: 1 addition & 0 deletions src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private function buildMessage(Message $message, Email $email): void
$message->to($email->getRecipient())
->cc($email->hasCc() ? $email->getCc() : [])
->bcc($email->hasBcc() ? $email->getBcc() : [])
->replyTo($email->hasReplyTo() ? $email->getReplyTo() : [])
->subject($email->getSubject())
->from($email->getFromAddress(), $email->getFromName());

Expand Down
Loading