1 00:00:00,000 --> 00:00:00,230 2 00:00:00,230 --> 00:00:02,980 >> DAVID MALAN: Let's improve upon the website I'm making, via which freshman 3 00:00:02,980 --> 00:00:06,910 can register for intramural sports, by emailing them when they have 4 00:00:06,910 --> 00:00:07,660 registered. 5 00:00:07,660 --> 00:00:11,180 How to do this-- well, here in froshims-3, notice that the only 6 00:00:11,180 --> 00:00:16,480 change I've made now is to update the value of action to register-3.php. 7 00:00:16,480 --> 00:00:21,290 In register-3.php now, notice that I'm first requiring a file called 8 00:00:21,290 --> 00:00:22,540 class.phpmailer.php. 9 00:00:22,540 --> 00:00:24,230 10 00:00:24,230 --> 00:00:28,380 This belongs to a library called PHPMailer that exists somewhere inside 11 00:00:28,380 --> 00:00:32,870 of the computer, and I'm specifying here that PHP should require its use, 12 00:00:32,870 --> 00:00:36,700 just like #include in C requires a header file. 13 00:00:36,700 --> 00:00:42,450 >> Next I check if name is not empty and gender is not empty and dorm is not 14 00:00:42,450 --> 00:00:46,080 empty, then let's proceed to do the following. 15 00:00:46,080 --> 00:00:50,910 First I'm going to even instantiate an object of type PHPMailer. 16 00:00:50,910 --> 00:00:54,250 This is somewhat new syntax, but it's a feature of an object-oriented 17 00:00:54,250 --> 00:00:56,450 programming language, which PHP is. 18 00:00:56,450 --> 00:01:01,060 In particular, this effectively declares a variable of type PHPMailer 19 00:01:01,060 --> 00:01:03,680 and calls ultimately that variable mail. 20 00:01:03,680 --> 00:01:06,820 Let's now use that variable to send an email. 21 00:01:06,820 --> 00:01:11,660 >> Let's first specify by calling a function called isSMTP that I would 22 00:01:11,660 --> 00:01:15,380 indeed like to send this email using the protocol SMTP. 23 00:01:15,380 --> 00:01:19,550 Let's next specify that the host via which I'll send this mail shall be, 24 00:01:19,550 --> 00:01:21,526 for instance here on campus, smtp.fas.harvard.edu. 25 00:01:21,526 --> 00:01:24,620 26 00:01:24,620 --> 00:01:29,500 Let's then set the from address of this email to jharvard@cs50.net. 27 00:01:29,500 --> 00:01:34,250 Let's then add the address of jharvard@cs50.net so that, simply, 28 00:01:34,250 --> 00:01:37,330 this time John Harvard will be emailing himself. 29 00:01:37,330 --> 00:01:40,990 And let's then set the subject of this email to registration. 30 00:01:40,990 --> 00:01:44,680 >> Lastly, let's set the body of this email to be the following string. 31 00:01:44,680 --> 00:01:46,460 This person just registered. 32 00:01:46,460 --> 00:01:47,330 Name-- 33 00:01:47,330 --> 00:01:48,200 such and such. 34 00:01:48,200 --> 00:01:48,860 >> Captain-- 35 00:01:48,860 --> 00:01:49,710 such and such. 36 00:01:49,710 --> 00:01:50,330 Gender-- 37 00:01:50,330 --> 00:01:51,230 such and such. 38 00:01:51,230 --> 00:01:51,800 Dorm-- 39 00:01:51,800 --> 00:01:52,940 such and such. 40 00:01:52,940 --> 00:01:56,800 Notice that this string extends onto multiple lines, but I've concatenated 41 00:01:56,800 --> 00:02:00,800 them together using PHP's dot operator so that at end of the day, this is 42 00:02:00,800 --> 00:02:06,100 real just one long string broken here in my text editor onto multiple lines. 43 00:02:06,100 --> 00:02:08,070 >> Now it's time to send the email. 44 00:02:08,070 --> 00:02:11,380 Here I'll call a function called Send, but I'll check whether it's return 45 00:02:11,380 --> 00:02:12,470 value is false. 46 00:02:12,470 --> 00:02:16,540 If so, I'm simply going to die, so to speak, by printing that error. 47 00:02:16,540 --> 00:02:18,340 >> Now notice one other piece of syntax. 48 00:02:18,340 --> 00:02:20,510 Throughout these several lines of code, I've made use 49 00:02:20,510 --> 00:02:22,260 of this arrow operator. 50 00:02:22,260 --> 00:02:26,060 Much like in C, where the arrow operator dereferences a pointer and 51 00:02:26,060 --> 00:02:29,880 leads you to some value, similarly here does this allow you to access a 52 00:02:29,880 --> 00:02:34,910 field inside of an object, in this case an object of type PHPMailer. 53 00:02:34,910 --> 00:02:37,960 Now were I to now visit this page, I'd see the following. 54 00:02:37,960 --> 00:02:42,600 If I now provide David Malan as my name, captain as captain, I'll specify 55 00:02:42,600 --> 00:02:47,340 mail, and I'll live here in Matthews and then click register, I should soon 56 00:02:47,340 --> 00:02:48,590 receive that email. 57 00:02:48,590 --> 00:02:50,496