Python for Entrepreneurs Transcripts
Chapter: Sending and receiving email
Lecture: Demo: Sending basic HTML email
0:01 Our email service is all initialized, it has everything it needs in terms of credentials and connection info to get started.
0:08 What it doesn't have is the ability to create an SMTP client or send an email so let's work on those things.
0:13 So first thing we are going to do is we are going to add another static method
0:16 and this is just going to be used locally, I don't think it will be used outside, so I'll call this create SMTP server,
0:23 right now I am going to return None, now we could use Python't built-in email system but I want to use something different,
0:33 I am going to use a package called mailer, so if we come over here, we can go and check out the documentation for this character,
0:42 so mailer is just a simple package that makes it easier for us to send email, you can see that we can setup an HTML version, a plain text version
0:51 if the client that receives the message doesn't want to see HTML, they can actually display just the text and handle attachments, things like that.
0:59 So we are going to use this mailer package, so we are going to start just letting PyCharm drive for us, so we'll say import mailer and PyCharm says
1:08 you know, I don't really know what mailer is but I can install it for you and we'll let it. OK that was great, now it's going to say oh first of all,
1:20 it's going to say add requirement mailer to setup, so when you install it it will automatically install mailer rather than crash,
1:28 you definitely want to do that, and now the last thing is it's going to say look, we are not using that. Alright so what we want to do,
1:36 is come down here and create an SMTP client like so, so we are just going to create an instance to this mailer class,
1:49 and we just need to set the host, the port, whether or not to use TLS, is it encrypted or not, so here I have just copied those values in,
1:57 set the host port, username, password and to use encryption, and we are just going to return that.
2:02 Anywhere we need to create one of these abilities, to send the message, we are going to call this function to get it started.
2:07 The next thing we want to do is we want to be able to send a basic email,
2:11 that is going to be the base method that the rest of this class is going to use, so we are going to add methods like send_password_reset,
2:20 send_new_account, send_purchase_email, all of these things and what those are going to do is
2:25 they are going to basically build up the information that goes into the email and then they are going to feed it off to this method
2:31 which will actually do the sending. So for this one we don't need too much information, we are going to need a to_address, a subject and an HTML_body.
2:43 Alright, so the first thing we want to do is we want to get access to this SMTP server
2:50 and there is lots of things that can go wrong, so let's start here. So now we have our server, let's just add the except block,
3:00 we'll do better than that in a minute, the next thing we want to do is we want to create a message we want to send so we'll say "From=",
3:11 now let's just use a standard "from" address, so let's go up here and do this. Now in order for this to work, because the credentials at SES
3:23 I have only approve sending out of talkpython.fm I am going to use a talkPython address.
3:30 So it will be demo and this will be Talk Python Demo, something to this effect, right,
3:35 you will need to change this to be whatever your domain that you verified with SES is. So let's go down here,
3:45 let's make sure we set the character set to utf-8, that's pretty standard, like so, and then let's go ahead and set the Subject to be the subject
3:56 and we'll set the HTML to be the HTML_body. Now it gets a little complicated, I have some HTML,
4:03 how do I set the body to some text version of the HTML body, what do I do here,
4:10 well, it might sound complicated, but it turns out it's not complicated, we are going to use a nice little package.
4:17 So if we come over here, to pypi.io we can look for HTML2text, so this will turn HTML into markdown structured text
4:27 so for example if we have like bold in our HTML it will just go ** and put the words in there, standard markdown.
4:36 So that actually seems like a pretty decent compromise for well if you want to look at HTML, we'll give you kind of markdown that you can read.
4:45 So we are going to use HTML2text. And again, PyCharm can take care of everything for us.
4:58 And with that handy package, it is super easy to convert this to text. We just give it the HTML that we want to convert, and boom, we are done.
5:11 So now we've created our message, we've created our sender, it's up to us to just send it.
5:18 Easy enough, right, no problem at all, but maybe we want to check, remember we have that debug mode, we'll say "if it's in the debug mode",
5:30 so let's say "if it's not in debug mode, then we want to send it". Otherwise, let's do this.
5:38 Let's print this out, just so we know that something went wrong here, and maybe just so that you can see things happen,
5:48 let's do a print("Sending message (live)!"), something like that and we'll do an "else", print("Skipping send, email is in dev mode").
6:01 Alright, the next thing for us to do is to actually test this out by sending some mail.