Skip to content

Commit

Permalink
[9.x] Alternative Mailable Syntax (#44462)
Browse files Browse the repository at this point in the history
* working on cleaner mailables

* continue work

* work on headers

* add file

* organize methods

* add doc blocks

* add address support to mail to, cc, bcc

* adding tests

* add more testing

* formatting

* update stubs

* update stubs

* add html as an option

* Update src/Illuminate/Mail/Mailables/Headers.php

Co-authored-by: Tim MacDonald <hello@timacdonald.me>

* Update src/Illuminate/Mail/Mailables/Envelope.php

Co-authored-by: Tim MacDonald <hello@timacdonald.me>

* Update src/Illuminate/Mail/Mailables/Content.php

Co-authored-by: Tim MacDonald <hello@timacdonald.me>

* Apply fixes from StyleCI

* add methods to envelope

* fixes and callbacks

* map attachables

* add methods to other classes

* add using method

Co-authored-by: Tim MacDonald <hello@timacdonald.me>
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
3 people authored Oct 11, 2022
1 parent 1e08c97 commit fd68bd6
Show file tree
Hide file tree
Showing 11 changed files with 1,037 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/Illuminate/Foundation/Console/MailMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ protected function writeMarkdownTemplate()
*/
protected function buildClass($name)
{
$class = parent::buildClass($name);
$class = str_replace(
'{{ subject }}',
Str::headline(str_replace($this->getNamespace($name).'\\', '', $name)),
parent::buildClass($name)
);

if ($this->option('markdown') !== false) {
$class = str_replace(['DummyView', '{{ view }}'], $this->getView(), $class);
Expand Down
34 changes: 30 additions & 4 deletions src/Illuminate/Foundation/Console/stubs/mail.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace {{ namespace }};
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class {{ class }} extends Mailable
Expand All @@ -22,12 +24,36 @@ class {{ class }} extends Mailable
}

/**
* Build the message.
* Get the message envelope.
*
* @return $this
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function build()
public function envelope()
{
return $this->view('view.name');
return new Envelope(
subject: '{{ subject }}',
);
}

/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
return new Content(
view: 'view.name',
);
}

/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
return [];
}
}
34 changes: 30 additions & 4 deletions src/Illuminate/Foundation/Console/stubs/markdown-mail.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace {{ namespace }};
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class {{ class }} extends Mailable
Expand All @@ -22,12 +24,36 @@ class {{ class }} extends Mailable
}

/**
* Build the message.
* Get the message envelope.
*
* @return $this
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function build()
public function envelope()
{
return $this->markdown('{{ view }}');
return new Envelope(
subject: '{{ subject }}',
);
}

/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
return new Content(
markdown: '{{ view }}',
);
}

/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
return [];
}
}
17 changes: 17 additions & 0 deletions src/Illuminate/Mail/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,21 @@ public function attachTo($mail)
fn ($data) => $mail->attachData($data(), $this->as, ['mime' => $this->mime])
);
}

/**
* Determine if the given attachment is equivalent to this attachment.
*
* @param \Illuminate\Mail\Attachment $attachment
* @return bool
*/
public function isEquivalent(Attachment $attachment)
{
return $this->attachWith(
fn ($path) => [$path, ['as' => $this->as, 'mime' => $this->mime]],
fn ($data) => [$data(), ['as' => $this->as, 'mime' => $this->mime]],
) === $attachment->attachWith(
fn ($path) => [$path, ['as' => $attachment->as, 'mime' => $attachment->mime]],
fn ($data) => [$data(), ['as' => $attachment->as, 'mime' => $attachment->mime]],
);
}
}
Loading

0 comments on commit fd68bd6

Please sign in to comment.