WEBVTT 1 00:00:00.170 --> 00:00:01.380 Hi, welcome back. 2 00:00:01.380 --> 00:00:04.270 In this video, we're going to look at MailGun. 3 00:00:04.270 --> 00:00:09.260 MailGun is one of a family of email sending services. 4 00:00:09.260 --> 00:00:11.780 And MailGun, in particular, I like because it's 5 00:00:11.780 --> 00:00:15.330 really straightforward to use, and it's not, 6 00:00:15.330 --> 00:00:16.360 you know, the most powerful 7 00:00:16.360 --> 00:00:18.090 email sending service in the world, 8 00:00:18.090 --> 00:00:19.970 but it sort of does everything you need 9 00:00:19.970 --> 00:00:23.390 to send simple emails using Python, or indeed, 10 00:00:23.390 --> 00:00:25.610 any other programming language. 11 00:00:25.610 --> 00:00:29.250 Now, if you want to send an email using MailGun 12 00:00:29.250 --> 00:00:33.513 to anyone in the world, you need to have a domain name. 13 00:00:34.380 --> 00:00:38.180 For example, mailgun.com is a domain name, 14 00:00:38.180 --> 00:00:41.180 and collabacode.com is a domain name. 15 00:00:41.180 --> 00:00:42.770 If you don't have a domain name, 16 00:00:42.770 --> 00:00:44.950 you can still go through this lecture 17 00:00:44.950 --> 00:00:46.680 and learn about using MailGun, 18 00:00:46.680 --> 00:00:49.633 but you'll only be able to send emails to yourself. 19 00:00:50.660 --> 00:00:52.730 So, that's just something to take into consideration. 20 00:00:52.730 --> 00:00:55.220 If you don't have a domain, the assumption is 21 00:00:55.220 --> 00:00:57.790 you don't have people who want to receive your emails, 22 00:00:57.790 --> 00:01:00.530 or who have signed up to receive your emails yet. 23 00:01:00.530 --> 00:01:03.060 So, that's why they don't let you send emails to people 24 00:01:03.060 --> 00:01:06.173 if you haven't got a domain name registered with MailGun. 25 00:01:07.120 --> 00:01:09.210 And so, what we're gonna do, in order to learn about 26 00:01:09.210 --> 00:01:10.610 sending emails with MailGun is 27 00:01:10.610 --> 00:01:12.840 we're going to first sign up. 28 00:01:12.840 --> 00:01:15.650 Now, to sign up is free, and it takes 29 00:01:15.650 --> 00:01:17.410 57 seconds, allegedly. 30 00:01:17.410 --> 00:01:18.780 I'm sure it takes longer than that. 31 00:01:18.780 --> 00:01:20.760 But here, all you wanna do is, you know, 32 00:01:20.760 --> 00:01:22.320 fill in the details. 33 00:01:22.320 --> 00:01:26.460 You will not need to add payment info now, if you want, 34 00:01:26.460 --> 00:01:28.260 at least not at the time of recording, 35 00:01:28.260 --> 00:01:31.840 and you wanna stick to the free plan, okay? 36 00:01:31.840 --> 00:01:34.720 If you add payment info, it's going to put you on 37 00:01:34.720 --> 00:01:37.100 the pay-as-you-go plan, which means, 38 00:01:37.100 --> 00:01:40.010 in the unlikely event that you do an infinite loop 39 00:01:40.010 --> 00:01:42.400 of sending emails, you will have to pay. 40 00:01:42.400 --> 00:01:44.640 So the best thing to do, don't add payment info 41 00:01:44.640 --> 00:01:47.990 if you don't wanna pay, and that'll give you the free plan, 42 00:01:47.990 --> 00:01:50.340 and you won't be able to go over it. 43 00:01:50.340 --> 00:01:52.900 So, in case you make something wrong, 44 00:01:52.900 --> 00:01:54.935 nothing bad will happen, okay? 45 00:01:54.935 --> 00:01:57.480 So, just fill in your details, click "I'm not a robot," 46 00:01:57.480 --> 00:01:59.660 and then you will have your account. 47 00:01:59.660 --> 00:02:01.240 Now let's go over to what happens 48 00:02:01.240 --> 00:02:02.890 when you have an account already. 49 00:02:04.030 --> 00:02:06.570 When you log into MailGun, you'll be received with something 50 00:02:06.570 --> 00:02:08.770 that looks more or less like this. 51 00:02:08.770 --> 00:02:09.850 A couple of things are very different 52 00:02:09.850 --> 00:02:13.080 because I have a domain that I've added to MailGun already, 53 00:02:13.080 --> 00:02:14.330 and you won't have that. 54 00:02:14.330 --> 00:02:18.160 But nonetheless, you'll have sort of a dashboard. 55 00:02:18.160 --> 00:02:20.470 If you go over to your domains list, 56 00:02:20.470 --> 00:02:24.370 you'll have a sandbox domain, such as this one. 57 00:02:24.370 --> 00:02:27.570 With a sandbox domain, you can only send emails 58 00:02:27.570 --> 00:02:29.820 to the email you signed up with. 59 00:02:29.820 --> 00:02:31.900 So, in my case, that's up here on the right, 60 00:02:31.900 --> 00:02:33.500 jose@schoolofcode.me. 61 00:02:33.500 --> 00:02:34.520 This is an old account. 62 00:02:34.520 --> 00:02:36.240 I no longer use School of Code. 63 00:02:36.240 --> 00:02:37.630 If you're interested in our stuff, 64 00:02:37.630 --> 00:02:41.560 it's CollabaCode now, .com, in fact. 65 00:02:41.560 --> 00:02:44.313 So, this sandbox domain, you can click on it, 66 00:02:45.310 --> 00:02:49.130 and you'll be received with some things here. 67 00:02:49.130 --> 00:02:51.283 And default SNTP login. 68 00:02:52.190 --> 00:02:56.280 This is the email account that you can use using SNTP. 69 00:02:56.280 --> 00:02:58.480 You've got an API-based URL, 70 00:02:58.480 --> 00:03:01.030 you've got a password, and you've got an API key. 71 00:03:01.030 --> 00:03:04.460 So, the SNTP login and the password 72 00:03:04.460 --> 00:03:07.260 are used to log in via SNTP, 73 00:03:07.260 --> 00:03:09.840 as we saw in the last couple of videos. 74 00:03:09.840 --> 00:03:10.673 Two videos ago? 75 00:03:10.673 --> 00:03:13.460 Yeah, I think so. 76 00:03:13.460 --> 00:03:14.423 Or the last video. 77 00:03:15.350 --> 00:03:18.170 Nonetheless, when you are sending emails using Gmail, 78 00:03:18.170 --> 00:03:21.148 you can instead swap that for 79 00:03:21.148 --> 00:03:23.460 this default SNTP login and default password, 80 00:03:23.460 --> 00:03:28.100 and you can send emails using SNTP, using these two things. 81 00:03:28.100 --> 00:03:32.170 The other option is to go on a more Pythonic scale 82 00:03:32.170 --> 00:03:33.840 and use the API. 83 00:03:33.840 --> 00:03:35.800 Using the API is pretty straightforward. 84 00:03:35.800 --> 00:03:38.480 All you need to do is instal requests 85 00:03:38.480 --> 00:03:41.250 and run an API request. 86 00:03:41.250 --> 00:03:44.283 So, we're gonna look at using that in this video. 87 00:03:45.490 --> 00:03:48.690 Okay, now in the authorised recipients, 88 00:03:48.690 --> 00:03:51.113 you're gonna wanna add your own email. 89 00:03:52.220 --> 00:03:53.983 So, this is actually quite new. 90 00:03:56.017 --> 00:03:57.760 It used to be you could only send to your own email, 91 00:03:57.760 --> 00:04:00.080 but it seems that now there's a new thing. 92 00:04:00.080 --> 00:04:05.080 So, you can invite yourself, and there, in my recipient, 93 00:04:05.690 --> 00:04:07.707 and I'm guessing I'll get an email saying, 94 00:04:07.707 --> 00:04:10.310 "Hey, confirm you wanna be emailed by this guy," 95 00:04:10.310 --> 00:04:11.570 and we're gonna say yes. 96 00:04:11.570 --> 00:04:13.318 So, if that's the case, I'm sorry about 97 00:04:13.318 --> 00:04:14.151 the misinformation earlier. 98 00:04:14.151 --> 00:04:19.140 This is the new way to do it, apparently, which is good. 99 00:04:19.140 --> 00:04:19.973 And that's it. 100 00:04:19.973 --> 00:04:22.770 So, all you need, now that you've invited 101 00:04:22.770 --> 00:04:27.600 your authorised recipients is get the API-based URL 102 00:04:27.600 --> 00:04:30.110 and the API key, run with these details, 103 00:04:30.110 --> 00:04:33.520 and go over to PyCharm, where we will continue 104 00:04:33.520 --> 00:04:36.183 setting up this and sending emails using MailGun. 105 00:04:37.490 --> 00:04:41.640 So, let's go over to PyCharm after we copy this. 106 00:04:41.640 --> 00:04:45.900 We're gonna put MailGun API URL. 107 00:04:45.900 --> 00:04:49.837 It's gonna be this, and MailGun API key 108 00:04:51.720 --> 00:04:53.773 is going to be this key. 109 00:04:56.640 --> 00:04:59.260 There you have it, now we have our two 110 00:04:59.260 --> 00:05:01.370 different values here. 111 00:05:01.370 --> 00:05:06.030 What we need is also a way to store 112 00:05:06.030 --> 00:05:07.480 who the emails are coming from, 113 00:05:07.480 --> 00:05:10.660 who they're going to, and sort of the subject 114 00:05:10.660 --> 00:05:13.130 and the content of the email. 115 00:05:13.130 --> 00:05:18.130 Okay, so FROM_NAME is going to be Jose, in this case. 116 00:05:19.160 --> 00:05:24.160 FROM_EMAIL is going to be jose@schoolofcode.me. 117 00:05:27.342 --> 00:05:30.920 TO_EMAILS, and this is going to be a list of emails, 118 00:05:30.920 --> 00:05:35.217 is going to be another email, another@gmail.com. 119 00:05:35.217 --> 00:05:37.090 And you can put multiple emails there if you want, 120 00:05:37.090 --> 00:05:40.430 so you can send emails to more than one person. 121 00:05:40.430 --> 00:05:43.460 The SUBJECT is going to be "Test e-mail" for me, 122 00:05:43.460 --> 00:05:45.877 and the CONTENT is just going to be a simple content, 123 00:05:45.877 --> 00:05:48.517 "Hello, this is a test e-mail." 124 00:05:49.810 --> 00:05:51.970 Okay, now we've got all our data. 125 00:05:51.970 --> 00:05:55.220 And notice how these are all constants in Python. 126 00:05:55.220 --> 00:05:57.690 Constants in Python are always uppercase, 127 00:05:57.690 --> 00:06:00.580 and that just means we're not going to change them. 128 00:06:00.580 --> 00:06:05.040 Then all we have to do is import requests, 129 00:06:05.040 --> 00:06:06.710 so we're gonna do import requests. 130 00:06:06.710 --> 00:06:09.070 If you don't have it installed, just instal it 131 00:06:09.070 --> 00:06:12.450 in the environment that you're using for this project, 132 00:06:12.450 --> 00:06:17.450 and then we do requests.post, and here it goes. 133 00:06:17.570 --> 00:06:20.327 We're gonna put MailGun, MAILGUN_API_URL. 134 00:06:23.440 --> 00:06:28.440 This is where the request is going to go to. 135 00:06:30.970 --> 00:06:34.430 Then we've got an auth variable here, 136 00:06:34.430 --> 00:06:37.680 and the auth variable is a tuple. 137 00:06:37.680 --> 00:06:41.020 So, the first element of a tuple is API, 138 00:06:41.020 --> 00:06:43.140 and the second one is API_KEY. 139 00:06:44.560 --> 00:06:46.110 Okay, so there's a tuple there. 140 00:06:46.960 --> 00:06:49.360 And all this says is the request is gonna go through 141 00:06:49.360 --> 00:06:52.660 with some data to the API URL, 142 00:06:52.660 --> 00:06:54.970 and in that data it's going to contain 143 00:06:54.970 --> 00:06:56.970 this authentication data. 144 00:06:56.970 --> 00:06:58.550 This is called Baisc Auth. 145 00:06:59.966 --> 00:07:00.799 This is Basic Auth, and essentially, 146 00:07:04.350 --> 00:07:06.730 it just contains sort of a key and a value, 147 00:07:06.730 --> 00:07:09.823 and they can identify you with this value here. 148 00:07:10.690 --> 00:07:11.870 It's not terribly secure. 149 00:07:11.870 --> 00:07:13.280 It's not the most secure way of doing things, 150 00:07:13.280 --> 00:07:16.180 but it is basic and simple. 151 00:07:16.180 --> 00:07:18.690 And then we're gonna have a data dictionary, 152 00:07:18.690 --> 00:07:21.630 and in this dictionary, we're gonna have a from, 153 00:07:21.630 --> 00:07:23.340 which is gonna be the FROM_EMAIL. 154 00:07:25.500 --> 00:07:29.100 Then we are going to have a to. 155 00:07:29.100 --> 00:07:30.757 That is going to be TO_EMAILS. 156 00:07:32.100 --> 00:07:33.150 We're gonna have a subject. 157 00:07:33.150 --> 00:07:34.920 That is going to be the SUBJECT. 158 00:07:34.920 --> 00:07:38.000 And text, that is going to be the CONTENT. 159 00:07:38.000 --> 00:07:40.150 Now, where is FROM_NAME used? 160 00:07:40.150 --> 00:07:43.500 Well, this from is actually not meant to be just an email. 161 00:07:43.500 --> 00:07:47.450 It's meant to be a f strain that contains 162 00:07:47.450 --> 00:07:52.450 the FROM_NAME, and then FROM_EMAIL here as well. 163 00:07:53.940 --> 00:07:55.980 Okay, now we've got here the name 164 00:07:55.980 --> 00:07:57.010 and the email's coming from, 165 00:07:57.010 --> 00:07:59.680 and this format, I'm sure you've seen it before, 166 00:07:59.680 --> 00:08:03.020 like when you get an email through, 167 00:08:03.020 --> 00:08:04.840 I'm not gonna say a legacy system, 168 00:08:04.840 --> 00:08:06.710 but through something like Outlook or something like that, 169 00:08:06.710 --> 00:08:10.240 normally it's like Rolf Smith, and it's like 170 00:08:10.240 --> 00:08:14.590 rolf.smith@example.net. 171 00:08:14.590 --> 00:08:16.240 So yeah, it's normally in this format, 172 00:08:16.240 --> 00:08:18.397 and this is the format we're replicating here as well, 173 00:08:18.397 --> 00:08:20.350 and MailGun understands that, 174 00:08:20.350 --> 00:08:24.870 and it allows us to give a name to the sender, 175 00:08:24.870 --> 00:08:26.300 so that when somebody receives it, 176 00:08:26.300 --> 00:08:27.950 they'll see who it's coming from, 177 00:08:27.950 --> 00:08:30.160 as opposed to just an email. 178 00:08:30.160 --> 00:08:32.520 Okay, I'm gonna put this in the same line, 179 00:08:32.520 --> 00:08:34.740 just because that looks a bit nicer to me. 180 00:08:34.740 --> 00:08:36.230 And that's it. 181 00:08:36.230 --> 00:08:41.033 Once the authorised recipient is successfully authorised, 182 00:08:41.890 --> 00:08:44.050 you can run this, and it'll send the request 183 00:08:44.050 --> 00:08:46.600 through the MailGun API URL. 184 00:08:46.600 --> 00:08:49.070 It'll authenticate you with your API key, 185 00:08:49.070 --> 00:08:50.950 and then it'll send this data over, 186 00:08:50.950 --> 00:08:52.810 and they will do all the hard work of 187 00:08:52.810 --> 00:08:55.820 constructing the email, making sure it's sent, 188 00:08:55.820 --> 00:08:59.440 and of course, it's really simple to put this in a function 189 00:08:59.440 --> 00:09:02.380 and make it take a few parameters, 190 00:09:02.380 --> 00:09:06.180 and very easily you can send email to multiple people. 191 00:09:06.180 --> 00:09:09.750 So normally, when I am making a new Flask application, 192 00:09:09.750 --> 00:09:11.040 I don't wanna send emails. 193 00:09:11.040 --> 00:09:15.780 All I do is, honestly, this, I put in a function, 194 00:09:15.780 --> 00:09:17.040 and then I call that function from 195 00:09:17.040 --> 00:09:18.570 multiple parts of my application, 196 00:09:18.570 --> 00:09:22.490 and it's really simple to send emails with that. 197 00:09:22.490 --> 00:09:24.060 We're going to look, over the next couple of videos, 198 00:09:24.060 --> 00:09:29.060 at how to sort of organise this email sending method 199 00:09:29.140 --> 00:09:32.430 a bit nicely, and that way you can reuse it 200 00:09:32.430 --> 00:09:34.080 in your applications if you want. 201 00:09:35.120 --> 00:09:35.953 So, that's it. 202 00:09:35.953 --> 00:09:37.676 Try it out, let me know if it works, 203 00:09:37.676 --> 00:09:40.910 and hopefully you enjoyed learning about 204 00:09:40.910 --> 00:09:43.000 sending emails using MailGun. 205 00:09:43.000 --> 00:09:44.900 The same method, by the way, or a very similar one, 206 00:09:44.900 --> 00:09:47.883 will apply for other email sending providers like SendGrid. 207 00:09:48.820 --> 00:09:49.930 That's it for this video. 208 00:09:49.930 --> 00:09:51.380 I'll see you on the next one.