0:00
Hi, I'm Christopher Code Review Videos.com and in this video, we're going to cover email inside
0:04
FOS User Bundle. So we're going to cover how to override the templates and also how to sort of
0:09
test email is working throughout development. So the main thing is, again, this is not 100
0:16
FOS user bundle. The configuration of email is sort of a symphony wide issue and then the templates
0:23
are a FOS user bundle override issue. So by default, we get access to the resetting slash request
0:29
and on other routes, we can take a quick look at the routing that's available, so app, console, debug, router
0:37
And if you've been following along, you'll know that we've got the translations going on. But you can see that we've got these different paths for sort of working with our password
0:45
So request a password. And then these are the ones you won't really need direct access to, but you may end up seeing along the way
0:52
depending on where you're at in the process of your request for a new password
0:57
So if you've already requested it, the default with FOS user bundle is you can't re-request a new password within 24 hours
1:04
You've just got to wait those 24 hours and then you can request another password
1:09
Generally, if someone's after a password, then they're going to get in touch with you another way
1:13
So I would suggest giving your users the ability to do so
1:17
Now, when working with emails in development, it's pretty useful to intercept redirects and set that to true
1:24
We'll see what happens if we don't in a sec. and then in your swift mailer, this is in config dev
1:29
so these settings are only going to affect the development environment. In your swift mailer, you can set up a delivery address to somewhere
1:37
Like, Shark Lazers is a bit of a strange one. It's called Griller Mail
1:42
It's like a throwaway email address. Pretty useful for just testing, as it says, disposable, temporary email addresses
1:49
Pretty useful for testing stuff like this. And then also, I will put a link in the show notes to the
1:55
this, but it's just basically using your Gmail credentials for the purposes of testing email
2:01
If you've not got another way of sending out emails, it's pretty useful just to send by your
2:06
Gmail. Most people have a Gmail, I suppose. If you don't, we're setting one up for the purposes
2:11
of development. And then the other thing that we can do is if we go into our config, so our actual
2:17
config and we go to our FOS user bundle settings we can come in here and per well I explain this first I suppose from email we can set this to an address so we could say like Chris at code review videos
2:36
And we could also customize the sender name, so we could have it as Chris, just whatever, whatever you wish to have it as
2:45
And then you can also customize this per settings, like so per, what's the best way to describe that, I suppose
2:53
So if we say the registration section, you may wish to have a confirmation email
2:59
And the confirmation email may come from, let's see, so the from email is going to come from the address of registered at code review or something like that, codereview videos.com, something like that
3:13
You can, it's nice because you can override the more, the more generic ones with a more specific one
3:21
So Chris says hi or something, I don't know. That's just a nice little aside
3:26
So let's go ahead and look at the templates that we get. So we've already covered how to copy in the templates, but just to very quickly go back over it
3:34
So we're in a vendor, Friends of Symphony, user bundle. We've got resources, views, and then we've got all these different folders and files
3:43
All we've done is taken a copy of them and we put them into our resources, Fodge User Bundle Views, so app resources, so we've copied them across effectively
3:53
If you're unsure on that, please do look at the templates video in this series where we've already covered how to do that
3:59
And then inside each of these, depending on what you're doing, you may wish to be overriding the HTML, but in this case, we're wanting to look for the ones with the .txte
4:10
dot twig extension as these are going to be the email templates. Now the email templates again follow the same pattern as all the other HTML templates
4:19
in that. Really, there's not a lot going on. We've just got certain things that need to be in there for our templates to work
4:26
such as the subject has to be defined, the body text has to be defined
4:30
And if you're using HTML emails, you will also want to define something in here
4:35
So this gives you the sort of granularity to be able to send the email types that you wish
4:40
you need to configure HTML emails separately I link to how to do that in the show notes And just a link to a cookbook article for FOS user bundle so don not nothing particularly crazy going on If you not really been following along with the series so far you may be
4:55
confused about lines like this. Really, that's just going through the translation section
5:00
And what you can see there is we're passing in a username and confirmation URL into this
5:05
translation, which is a little outside the scope of translations as a whole. If you're unsure on
5:10
that I have a complete series on translations but what we can do is override these these messages so
5:16
the idea is the translations are set up in such a way that we have resources for us user bundle
5:21
translations it's very similar to how we've copied in the view templates the translations just live
5:27
in a different folder so if you're interested in customizing different language templates again
5:32
I've already covered this in translations video so I'm not going to go into it too much
5:36
but the translations in our case live in here and we can just sort of track
5:40
this back. So it's resetting. email.message. If we go into that template, or sorry, translation
5:47
we can see resetting dot, what was it, what was the one that we was looking at there? Go away. Resetting
5:53
Email.message. Resetting.com email.com message. And then we can override this bit, and you can see
5:59
that's why we're passing in those variables. So to the demonstration section, if we look at the
6:05
resetting email form, we can type in, say, A at. This is just a user that we've set up previously in this series
6:14
We do reset password. We've not started the server, have we? So let's go ahead and do that
6:18
So at console server run. And let's try that again. A.a.com. Go through with that
6:28
And then this should send our email off. And so I've been through and configured my Gmail settings to allow emails to be sent through
6:36
So we should expect to see an email come through there, which is fine
6:40
We got exactly what we expected to see. We probably want to delete that
6:45
Where are we? Go back to the inbox, tick their box and delete
6:50
And so that's fine. But what happens if we don't really want to set up emails
6:54
to actually send out during development? We can go in here. One downside, I suppose
7:00
you're going to need to keep your database open so that we can reset the password requested that field Otherwise it going to say you already requested a password for that user and it not going to be too particularly useful to us So in our config dev again go into web profile
7:15
I go into intercept redirects and set this to true. And what we can do is just go back and this time send another one to A.a.com
7:23
and this time we should intercept that email before it gets sent out. So it says redirection intercepted
7:29
and we can see that we've got a mail this time. See, last time, we wouldn't have been able to see
7:34
that I'll go back through that in a sec. And you can see that that same message has been sent out
7:39
This time with the forced header, which is before it was just sending out with the values I've set up for my Swift Mailer Gmail
7:48
which is why I sort of had to hide them as their personal ones. But yeah, you can see the exact sort of template going through
7:56
And you can just go into this resetting email. We could do whatever we wish in here
8:01
so we can just add in some junk, Just to prove that this is actually working, we'll need to go back to our database
8:07
Just reset that. No, not that one, this one. And just go back and retest this email
8:14
And again, the last thing that we'll do is actually to show you what happens if you don't set up redirects A.8.a.com
8:22
Reset password. Go back through. We should get through now. I accidentally click the link there
8:27
It doesn't really matter. So if we refresh this, you can see that we've got all this nonsense that we've just
8:33
added to the template, which is fine, I suppose. Let's just delete those two as well as anyone really could be accessed in this mailbox
8:39
so I don't really want them seeing everything that we're sending through. And then, so we'll just lead that in and we'll take off intercept redirects as false
8:49
And what happens if you don't do it like that, then if we go back to the reset form
8:56
this time just do it be.b.com, reset password. And you can see at the bottom, we don't actually have a mail, interstate
9:03
So there's not a real, no, that's not the right one. We've not really got a way of looking at that mail that got sent through
9:10
so the email section is not available to us. So again, just set this back to true, go back to our form
9:18
and we'll probably need to reset our users. Password requests. That will just reset a at a.com
9:25
A at a.com. One last time, and we can see that the mail got intercepted
9:30
and so that's where we can look at it