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

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


Talk Python's Mastodon Michael Kennedy's Mastodon