|

How To Send Email To New Users

How To Send Email To New Users

Almost every modern web application has the ability to send email to users at various points in time. In fact, some might say that some of these companies have downright abused the idea of sending user email notifications and send them at every possible instance. We’re not going to look at how to do that here. It might be nice however to be able to send a Welcome Email to new users that sign up for your web application. We’re going to test out this very concept right now in this tutorial about sending email to new users when using Laravel.


Set up an Email Testing Configuration

First off, you’re going to need a way to test the act of sending email from your application. I suppose you could send all emails to your own personal email address during the application development, but there might be a better way. For this task, we will make use of Mailtrap.io where you can quickly set up a demo inbox. This way, you can send emails from your application, and see what they will look like when your users receive them. It’s really a neat little service.
mailtrap for testing email

Once you have your Mailtrap.io account and demo inbox in place, you can populate the .env file of your Laravel installation similar to what you see here:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_user_name
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=hello@welikegames.dev
MAIL_FROM_NAME='We Like Games Team'

Create a new email to send

Just like most things in Laravel, you won’t need to create anything from scratch to send an email. You can make use of artisan to quickly generate a new mail class. We’d like to send an email to new users, so let’s type php artisan make:mail Hello to create a new Hello email class we can send to new users of the application.mail class generator

This creates a new Mail directory as we can see here:new mail directory to hold mail classes

The resulting Hello class boilerplate is stubbed out for you as we see below:
app/Mail/Hello.php


Emails need a view file

In that code we see above, note that the build() method is referencing a view file. We will need that so let’s go ahead and create a new file in resources/views/emails/hello.blade.php. Notice that we called our view hello.blade.php, so you will need to remove view.name and replace with emails.hello in the build() method above. Our simple view file can contain something as simple as:

resources/views/emails/hello.blade.php


Set up a simple test route to send an email

Now that we have all the infrastructure set up, we can try firing off a simple test email on a test route. Just for grins, we’ll email Tom from our routes file.

routes/web.php

With this in place, we can visit the /testemail route, and it will take just a moment to trigger an email.
sending a test email

If we take a look at our demo inbox over at Mailtrap, we can see the email that got sent! Very Cool!successful test email sent to mailtrap


Personalize Your Email

I’m so pumped that we got this test email working, but it was just a super basic email. That email could have gone to anyone. Let’s not be so impersonal. We should address that email to the person’s name, just like any other email. What we will do is to log in an existing user, then use that user instance to personalize the email. Here is how we can modify our code to do just that.

First we update our /testemail route.
routes/web.php

Then, we need to modify our Hello mailer class to make use of a User instance. Here is a neat bit of information. Just by making the $user instance public in this class, the associated view is going to have access to that variable. We don’t need to manually send it through via with() or anything like that. If it helps keep things more clear, you can still do so however.

app/Mail/Hello.php

Finally, we’ll need to update our mail view file so we can access the user’s name.

resources/views/emails/hello.blade.php

Let’s go ahead and test out that /testemail route one more time!sending personalized email

Pretty cool! We can see that we sent an email to Mario, which if you have been following along in this series of building a games app, was the very first user to register to the site.


Let’s email that new user

Ok, all of the legwork up until now was done just to test things out. We can now remove all that testing mail code from our routes file and put some mail code where we want to email a new user. Where is that? Well it makes sense that we would place this in the store() method of the Registration Controller. Let’s see how we can do that here.

app/Http/Controllers/RegistrationController.php

All we had to do was import the Hello class, and include that super simple line of code to email the user: \Mail::to($user)->send(new Hello($user)); That is slick! Let’s test it out. Looks like Gannon is in town.new user registration to send emailnew user sign up worked

And if we head on over to Mailtrap, it looks like the new user email was sent successfully!we sent a new user email


Make your mail pretty with Markdown

Let’s now take a moment to test out the Markdown formatted features that Laravel offers. This is a great way to create some really good looking emails with little to no fuss. We’ll generate a new view file and specify the markdown flag by typing: php artisan make:mail HelloThere –markdown=”emails.hellothere”. We get a new HelloThere class file, as well as a new hellothere.blade.php file in our emails folder.
newly generated markdown email class

The generated hellothere.blade.php file has some boilerplate to get you going. This is what it starts out as.

resources/views/emails/hellothere.blade.php

You’ll notice that at the bottom of this file, a variable of config(‘app.name’) is being used. This is set to ‘Laravel’ by default. You can update this to match the name of your application by going to config/app.php and updating the name key like you see here.updating the name of your laravel application

With this in place, we can update the view file to customize the message, and test it out!

If we’d like to send a test email without having to actually register a new user, we can do so via tinker:sending email with tinker

If we look at our email that we received at mailtrap, we can see it looks pretty darn good!markdown email is pretty


Customize The Markdown Theme

I think we can agree that the styling of the markdown powered emails looks quite nice, and saves a lot of time and work especially for the design challenged like myself. In fact, the default theme looks great, but if you would like to screw things up customize how it looks you can do so. For this you will need to publish your own theme. You do this by typing something like php artisan vendor:publish –tag=laravel-mail at the command prompt. What this does is take any vendor files associated with mail and publishes them to your project root so you can modify them however you like.

By running this command, you now have a whole new set of files in resources/views/vendor/mail. What we did below was to copy the default.css file into a new file of welikegames.css. Once this is done, you can hack that new css file to your heart’s content to customize the look of your markdown email. You will also need to visit your mail.php file and change the theme key from default to welikegames (or whatever you name yours). Both screenshots are shown here.creating your custom themechanging the markdown theme in laravel

Now that we have made some edits to the css styling of the new theme, let’s send some more email and see how it looks.
Customized
customized markdown email is pretty
Defaultmarkdown email is pretty

Awesome Stuff!


How To Send Email To New Users Summary

All applications are going to need to send email to their users at some point in time. With this tutorial, we covered how to do just that. For our purposes, we looked at sending an email to new users of our Games site. You may need to email users for other reasons, but the process is the same. We made use of Mailtrap as a great way to test sending emails, without having to use an actual live email address which is great. What is really fantastic is that you will see exactly what your users will see when you email them. Artisan made it easy for us to generate new mail class files. From there we saw how to send an email using those files in both our routes and controller files. Finally, we had a look at how Markdown styling is now incorporated as a feature into Laravel. It makes for really nice looking emails that can be sent by even us backend developers with ease.