Loading video player…

Sending Plaintext Emails With Python

After learning how to establish a secure connection to a SMTP server, it’s time to send an actual email! Therefore, this video shows you how to create an email and send it via Python to another mail account using built-in functionalities.

00:00 Now that you know how to create secure connections to a mail server, it’s time to send an actual email. We’re going to change this code to create the encrypted connection from the start, so the first thing you need to do is change this port back to 465, and then go ahead and delete all these try and except blocks here.

00:20 You want to re-add that with statement, so with smtplib.SMTP_SSL(), pass in the smtp_server,

00:33 the port, set the context=context,

00:40 then call this server.

00:46 Like before, let me close this project manager out. Log into the server using server.login(), pass in the sender and the password.

00:59 And once you’re logged in, here is where you would actually send the email. At this point, you need to add a couple more parameters. You need to know who you’re sending the email to, so you can just do something like receiver.

01:15 And I have another throwaway email account—I probably have too many of these but they’re all free, so here we are. And then you need the actual message that you want to send as well.

01:30 I’m just going to put in a multiline string here,

01:34 call the """Subject: Hi There!""", put a blank line in, and then put the actual message. """This message was sent from Python!""".

01:49 Close that string. And now you can actually go and send the email! So down here, after you’ve logged into the server, take your server and call .sendmail(), just like that.

02:04 Pass in the sender, the receiver, and the message. Okay! This should be all set! Open up a terminal, call the script,

02:20 enter your password.

02:28 I don’t see any error messages, so let’s hop over to the inboxes and see if they actually sent. So let me hop over to Gmail, and this is the other inbox that I’ve got. And look at that!

02:41 There’s the email from my other email account. If you don’t see it in your inbox, try refreshing. I’d say about 50/50 of the time they’ll end up in my spam, so don’t get too frustrated if you don’t see it there. Now, there is one thing to note with this email. If you click on to, you can see you’ve got your TLS encryption here, but it’s from the other email account, but it actually wasn’t sent to this email account.

03:09 So, if we go back to the email account that sent the email and head to Sentokay, here it is. Let’s open this up. And if you look, the receiver email was actually blind carbon copied on here and it wasn’t sent directly to them.

03:28 If you want to send an email directly to somebody, you have to make a couple changes to the message itself. This is due to the SMTP protocol. If you’re familiar with HTTP requests, you may realize that there are a series of lines where each line means something.

03:44 SMTP is pretty similar. Just go ahead and add a From: and a To: line and then do some string formatting to add in the sender and the receiver, just like that. All right. That’s saved.

04:02 Let’s try it again. Enter in the password.

04:13 We don’t see any errors, so hop over to the inboxes. Now if I go back to Sent, you can see that this time it was actually sent to that email. And in the actual inbox, here’s the second one, and it was sent to the email.

04:35 So, the formatting of your message can play into how it’s actually sent, so if you’re running into issues don’t be afraid to look around and see if the SMTP protocol is playing into how your messages are going out. All right!

04:49 Now that you can send plain text emails, in the next video you’re going to learn how to add HTML formatting so you can make some prettier emails to send. Thanks for watching.

Avatar image for mhabtab

mhabtab on Dec. 6, 2019

When I try above code I get the error:

raise SMTPServerDisconnected(‘please run connect() first’)

Avatar image for Joe Tatusko

Joe Tatusko RP Team on Dec. 9, 2019

Hi mhabtab, I’ve looked around and see a couple different potential causes for that error (but they’re all somewhat vague unfortunately), have you configured your gmail account to allow for programmatic access?

Avatar image for gracetan

gracetan on April 22, 2020

Hi Joe, I used smtp protocol to try to see the From and To from my emails, but i still do not see them, but i can see it at the bcc. Do you know why it looks like this?

Avatar image for millerdane75

millerdane75 on June 5, 2021

HI Joe,

My emails are being sent however they are still being bcc and not sent to the to. I changed my code as you indicated but still not luck.

Code: message = “”“\ From: {} To: {} Subject: {}

This message was sent from python.

”“”.format(sender,receiver,subject)

Avatar image for millerdane75

millerdane75 on June 5, 2021

Please disregard my last email. As I moved to the next video and I used the email.mime.text approach it worked.

Thanks much

Become a Member to join the conversation.