Python for Entrepreneurs Transcripts
Chapter: Sending and receiving email
Lecture: Concepts: Sending basic email
0:01 Let's review what we had to do to send email out of our web application.
0:05 Not mailing list, not newsletters, but one of individual emails,
0:09 based on various actions people take in our site,
0:12 like for example signing up or asking for a password reset
0:16 or purchasing something, getting them receipts, things like that.
0:20 So we use the mailer package, and we are going to create a mailer object,
0:25 which is basically an SMTP client.
0:28 So you can see that we had set up a global initialize function
0:31 that pulled things like the server port, username and password
0:34 out of our configuration files and just stashed them in these variables.
0:39 Then when we want to get one of these mailers,
0:42 we just go and pass all the various credentials across
0:45 and we say use_tls is True, so use encryption,
0:48 this is very good, now we are going to get this back.
0:51 Once we have one of these SMTP clients, these mailer objects,
0:54 we can actually use it to send email.
0:58 So here is our base send_message method.
1:02 Remember, we are going to create other methods that customize,
1:05 that basically build up the body, the subject,
1:07 things like that and then call this function.
1:09 So it's the only one that is really creating the SMTP sender
1:13 and actually creating the message itself.
1:15 So we call our function you just looked at, create SMTP sender, to get the SMTP client;
1:20 then we want to create a message object, set the from address,
1:23 the to address, the character set, the subject,
1:26 at the html_body and we are also going to set the body
1:30 so that there is an alternate view if somebody has
1:34 "show HTML" turned off in their mail client,
1:36 they will still see something and we use the HTML to text package
1:40 to actually turn that into markdown in a real simple way.
1:43 So that will give them a decent representation of some kind of body there
1:48 and then we also added a debug mode to our configuration files,
1:51 to our initialization and pass that on to the email service
1:55 so that we don't send email while we are fiddling and developing the app,
1:59 we only send email in production.
2:01 Obviously, when you are testing the email, you want to turn that off,
2:04 so that you don't mail your clients or your users
2:06 but only mail yourself or people on your team,
2:09 in general, you should probably not let it send email in debug mode or in dev mode.
2:15 And we did this in a "try..except" block, because sending email,
2:18 like many network things, can go wrong in many ways,
2:21 so here, in this case we are just printing out a basic message of what went wrong,
2:25 it couldn't send an email to this email address because of whatever the exception contains,
2:30 and then notice we are re-raising the exception,
2:32 because we don't want to eat it, we just want to log it and then raise the exception again.
2:37 I didn't do that in my demo, you probably want to add that to your actual code.
2:40 In my real websites, of course the exceptions don't get eaten,
2:45 they get dealt with appropriately, whatever you are doing up above that.
2:50 OK, so let's briefly look at the packages,
2:53 here on pypi.org we have the mailer package,
2:56 "pip install mailer", you saw that we just let PyCharm handle all of that,
2:59 this is a really nice package building on top of Python's ability to send mail
3:03 and it just makes it a little bit nicer.
3:06 We also used html2text, because we want a text alternate version
3:10 if you are not going to be able to see the HTML, what does the plain text version look like,
3:15 but we can use this html2text to get that.
3:18 You could even let your users decide OK,
3:21 I want to receive text messages or I want to receive HTML messages
3:24 and then you could just only set one or the other in the body in your mail message.