Python for Entrepreneurs Transcripts
Chapter: Sending and receiving email
Lecture: Demo: Setup for sending email
0:02 Let's add email capabilities to our web app.
0:05 So you'll see that I've copied the previous working version
0:09 to a new folder 13_email, and renamed the project as well.
0:13 So, be sure to open that one to get the code into GitHub project.
0:17 Now, I have also done a few other things in preparation.
0:20 One is I added the ability for us to indicate
0:23 whether our web app is in debug or release mode.
0:26 And, why do we care about that?
0:29 Well, if we were working with some, say, user account
0:32 or testing some kind of bug related to email,
0:34 we don't want to accidentally send a bunch of email to real live users
0:38 and indicate or tell them "hey, something has changed about your account"
0:42 or whatever it is we've triggered,
0:45 when in actuality we only changed our local database and nothing really changed.
0:50 So having this debug mode is pretty important if the emails
0:54 you are messing with might actually go to someone other than you,
0:57 so I'll talk about how we did that, and we also have this init_smtp_mail
1:02 as opposed to the mailing list which we had before,
1:05 so let's just look how this goes together.
1:07 So here I now put little dividers here so you can see what is going on,
1:09 we have a mode, we can set this to dev or we can set it to prod,
1:13 honestly if you set it to anything other than dev
1:15 it's going to treat itself as in production.
1:20 So we're going to pull that in and check that
1:22 and then this is left over from the MailChimp section the mailing list,
1:26 and then here we are going to need four pieces of information
1:28 that we would get from SES, Simple Email Service,
1:32 so you go on and go to your SES account, or you create an AWS account,
1:36 go to the SES section, turn that on, you are going to have to verify your domains
1:41 before SES will send from a domain on your behalf,
1:44 you have to verify that you control that domain,
1:47 so I had to go through this step to verify talkpython.fm
1:50 so that I could send email to the users and say this email is from firstname.lastname@example.org.
1:58 So you've got to do all those steps and then at the end,
2:01 you'll come up to a section where you get the username, password
2:05 and the server address and the port; so, we are going to have all that here,
2:08 that is going to be great, you'll just fill those in,
2:11 and let's go check the mode real quick,
2:14 so here you can see we are going to set the mode, we are going to grab this
2:18 and we are going to set it depending on whether that setting
2:21 is set to dev or not, really simple, right?
2:25 And then, down here, then here in the email,
2:28 we are not quite done with this section but we are going to come down here,
2:32 we'll get the settings, and we'll get those four values
2:34 and I also have this "unset", which is what is in GitHub right now,
2:39 and I'll just print a warning if I run it it'll say "warning, SMTP values are not set",
2:43 and so we want to make sure it just says
2:45 "Look, you are not going to really be able to send mail unless you go in
2:48 and tweak this and put in your actual settings."
2:51 It doesn't have to be SES, it's going to be any SMTP server
2:54 but the one we are using is SES.
2:57 Great, so now we are ready to add email capabilities,
3:00 let's go over here and add an email service.
3:03 So anytime we want to send email, we are just going to go back to this service
3:07 and we are going to do it here.
3:12 So let's create a class, it's going to mostly hold static information,
3:19 it's going to need to hold the username and the password,
3:23 the four things that you just saw, or five really,
3:25 including whether it's in debug mode so let me just create those really quickly.
3:30 So we want to have some way to set these values,
3:33 we don't really want to let them... expose them outside,
3:36 put the double underscore to sort of hide them,
3:38 so I want to have a method called global_init.
3:42 And it's going to pass in things lie the username, the password and so on.
3:45 And then we'll just store them.
3:47 Now, one thing we do need to do is this needs to be a number,
3:50 so we are going to need to convert it and what I have over here is not a number
3:54 so I am going to put zero, but just realize, zero is not the right answer,
3:58 there is three or four options, none of which is zero,
4:03 OK, so zero there because this is going to be a number,
4:05 so this will setup all of our emails here.
4:10 The next thing we need to do is import this
4:13 and call the setup function here, so let's do this.
4:21 And the other thing we need is we are going to need "global dev_mode",
4:25 to indicate whether it's in debug or not.
4:29 OK, so that will initialize the email service,
4:35 so then later when we want to call create an SMTP server client, send the email,
4:40 it's going to have everything it needs,
4:42 and we are just basically carrying those on from within the config file onto here.
4:47 We make sure the production.ini also has a numerical value here.
4:53 Great, let's run it, make sure everything works,
4:56 it looks like it is starting just fine, beautifully.
4:59 Now notice, you didn't see those warnings about your setup is not working
5:02 because I am using a separate config file that I am not including in the source code
5:07 because it has my own settings, we should actually be able to send mail.