Laravel Tutorial: How Sending Emails in Laravel Works - Mailtrap

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to mail trap videos where we explore the world of emails here's how to send emails with laravel first let's go over basic configurations for sending emails using the laravel framework all the configurations related to sending emails are located in the mail.php file in the config directory in this file the default mailer is specified which will be used to send emails the value is taken from the dot EnV file with the mail mailer variable if this value is missing the default value is used which is specified in the second parameter let's look at the value in our case the value is the same and this variable has the SMTP value so in our case the SMTP driver will be used below you can see the configuration for each driver for example here you can specify your custom driver as the default driver by default there is no mail trap driver so like in this example you can list it along with all of the needed configurations laravel has built-in support for the listed default drivers from version 2 onwards all ouravel classes are stored in the mail directory which is usually located in the app directory when you first install the project this directory does not exist so you need to create it using the command line and Artisan to create a new mail class let's create this and name it my email the class was successfully created and here you can see the address where the file is stored so the mail directory has been created and you can see our newly created mail class inside this class contains several methods envelope content attachment Etc and can be used to configure various settings for this specific email let's take a look at how to add a sender to an email so while using the envelope method you can easily specify the sender by adding a from parameter inside the object's Constructor in this case this specific email will be sent on behalf of John from mailtrap gmail.com if you do not want to specify a sender for each email and want all your emails to be sent from a specific address you can do this using the configuration file in this file there is a global from address section this address will be used in case the address is not specified directly in the mail class this global address by default has the value hello at example.com but if the dot EnV file has a value specified it will be used as the global address you can also specify the sender's name here or use the name taken from the dot EnV variable that's all for the basic configurations in the narfl for sending emails now let's take a look at how to send emails using the framework and an SMTP server which in our case will be that of a mail trap first you'll need to register an account and verify the domain from which you will send emails afterward on the sending domains page go to the API and SMTP Tab and select SMTP settings there you'll find the settings you need to use for sending emails the next step is to copy these settings into our laravel project first you'll need to copy the host value into the dot EnV file here you will see the mail host variable paste your host into this variable the mailer should be set to SMTP next copy the port value the username and the password for encryption specify TLS you also need to specify the email address from which you will send emails in our case it's no reply at mailtrap.club the email domain should match the registered domain to avoid any errors alright the settings are specified keep in mind that the SMTP server can be from other services as well like Gmail for example but again in our example we'll use mailtrap after specifying all the necessary settings you need to create a mailable class this can be done in laravel using this Artisan command in our case this class is already created and you can find it in the mail folder from the app directory in it you will find the email header the content which will be a template in our example and the parameters passed to the template you can also add attachments to the email if necessary the next step is to create the email template with the specified name in The View variable to do this navigate to the resources folder then to the Views folder within it and finally to the mail folder in our example the template is already created in the corresponding directory make sure the first part of the file name matches the view variable second part inside the template you will see the text and the dynamic content that will be passed using the width parameter also remember to specify these parameters in the Constructor of your mailable class after this you can create a test route to check if the email sending is working properly to do this find the web file in the root directory and add a new route specify the dynamic name that you want to feature in the email and the address to which you're sending the email now you can access the test route and see if the email sending is working let's open the browser and our mailbox go to your laravel project and access the test route and we can see that the email was sent and delivered to the recipient's mailbox with the specified text so this is how you can send emails in laravel using an SMTP server however depending on the project using a transactional email API can be a better alternative so let's take a look at how to do that in our example we'll be using the mailtrap PHP client the relevant documentation can be found on GitHub and there is also a link to it in the description of this video so first you'll need to go to the relevant Link in the framework integration section of the documentation and from there copy the installation command for mailtrap PHP library and HTTP client now navigate to your laravel project where a fresh laravel version should already be installed in your terminal paste and run the installation commands once the libraries are installed you can specify a new mail transport in the configuration file do this by copying the lines from the usage section of the documentation and pasting them into the mailers section in the configmail.php file Now find the dot EnV file where environment variables are stored and add the following values mail mailer mail host mail API key so copy these values and paste them into the dot EnV file in the mail section remove the existing mail variables to avoid conflicts and paste the new values for the male API key you can obtain it from the mailtrap admin panel when you access the domain from which you're sending the mail go to the API and SMTP tab copy the API key and paste it into the dot EnV file after adding the changes to the configuration clear the configuration cache using the PHP Artisan config clear command paste the command in the terminal and run it next create a mailable class by generating it using the provided command [Music] you can find this class in the welcomemail.php file from the example given in the documentation simply copy all of this code and replace the one that was previously generated in the welcome mail.php file after that make some changes such as specify the sender's email address and removing the line for sending a copy of the email if that's not needed next create a template for the email in the resources views directory and give this template a specific name in our example it's email.blade.php now paste the text of your template let's use the example from the documentation for the library to know which template to send it's important that the name of the template file matches the one specified in the content method of the mailable class in order to send an email via a console command you'll need to add the calling line identification route to the console.php file navigate to the file and paste the send welcome mail command just make sure that you correct the recipient's email and the name which will appear in the placeholders of the template you'll also need to specify the namespaces for the mail and welcome email classes now you have everything set up to send emails using the laravel framework via API to verify that everything is working correctly run the console command let's check our inbox and there you go the email is there with all the correct details as we specified alright now that we know how to send regular emails via SMTP and API in laravel let's go over how to send HTML emails in our case we already have a registered mailtrap account a verified domain and the latest version of laravel installed so after doing the same you'll need to add the configuration settings in the dot EnV file you can find these settings on the page of the registered domain in mailtrap's dashboard after you've entered all the configuration settings you'll also need to create a mailable class which will correspond to the email you're sending and use the appropriate template for the sake of the example we'll just use this simple text as our email template to send the email we created a test route that directly sends the email using mailtrap's SMTP server let's take a look at what our email looks like if we send it now and there it is unformatted and all in one line so to improve the appearance of the email let's include some HTML tags in our email template as we can see here we've added Styles and wrapped each line of text in paragraph tags so that each part of the text starts on a new paragraph alright let's send the email again there it is and we can see the difference in how the email looked before adding HTML tags to the email template and then after now let's take a look at how we can add an attachment to the emails well you've already prepared a test class for sending emails and in this class you can see the attach method which is responsible for sending attachments to allow our email to send attachments we need to add an additional parameter to this class let's call it attached file now we need to specify the file path to the attached method which takes the file path in its Constructor in the static method in our case the file will be stored in an attached file variable after this let's open the place where the email is actually sent in our case it is the test route where we have set up email sending to mailtrap.club gmail.com here we need to pass an additional variable containing the path to the file for example let's use a favicon.ico file that is located in the public directory and now pass this variable as the second parameter to our mail test class all right let's run the test route and see how our email looks and there you go our email with the favicon.ico file attachment in it okay so last but not least let's go over how we can send an email to multiple recipients again we already have a mail test class which is responsible for the email content here you can see the title the email template and dynamic parameters we also have a test route that sends emails to the specified addresses when accessed currently we only have one recipient listed so let's add another recipient now in the for each Loop the email will be sent to each of the addresses specified in the array let's try running our test route and make sure everything works as expected and in our inbox we see two emails one to the first email address and another to the second one also each of them has a corresponding name in addition to specifying multiple recipients we can also indicate who we are sending a carbon copy or a blind carbon copy to this could be done in several ways the first one is in our mailable class find the envelope method and in the Constructor add the CC and BCC parameters the second way is adding these parameters in the mail class okay let's indicate the email addresses so now when sending an email to each recipient two copies will be sent one regular and one hidden let's check this by re-running our test route and in our mailbox we see that two new emails have been delivered one for the first recipient and another for the second recipient in the headers we can see the recipient's address the CC address and the BCC address is of course hidden that's it just like that you can start sending emails using laravel we at mailtrap hope you found this video tutorial helpful thank you for watching this video brought to you by mailtrap an email delivery platform to test send and control your email infrastructure in one place like and share this video And subscribe to our channel to see more tutorials like this one and we will see you at our next one
Info
Channel: Mailtrap
Views: 10,032
Rating: undefined out of 5
Keywords: Mailtrap, sending mail in laravel, sending email in laravel, laravel send email, laravel send mail, how to send email in laravel, send email with laravel, send email using laravel, email sending in laravel, mailtrap laravel, mailtrap laravel config, laravel email send example, send email from laravel, send email laravel gmail, send mail in laravel using smtp, smtp laravel send email, smtp laravel, email api laravel, laravel, laravel10, laravel tutorial, laravel project
Id: lsna1S8y1vg
Channel Id: undefined
Length: 13min 49sec (829 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.