Python for Entrepreneurs Transcripts
Chapter: Sending and receiving email
Lecture: Demo: Setup for sending email

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


Talk Python's Mastodon Michael Kennedy's Mastodon