NathanGeerinck /
laravel-newsletter
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Mail; |
||
| 4 | |||
| 5 | use App\Models\Campaign; |
||
| 6 | use App\Models\Template; |
||
| 7 | use App\Models\Subscription; |
||
| 8 | use Illuminate\Bus\Queueable; |
||
| 9 | use Illuminate\Mail\Mailable; |
||
| 10 | use Illuminate\Queue\SerializesModels; |
||
| 11 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @property Subscription subscription |
||
| 15 | * @property Campaign campaign |
||
| 16 | * @property Template template |
||
| 17 | */ |
||
| 18 | class CampaignMail extends Mailable implements ShouldQueue |
||
| 19 | { |
||
| 20 | use Queueable, SerializesModels; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 21 | |||
| 22 | public $subscription; |
||
| 23 | public $campaign; |
||
| 24 | public $template; |
||
| 25 | |||
| 26 | public function __construct(Subscription $subscription, Campaign $campaign, Template $template) |
||
| 27 | { |
||
| 28 | $this->subscription = $subscription; |
||
| 29 | $this->campaign = $campaign; |
||
| 30 | $this->template = $template; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Build the message. |
||
| 35 | * |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function build() |
||
| 39 | { |
||
| 40 | return $this->subject($this->campaign->subject) |
||
| 41 | ->view('emails.campaign') |
||
| 42 | ->with([ |
||
| 43 | 'content' => $this->str_replace_dynamic([ |
||
| 44 | '%subject%' => $this->campaign->subject, |
||
| 45 | '%email%' => $this->subscription->email, |
||
| 46 | '%name%' => $this->subscription->name, |
||
| 47 | '%country%' => countries($this->subscription->country), |
||
| 48 | '%unsubscribe_link%' => route('subscriptions.preunsubscribe', [$this->subscription->email, $this->subscription->unsubscribe]) |
||
| 49 | ], $this->template->content) |
||
| 50 | ])->withSwiftMessage(function ($message) { |
||
| 51 | $headers = $message->getHeaders(); |
||
| 52 | $headers->addTextHeader('X-Campaign-ID', $this->campaign->id); |
||
| 53 | }); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function str_replace_dynamic(array $replace, $string) |
||
| 57 | { |
||
| 58 | return str_replace(array_keys($replace), array_values($replace), $string); |
||
| 59 | } |
||
| 60 | } |
||
| 61 |