PHP-and-MySQL/C4/Sending-Email-Part-2/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:00 Okay so we have created our HTML form here and determined that the data has been processed through the POST variable when our form has been submitted.
0:12 The next thing I'll do is just check some conditions on this just for the sake of the tutorial
0:22 I'll say if the string length...
0:25 No - first of all I'll check for existence - So if name and message
0:30 This is just saying does this exist and does this exist because as long as they do, they will always have a true value.
0:38 And we are using the "and" operator here which says "Is this true AND is this true".
45 If this is TRUE, we will execute the code here.
0:49 Otherwise I want to kill this script and I'll say "You must enter a name and message"
1:04 And maybe just underline that for effect.
1:07 And inside our block of the code - if this is TRUE we will perform another check.
1:14 So here we have checked with an existence check
1:20 And now here what we will do is run another check.
1:25 How can I word it? I'll do a length check. So I will comment this as length check.
1:32 We will say name or rather the length of the string using the string-length function.
1:40 We check if string-length of name is greater - no, lesser or equal to our max length which is '20'. We can have any number here.
1:55 And the string-length of message is lesser or equal to 300 characters. Obviously, you can have any number here, too.
2:12 Then we will execute this block of code.
2:16 Otherwise we will say "Max length for name is 20 and max length for message is 300".
2:30 Obviously a good thing to do is to store 300 and 20 in variables
2:36 Lets set them here. So you can say "namelen" equals 20 and you can say "messagelen" equals to 300.
2:47 Then what you can do is incorporate this here. So "namelen" there.... oooops so "namelen" there
2:55 And here you could say - oh! lets put that back - and here you can say "messagelen".
3:04 Down here, too, replace these. So these will be dynamically replaced if you are performing the check.
3:12 So here you say "messagelen".
3:15 So let's test this out. The "namelen" is maximum 20 characters so here we can only enter a maximum of 20 characters. So, Alex here.
3:26 In message, I'll enter some text more than 300 characters long. I'll just copy paste this along.
3:33 That should be more than 300 characters now.
3:38 So if I click on "Send me this" button, we get the message - The max length of the name is 20... that's the variable that's been put here
3:49 And the maximum length for this is 300; that's another variable that's taken from here.
3:56 We are taking the check and echoing out this variable as well.
4:02 Presuming that everything is okay we are going to get the email sent to the user.
4:07 I keep stressing this - its the address here and we've already got our subject line here.
4:13 Perhaps we could bring these down here; there is no point setting a variable if the email is not ready to send.
4:20 So this is our setup variables and what we need to send the email. We will also have the...
4:32 We could say "from" but this is similar to email address.
4:38 So we got the "name" already and all we really need is the "message" which again is here.
4:46 We also need some header information which I'll show you soon but I'll head straight to the "mail function".
4:58 The "mail function" is as follows - mail and the first variable you need to include is who this message is "to". So I'll type "to".
5:11 Then the subject of the email which is just "subject".
5:15 Its here. Then we have the body of the email so "body".
5:20 Here we will say body equals the following - This is an email from "name". So we have included the name inside the body of the email.
5:36 Then just use backslash n which is new line - so that's 2 new lines.
5:42 Next we will echo out the message which will be included in this.
5:49 So our body consists of a generic message here, the user's name that we have processed in the form and then two new lines and next we have entered the message that has been entered in our form here. Okay?
6:03 So lets get rid of these.
6:06 That looks good to go.
6:09 As a mail function you may think how that would work but sending email php is quiet easy.
6:21 But when we do actually send the email, we can see that there are some problems.
6:27 We have got a warning - the mail function "send mail from" is not set in php dot ini or custom "From:" header missing.
6:36 I have not set the "send mail from" in my ini. I missed that. So I'll do it manually.
6:44 After we have done this, we'll run into another error as well.
6:48 I will teach you how to fix it but in the next part of the video.
6:52 In the next part we'll deal with anymore errors we get after that.
6:56 So join me in the next part. Bye for now. This is Evan Varkey dubbing for the Spoken Tutorial Project.

Contributors and Content Editors

Chandrika, Pravin1389