├── AppMailer.php └── README.md /AppMailer.php: -------------------------------------------------------------------------------- 1 | mailer = $mailer; 65 | } 66 | 67 | /** 68 | * EXAMPLE 69 | * Deliver the email confirmation. 70 | * 71 | * @param User $user 72 | * @return void 73 | */ 74 | public function sendEmailConfirmationTo() 75 | { 76 | $this->to = $user->email; 77 | $this->subject = 'Subject Name'; 78 | $this->view = 'emails.confirm'; 79 | $this->data = []; 80 | 81 | $this->deliver(); 82 | } 83 | 84 | 85 | /** 86 | * Deliver the email. 87 | * 88 | * @return void 89 | */ 90 | public function deliver() 91 | { 92 | $this->mailer->send($this->view, $this->data, function ($message) { 93 | $message->from($this->from, $this->fromName) 94 | ->subject($this->subject) 95 | ->to($this->to); 96 | }); 97 | } 98 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Laravel Mailer Class 2 | 3 | A simple Laravel mailer class 4 | 5 | You can use this as you wish, just a simple class to help cleanup code and send mail. 6 | 7 | The best way to use is either type-hint from your method: 8 | 9 | ```php 10 | public function store(AppMailer $mailer) 11 | { 12 | // Do some stuff 13 | // pass the user object through, do whatever you want here 14 | 15 | // Send email 16 | $mailer->sendEmailConfirmationTo($user); 17 | return back(); // Whatever you like 18 | } 19 | ```` 20 | 21 | or via your constructor method like so and I store in ````app/Mailers```` 22 | 23 | ```php 24 | use App\Mailers\AppMailer; 25 | 26 | /** 27 | * @var AppMailer 28 | */ 29 | private $mailer; 30 | 31 | /** 32 | * constructor. 33 | * @param AppMailer $mailer 34 | */ 35 | public function __construct(AppMailer $mailer) 36 | { 37 | $this->mailer = $mailer; 38 | } 39 | 40 | ```` 41 | 42 | Then just refer to ````$this->mailer->sendEmailConfirmationTo()```` or whatever methods you have within the mailer class. 43 | --------------------------------------------------------------------------------