1 00:00:00,430 --> 00:00:06,960 In this lesson we'll create this send a message dot BHP file which is responsible for validating our 2 00:00:06,960 --> 00:00:15,790 contact form data and transmitting the email using the S.A. IP protocol so start a new document and 3 00:00:15,790 --> 00:00:26,420 save the file as send NSG dot BHP and make sure you save it in your perfect cup directory although we 4 00:00:26,420 --> 00:00:30,230 won't be storing this data into our database. 5 00:00:30,360 --> 00:00:40,680 We're going to start our p p script with a database connection this will come in handy if you ever need 6 00:00:40,680 --> 00:00:45,120 to store the contact form submissions into a database. 7 00:00:45,130 --> 00:00:51,700 This is commonly done for future reference and that way you can track your customers. 8 00:00:51,730 --> 00:01:00,250 Next we'll request the form data using the post method and we've stored the data into three variables 9 00:01:00,280 --> 00:01:02,710 f name email and message. 10 00:01:02,800 --> 00:01:07,270 So this is the data coming from our contact form. 11 00:01:07,270 --> 00:01:12,910 We've used my Eskew Well I real escape string just to prevent any Eskew oil injection. 12 00:01:12,910 --> 00:01:15,870 And so again that adds an additional layer of security. 13 00:01:15,880 --> 00:01:21,270 We also need to create a variable that defines the recipient's email address. 14 00:01:21,370 --> 00:01:25,400 So we're going to store that in a variable called email too. 15 00:01:25,480 --> 00:01:30,720 This is where the form data will submit to. 16 00:01:30,850 --> 00:01:34,270 So you'll receive this email here. 17 00:01:34,450 --> 00:01:36,200 So you're going to plug in your email address. 18 00:01:36,220 --> 00:01:43,060 Next will create a variable that stores the subject line of the contact form so we'll just put test 19 00:01:43,060 --> 00:01:46,810 the message and now we'll validate the form data. 20 00:01:46,810 --> 00:01:50,770 So this is just done in a basic if statement and we've done this quite a few times. 21 00:01:50,800 --> 00:01:57,180 If f name is greater than 50 characters we're going to echo the error code F name long. 22 00:01:57,550 --> 00:02:03,910 If it's less than two characters f name short if the email is more than 50 characters email long if 23 00:02:03,910 --> 00:02:10,190 it's less than two characters emails short if the email is in the incorrect format we have the e format 24 00:02:10,220 --> 00:02:14,270 error code and message long and message short. 25 00:02:14,270 --> 00:02:18,660 If validation passes we have our L statement. 26 00:02:18,680 --> 00:02:23,690 If the validation passes we can proceed to sending the message using p p Mailer. 27 00:02:23,810 --> 00:02:30,530 We start with a require statement that includes an essential file in the p p Mailer directory and that's 28 00:02:30,530 --> 00:02:33,530 called P P Mailer auto load dot BHP. 29 00:02:33,560 --> 00:02:42,380 Next we create a new P P Mailer object and store that in a mail in a variable called mail and this is 30 00:02:42,380 --> 00:02:49,540 the actual email submission code block and I'll just go through it. 31 00:02:50,140 --> 00:02:58,180 So the this line over here sets them the BHP mailer to use the SMP P protocol. 32 00:02:58,180 --> 00:03:03,740 Next we have the host the s empty P host is your outgoing mail provider. 33 00:03:03,790 --> 00:03:15,950 So an example would be G.M. if you're using gmail as your outgoing email provider the SMP host is SMP 34 00:03:15,950 --> 00:03:19,130 PDR gmail dot com. 35 00:03:19,140 --> 00:03:24,840 Now if you're not using Gmail this information would be available through your email service providers 36 00:03:24,840 --> 00:03:30,360 Web site or you can call them depending on who you're using. 37 00:03:30,430 --> 00:03:35,490 Next we specify that our host requires authentication which is almost always the case. 38 00:03:35,560 --> 00:03:43,370 So that's why it's at the true we then set the user name which is your email address and the password 39 00:03:43,370 --> 00:03:45,360 you use for this email account. 40 00:03:45,380 --> 00:03:49,800 I've left it blank but you will need to plug that in. 41 00:03:49,840 --> 00:03:59,390 Next we enable TR less encryption for security purposes Gmail uses pt. 5 8 7 to send emails via SMP. 42 00:03:59,530 --> 00:04:05,020 So again you'll need to check with with your service provider to find out which port they use. 43 00:04:05,020 --> 00:04:14,970 Next we set up our reply to email and so that's stored in our email variable from email is stored in 44 00:04:14,970 --> 00:04:18,510 the email to variable. 45 00:04:18,540 --> 00:04:20,300 This is who the email is from. 46 00:04:20,320 --> 00:04:30,740 And for that we have used f name to the person that filled out the contact form is the one sending the 47 00:04:30,740 --> 00:04:34,730 message. 48 00:04:34,820 --> 00:04:37,340 You can add additional recipients as well. 49 00:04:38,690 --> 00:04:44,180 So here we've just plugged in a demo a demo email you don't have to have additional recipients. 50 00:04:44,180 --> 00:04:44,960 This is optional 51 00:04:47,620 --> 00:04:56,170 this line sets the email to HMO format and that's better for styling purposes over text based emails 52 00:04:58,240 --> 00:05:02,520 than we have the email subject which is stored in the subject variable. 53 00:05:02,780 --> 00:05:10,610 The email content the body content which is stored in the message variable so remember we defined all 54 00:05:10,610 --> 00:05:11,830 these variables up here. 55 00:05:11,840 --> 00:05:20,330 All this data comes from our contact form on the contact page. 56 00:05:20,330 --> 00:05:28,880 Lastly we have an if statement if the email message was not sent for any reason we're going to echo 57 00:05:29,940 --> 00:05:31,970 message could not be sent. 58 00:05:32,220 --> 00:05:39,180 Now the only other alternative to this is that the email was sent in which case we're going to echo 59 00:05:39,180 --> 00:05:42,160 the word true this code. 60 00:05:42,170 --> 00:05:48,140 This access code will then be sent to our contact dot BHP page and interpret it using JavaScript and 61 00:05:48,200 --> 00:05:51,520 Ajax to alert the user of the success. 62 00:05:51,530 --> 00:05:52,010 State 63 00:05:55,210 --> 00:05:57,310 and so that's the entire script there. 64 00:05:58,250 --> 00:06:04,250 So I'm going to refresh the contact got BHP page. 65 00:06:04,390 --> 00:06:08,640 Now I've plugged in our password for this email account. 66 00:06:08,830 --> 00:06:11,020 I'll just fill out the contact form details 67 00:06:18,800 --> 00:06:19,830 and click to submit an 68 00:06:23,450 --> 00:06:25,490 SO HERE WE GET THE SUCCESS message. 69 00:06:25,490 --> 00:06:29,890 Message sent and it should appear in the inbox and there it is. 70 00:06:29,890 --> 00:06:30,610 It's fairly quick 71 00:06:33,910 --> 00:06:37,530 and so that's what the email looks like in the inbox. 72 00:06:37,640 --> 00:06:39,560 You can see the subject text message 73 00:06:42,880 --> 00:06:51,070 and it's from Bob to admin at you excel dot com and then the message body 74 00:06:54,110 --> 00:06:59,310 if we click reply you can see that the reply address is Bob. 75 00:06:59,410 --> 00:07:03,460 Gmail dot com so everything is working the way it should be.