Python for Entrepreneurs Transcripts
Chapter: Sending and receiving email
Lecture: Demo: Sending basic HTML email

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