PHP-and-MySQL/C4/Sending-Email-Part-2/English-timed
From Script | Spoken-Tutorial
Revision as of 22:28, 5 June 2015 by Sandhya.np14 (Talk | contribs)
Time | Narration |
00: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. |
00:12 | The next thing I'll do is just check some conditions on this, just for the sake of the tutorial. |
00:22 | I'll say if the string length... |
00:25 | No, first of all I'll check for existence. So if $name and $message, |
00: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. |
00:38 | And we are using the "AND" operator (&&) here which says "is this true AND is this true". |
00:45 | If this is TRUE, we will execute the code here. |
00:49 | Otherwise I want to kill this script and I'll say "You must enter a name and message". |
01:04 | And may be just underline that for effect. |
01:07 | And inside our block of the code, if this is TRUE we will perform another check. |
01:14 | So, here we have checked with an existence check. |
01:20 | And now here what we will do is run another check. |
01:25 | How can I word it? I'll do a length check. So I will comment this as length check. |
01:32 | We will say $name or rather the length of the string using the string-length function. |
01:40 | We check if this string-length function of '$name' is greater - no, lesser or equal to our 'max length' which is '20'. We can have any number here. |
01:55 | And the string-length of '$message' is lesser or equal to 300 characters. Obviously, you can have any number here, too. |
02:12 | Then we will execute this block of code. |
02:16 | Otherwise we will say "Max length for name is 20 and max length for message is 300". |
02:30 | Obviously a good thing to do is to store 300 and 20 in variables. |
02:36 | Let's set them here. So, you can say "$namelen" equals 20 and you can say "$messagelen" equals to 300. |
02:47 | Then what you can do is, incorporate this here. So "$namelen" there.... oooops so "$namelen" there |
02:55 | and here you could say - oh! let's put that back - and here you can say "$messagelen". |
03:04 | Down here too, replace these. So, these will be dynamically replaced if you are performing the check. |
03:12 | So, here you say "$messagelen". |
03: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. |
03:26 | In Message, I'll enter some text, more than 300 characters long. I'll just copy paste this along. |
03:33 | That should be more than 300 characters now. |
03: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. |
03:49 | And the maximum length for this is 300; that's another variable that's taken from here. |
03:56 | We are taking the check and echoing out this variable as well. |
04:02 | Presuming that everything is okay we are going to get the email sent to the user. |
04:07 | I keep stressing this, it's the address here and we've already got our subject line here. |
04:13 | Perhaps we could bring these down here; there is no point setting a variable if the email is not ready to send. |
04:20 | So, this is our 'setup variables'. We also have the... |
04:32 | we could say "from" but this is similar to the email address. |
04:38 | So, we got the "name" already and all we really need is the "message" which again is here. |
04:46 | We also need some header information which I'll show you soon but I'll head straight to the "mail function". |
04: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. |
05:11 | Then the subject of the email which is just $subject. |
05:15 | It's here. Then we have the body of the email, so $body. |
05: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. |
05:36 | Then, just use backslash n (\n) which is new line, so that's 2 new lines. |
05:42 | Next, we will echo out the '$message' which will be included in this. |
05: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? |
06:03 | So, let's get rid of these. |
06:06 | That looks good to go. |
06:09 | As a 'mail function', you may think how that would work but sending email php is quite easy. |
06:21 | But when we do actually send the email, we can see that there are some problems. |
06:27 | We have got a warning - "the mail() function "send mail_from" is not set in 'php dot ini' or custom "From:" header missing". |
06:36 | I have not set the "send mail from" in my 'ini'. I missed that. So I'll do it manually. |
06:44 | After we have done this, we'll run into another error as well. |
06:48 | I will teach you how to fix it but in the next part of the video. |
06:52 | In the next part, we'll deal with anymore errors we get after that. |
06:56 | So join me in the next part. Bye for now. This is Evan Varkey, dubbing for the Spoken Tutorial Project. |