This course will be retired on April 10, 2020. We recommend "Build a Basic PHP Website" for up-to-date content.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
With our message validated and sanitized we can now send an email to ourselves. Well, almost. We will need to install a mailer library to handle this for us! In this video we will install, using composer, the swiftmailer library.
-
0:00
So now that we have our message composed, our mailer created using our transport or
-
0:04
send mail, now we need to take that mailer and send out our message.
-
0:08
So how do we do that, but with another method?
-
0:11
So we'll do $mailer, not our message, and then the method
-
0:16
that we're going to use is called send, which makes a lot of sense to me.
-
0:20
Open and close your parentheses and close it out.
-
0:22
And what do you think we might want to send?
-
0:24
But our message.
-
0:27
Now we can just let it go at this, that's fine.
-
0:30
But what if there's a problem?
-
0:31
Well, the mailer will return to us a value.
-
0:34
So we're going to need to capture that into an actual variable.
-
0:38
So we'll tell it result.
-
0:43
We'll set that equal to the output of the mailer send.
-
0:47
Now the thing that we're going to get here is a response of either zero,
-
0:52
or an integer.
-
0:53
The zero says that no messages were sent.
-
0:56
An integer tells us how many email addresses it went out to.
-
1:00
This helps us find which email addresses are going to be messed up if we have
-
1:03
a large amount of emails.
-
1:05
But for now, we just wanna make sure that at least one was sent,
-
1:08
because that is all that would be sent from this particular application.
-
1:12
We'll do that with a simple conditional.
-
1:15
We're gonna check our result.
-
1:18
And if that result is greater than zero, meaning something as been sent,
-
1:21
then we're good to go.
-
1:26
Now if it hasn't, we're gonna wanna do something.
-
1:34
Go back up here, clean this up a little.
-
1:36
Okay.
-
1:37
So, we're gonna eventually wanna send a message.
-
1:41
To the user that the message failed to send.
-
1:48
We're probably also gonna wanna log this.
-
1:54
That way if we have a Chron job that sends us our logs on a regular basis,
-
1:57
we'll know if we're having some issues constantly.
-
2:00
Now for each one of these, we're always gonna wanna redirect.
-
2:03
Well if there's an error,
-
2:04
I would assume we're gonna wanna redirect back to the contact page.
-
2:07
We've already done that before.
-
2:09
We did it with the $app, okay, and
-
2:11
we set that using the object operator to redirect.
-
2:16
Okay, so once we do our redirect, we're gonna redirect it to our contact page.
-
2:23
Close that out with a semicolon and save it.
-
2:25
Now if it's good, we're going to want to send a message as well.
-
2:33
Maybe just say thank you.
-
2:36
Then we're also going to want to redirect again.
-
2:38
So $app redirect, and
-
2:41
we'll redirect them back to our home page.
-
2:47
And then the message would appear on the home page that says, hey,
-
2:51
thanks, appreciate it.
-
2:53
Okay, so that gives us a good if, else statement to work with.
-
2:57
That's very basic.
-
2:58
There's lots of room for improvement here, but
-
3:00
I just want to see, is this going to send out?
-
3:03
Is this going to work?
-
3:05
All right, so let's see if we end up back at the home page, or
-
3:08
if we end back at the contact page when we run this.
-
3:11
If there's any errors, no problem, we'll just work our way through them.
-
3:14
Let's start by rendering and viewing with the eyeball.
-
3:19
Go to our contact page.
-
3:22
All right. Full name, Hampton Paulk.
-
3:24
Email address, hampton@teamtreehouse.com.
-
3:30
And my message, Hello Waldo.
-
3:32
All right I'm gonna hit Submit.
-
3:36
[NOISE] address and mailbox given does not comply, okay, does not comply.
-
3:42
Well I've done this before, so
-
3:43
I'm gonna show you how I've figured out what the issue is.
-
3:46
So if I hit this website, which is actually the Swift mailers documentation
-
3:50
about messages, I can put this link in the notes.
-
3:53
If you scroll down here, and you're looking at how we set this up, see,
-
3:57
we've got our new instance, our subject, from, to, look at the from.
-
4:03
Notice it's an array with the email address, and then the name.
-
4:06
If you go back over and look at mine, and
-
4:09
you go up to the setFrom, let's close this console out.
-
4:14
Our setFrom here on line 48,
-
4:17
that's gonna show us that we're doing our name first and
-
4:22
then the email, but the standard is to do the email and then the name.
-
4:27
All right, so there's our problem.
-
4:28
So what I'm gonna do is just swap these two.
-
4:31
I'm gonna cut this one out of here, do my email first,
-
4:35
same array pointer here, and then paste in my clean name.
-
4:39
All right, that's one step.
-
4:41
Now, do I need to do anything here?
-
4:43
I'm not sure yet.
-
4:44
Let's just give it another try.
-
4:45
So I'm gonna go back, hit refresh, and then type it in again.
-
4:55
All right, message to Waldo, and then hit Submit.
-
5:01
It directed us back to our homepage, so that should tell me that we have mail.
-
5:05
Great!
-
5:06
How do we check it?
-
5:07
All right, in work spaces, to be a little creative,
-
5:10
we're gonna need to do it through our terminal.
-
5:13
Now this is going back real old school folks, so prepare yourselves.
-
5:16
Show console, and then at the very top, I'm gonna type in mail.
-
5:24
And now it pulls up Heirloom Mail Version 12.5.
-
5:27
That's right, that's what it is.
-
5:28
But if you look, there's a message from a guy named Hampton, that was sent today.
-
5:33
And the message title, or the actual subject is Email from our Website.
-
5:38
So I'm gonna hit 1 and then Enter.
-
5:41
And now you can actually see the email itself, Hello Waldo.
-
5:45
Now in any normal situation, this would've just sent the email to you where ever,
-
5:49
whatever your email is, but here locally, that's how I can check it.
-
5:53
And you can too, just by typing in mail from the console.
-
5:56
I'm gonna actually type delete, and that's gonna delete that message, and
-
6:01
then I'm going to quit out of here by typing quit.
-
6:04
Now if I clear the screen and type in mail again,
-
6:06
you'll see that there is now no mail for treehouse.
-
6:10
But, if I was to send that message again, I'll get another message.
-
6:13
Let's walk through it one last time.
-
6:15
Full name, Hampton Paulk.
-
6:18
Email address, hampton@teamtreehouse.com.
-
6:25
And my message, Hello again good sir.
-
6:30
Submit.
-
6:32
Back to our homepage, no message yet.
-
6:34
But If we go back to our console and type in mail,
-
6:37
we'll now have one from Hampton Paulk using the full name, same subject,
-
6:42
because that's statically set, email from our website.
-
6:46
We type 1 to get to the the message.
-
6:49
There's the message, Hello again, good sir.
-
6:52
I'm gonna type delete.
-
6:54
Hit Enter and then quit, and hit Enter.
-
6:57
And that is how you check your mail from your terminal.
You need to sign up for Treehouse in order to download course files.
Sign up