Laravel Testing: Test Emails in Laravel - Tutorial by Mailtrap

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to mail trap videos where we explore the world of emails today we cover how to test emails in laravel in our previous videos we explored various sending methods in laravel this time we're diving into email testing we'll cover two testing methods using mail trap email testing and using a log driver let's start with mail trap email testing as it provides a safe environment for inspecting and debugging email s in staging and Dev environments head over to mail trap. and click on the sign up button in the upper right corner choose the method of registration fill in the necessary fields and click on the sign up button go to email testing and use the mailbox created by default or set up a new one this mailbox will receive emails and prevent them from reaching the recipients under the SMTP settings tab click show credentials to reveal the the credentials of the fake SMTP server go to your laravel project and open the N file it contains all the necessary configurations for sending emails we'll configure the settings that have the mail prefix using email testings credentials you'll need to copy the SMTP host Port username and password next to mail from address indicate your sending email address the mail from name in our case will take the value of the app name variable the next step is to create a mailable class we've already created it in our project if you don't have it you can create it using the PHP artisan make mail command followed by the class name such as test laravel mail for example let's return to our class this class takes the name parameter in its Constructor which is then used in the mail test email template the template will look something like this the name variable will be substituted with the recipient's name to send the email we'll use the Tinker console utility in laravel you can configure it using the PHP Artisan Tinker command this allows us to run the code from the command line use the mail facade and the two method to specify the recipient this can be any email address since the email will arrive in mail trap's virtual inbox not in our recipient's mailbox call the send method and pass an instance of the mailable Class by specifying the full name space in our example it's app mail my test email pass the recipient's name as the parameter let's run the code and check the Inbox and there you go the email is right there this time we send a plain text test email but to leverage all of email testing's features it's better to test an HTML template you can you can explore HTML HTML Source text raw spam analysis and Tech infot tabs to analyze various aspects of your emails now let's see how you can test emails using the log driver similar to the previous example we have to generate a mailable class once that's done we need to configure the log driver which writes the email sending information to log files instead of sending emails the information about this driver is located in the male PHP file in the config directory to enable the log driver go to the end file and find the mail mailer if you have SMTP or any other driver here you should change it to log the logs will be written to the preconfigured location such as a file database Etc in our example logs are written to a file to start testing the emails we'll be using Tinker once again use the mail facade and specify the recipient using the two method if you don't indicate an email address here you'll see an error and since we're using the log driver and the emails won't be sent you can use any email address call the send method and configure it similarly to our previous example run the code there are no errors so let's check our logs logs are located in the storage directory under log LS find the log for the current date and open it here you can see all the details of the email we just fake sent including the specified recipient's email address headers and body and the name parameter was also passed correctly we sent a template and you can see exactly that in the locks all done you now know how to test emails in laravel without spamming recipients once all the tests are completed send your emails in production with with an email sending solution one like mail trap email sending for more instructions watch our video on sending emails in laravel all the links will be available in the description below we hope you found this tutorial helpful thank you for watching this video brought to you by mail trap an email delivery platform to test send and control your email infrastructure in one place like and share this video subscribe to our Channel and turn on notifications to see more tutorials like this one don't forget to check out our other videos for more useful content on email deliverability see you in our next [Music] one
Info
Channel: Mailtrap
Views: 1,133
Rating: undefined out of 5
Keywords: Mailtrap, mailtrap tutorial, mailtrap.io, laravel mailtrap, laravel tutorial, laravel project, laravel testing, laravel testing tutorial, laravel email, sending first test email laravel 10, laravel for beginners, laravel test email, laravel mail, laravel test emails, laravel test mail, test email in laravel, test email laravel, laravel mail test, laravel email testing, laravel send test email, test email localhost laravel, send email localhost laravel, laravel 10 tutorial
Id: pJT0o76RgdQ
Channel Id: undefined
Length: 5min 41sec (341 seconds)
Published: Thu Dec 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.