In this video, learn how to use Gmail as your personal email server. First, set up a new Gmail account and configure the required settings. Then, email your HTML weather data report using Gmail.
- [Instructor] Next we will be emailing the HTML via Gmail, and in order to use Gmail as our emailing server, we have to follow a few steps to set it up. And that's what we're going to be doing now. We'll be importing the smtplib from Python, and we'll call the smtp message and we're passing in the URL smtp.gmail.com and the port is 587.
But this by itself as our emailing server will not work unless we allow access for less secure access within Gmail and also we have to have a Gmail account and most of the time we have to be logged into the account on the web. It is a good idea to create a new Gmail account and when we then allow access for less secure apps, this setting will only affect that particular Gmail account, it won't affect the other Gmail accounts you might have.
So there are two URLs where we can set that. And most of them are not quite intuitive, so we're going to look at them next. We have to be logged into our Gmail account and then we can copy and past this URL into the browser. And this brings us to this page, less secure apps. Now this page is specific to the Gmail account we are logged in as, so for this account, it's off. So, in this case using our Python code, using the Gmail as a server for our Python code for emailing will work because this setting is off, allows less secure apps.
Another way to get there is by going to my account and then under Sign-in and security, there's connected apps and sites and down here when we scroll down a little bit, allow less secure apps is on so that is the same setting we just get that through a different pop. So now that we have setup Gmail as our emailing server, we can look at the code. So we are importing smtplib and then MIMEText and also in order to login we have to use a Gmail account and its password.
So in order to protect the password a little bit, what we've done here is as what we've done before, put the password into a separate module and we are importing that string from that module and then we use it down here so you can't really see what my password is. Down here in the self testing section, we're doing the same thing, we're getting the weather data, then we're creating the HTML file and then we are sending it, so we are passing in the email file that has been created.
And we are using the send Gmail function which is up here so it expects the file. And we open the msg file, s and HTML file. So that's important so we do a read binary and then from the MIMEText, do message read. But we also have to pass in HTML twice. Otherwise you get an error, the default is plain text so it wouldn't work for HTML. So it has to be read binary and twice HTML down here.
Then, type of msg is an MIMEText, it's not a string, this is a type, so because of that, we can add to this type, we can a subject. Which is what will appear in the email as the subject. And then we give it the from email address and to. And here I use my same email address just for purposes to show it here. And I'll also put in a fake email address. This doesn't exist another@Gmail.com does not exist.
But the servers are complaining about it. It's not checking if that's existing, it just happily sends it to this address. And well maybe there is something, another@Gmail.com. So after we created the server, we called these methods, ehlo, I think stands for Extended Hello. Then the starttls, ensures that our message will be encrypted, so tls stands for Transport Layer Security and then we have to call the ehlo again and these three lines of code will encrypt our messages.
Now, we have to login with our user Gmail account and it's password and then we can send a message. And after we send a message, we close the server. So when we run it, we will be executing our self-testing section here and we are looking for the test email file which we will be creating as before. So we run it, it's running, we can see that here. Now it's done, no more red light here, it's done.
So when we now go to the Gmail account, we can see we got the message, Hourly Weather, with a daytime string and this is the data. So we got time, date, we got the icon and for Los Angeles, we got our information.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Running Python programs within Eclipse
- Setting breakpoints
- Using the PyDev debugger
- Using design patterns
- Creating a GUI application with Tkinter
- Writing a Windows scheduling service