Sending mail with attachment using laravel and mailtrap

Views:
 
Category: Education
     
 

Presentation Description

Laravel provides a very simple api to send mail to users. Today in this topic I am going to show you how to send mail with attachment using mailtrap.io. MailTrap is a tool through which we can test our outgoing mail,their look and feel on various devices. All we will be learning today.

Comments

Presentation Transcript

slide 1:

S E N D I N G M A I L W I T H A T T A C H M E N T U S I N G L A R A V E L A N D M A I L T R A P . I O

slide 2:

Sending mail with attachment using laravel and mailtrap.io Source: ​https://decodeweb.in/php/php-frameworks/laravel-framework/sending-mail-with-attachment-using-laravel-and-mailtrap-io/ Laravel provides a very simple api to send mail to users. Today in this topic I am going to show you how to send mail with attachment using mailtrap.io. MailTrap is a tool through which we can test our outgoing mailtheir look and feel on various devices. All we will be learning today. Make laravel mailer class In laravel we can create a mailer class through artisan command. php artisan make:mail SendMailWithAttachment It will generate a filename ​SendMailWithAttachment.php ​ in App\Mail directory. Just copy paste below contents in the generated file. php namespace ​ ​App ​\ ​Mail

slide 3:

use ​ ​Illuminate ​\ ​Bus ​\ ​Queueable ​ use ​ ​Illuminate ​\ ​Mail ​\ ​Mailable ​ use ​ ​Illuminate ​\ ​Queue ​\ ​SerializesModels ​ use ​ ​Illuminate ​\ ​Contracts ​\ ​Queue ​\ ​ShouldQueue ​ class ​ ​SendMailWithAttachment ​ ​extends ​ ​Mailable ​ ​implements ​ ​ShouldQueue use ​ ​Queueable ​ ​SerializesModels ​ / Create a new message instance. return void / public ​ ​function ​ ​__construct ​ // / Build the message. return this / public ​ ​function ​ ​build ​ ​return ​ ​this ​-to ​testexample.com ​ -attachstorage_path ​app/1569316022.jpg ​ -markdown ​mail_example ​ In the above we are implementing ShouldQueue interface because I usually make use of laravel queue to send mails because user should not wait until mail is sent that’s why I use Queue. In the build method we are mentioning the recipient email id in to method in attach method we are giving the full path of the file we are sending as an attachment since my file is in laravel’s storage directory only thats why I am calling the file with ​storage_path ​ helper function. In the end I am using the markdown as it allows us to use mail template with custom values. resources/views/mail_example.blade.php component ​mail::message ​ Sending Mail With Attachment

slide 4:

Dear reader This is just a simple example of sending mail with attachment. PFA. brbr Thanksbr config ​app.name ​ endcomponent Configuring MailTrap.io Go to ​https://mailtrap.io ​ and register there. After successful registration select laravel and you will see such screen. Copy the highlighted section into your .env file there is already mailtrap set with empty values.

slide 5:

Testing the mail In web.php define a route for triggering the mailer class. Route::get ​mail-with-attachment ​ ​function ​ ​ ​ \Illuminate\Support\Facades\Mail::queue ​new ​ \App\Mail\SendMailWithAttachment Now open the terminals and the follow commands in each terminal. First terminal: ​php artisan serve Second terminal: ​redis-server ​ ​download the redis server from here ​ Third terminal: ​php artisan queue:work Lastly go to ​http://localhost:8000/mail-with-attachment ​ and check mailtrap.io Hoorray We received a mail in mailtrap.io with an attachment. So friends that’s all about sending mail with attachment using laravel and mailtrap.io.

authorStream Live Help