#100DaysOfWeb in Python Transcripts
Chapter: Days 65-68: Heroku and Python Platform-as-a-Service
Lecture: Automate sending emails with Heroku scheduler

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's take a look at how you send an email using the Heroku Scheduler. If I look in this folder here I have emailer.py.
0:09 This is a script I've just quickly banged together using SMTPlib. We'll look at that really quickly.
0:16 I'll run you through it, I won't explain this too much because this is stuff we've covered before in previous courses and multiple articles
0:23 but the gist of it is we're using Gmail. We're using the SMTP server from Gmail. We're using smtplib which is imported
0:32 from the standard library, we then start TLS and we login into our SMTP server. Now this is the tricky part that you will
0:42 customize for yourself, hopefully you have a Gmail so you can follow along. You need to get your email address.
0:48 That is what goes here in the first section of this command, and then you need your application password, this is something
0:56 you can get from your Gmail account when you go on to the Developers section. So grab that and pop that in here.
1:03 And then in your actual sendmail section this is where you create your message to send. And in this case, the first field here
1:12 is for your actual email that you're sending from. The second field is for the email you're sending to. In this case pybitesblog@gmail.com
1:22 and finally it is your actual subject plus message. So we are changing the subject to subject. Hello from Heroku, we have a new line to say
1:32 that we're now dropping down to the message. Test email from Heroku, cheers, Julian. And then we close out SMTP server connection with a quit.
1:42 And that's it, this is a really simple script to send an email, and what we now have to do is plug this into Heroku
1:51 and use the Scheduler to automatically run the script. And with my data in there we can actually test this script before we push the code up to Heroku.
2:02 Obviously, we can test it running just Python emailer.py but what we want to do is we want to simulate this actually being run from Heroku
2:12 and this is the equivalent of the command that Heroku Scheduler will run when we enter it in there. We can actually type in Heroku Run
2:21 because we're telling Heroku to run something and what we want it to do is we want it to run the Python command that we were going to execute locally.
2:29 So again, if we were executing Python emailer.py that would be running it locally. Now we're telling Heroku to run the actual script from its end
2:39 but obviously this is not going to run because this script does not exist on the Heroku server end yet, so let's
2:47 push that up now to git add, git commit Emailer script addition, and we do a get push Heroku master and the code goes up.
3:11 And with the code pushed up, now we can run accurate commands so Heroku run Python emailer.py and now see how it's telling us that
3:25 we're actually running that on the pybites100days dyno so let's span up our dyno, which means it's counting toward our free dyno hours.
3:34 That's why we had to enter our credit card. And let's run the script, now if I go to my actual PyBites email I will have an email from myself saying
3:45 hey, hello from Heroku, and there you go. Test email from Heroku, cheers, Julian. Kind of weird getting an email from myself.
3:53 So we can close that off and pop back in here and now we know that by running that Python emailer.py script or that line
4:03 that shell command, that we are actually going to send the email, and we know the script works so now if we pop back into Heroku Scheduler
4:11 the web GUI, we can click Add New Job and we simply type in that same command. Emailer.py, the reason we're not
4:20 typing in Heroku Run is because that was something required for the command line and by running it here, it is already assumed that Heroku Run is being
4:31 run in the background, now, dyno Size we're going to keep that as free. We don't want to charge you any money yet
4:37 and frequency, this is where you set up how often you want it to run, as I said. You can have it run every ten minutes
4:44 and it tells you when it's going to be run next. UTC Time, so at 11:35 UTC Time March 18th this will send me another email.
4:55 Now obviously in this instance I'm going to need to turn this off because I don't want to get an email from myself every ten minutes, but
5:04 you can get the gist of it, you can see where this is going, right, so this is where you do it, you set it up and you click on save, and then
5:12 you'll now see this little job located here. Now when this runs again, so this now has changed to 11:42, and when this
5:22 runs again, we will see it in the last run field here, and then we'll see another next due line here, and we let
5:32 a little bit of time pass, we refresh the page now and there you go, last run March 18th, 11:42 and the next due is March 18th, 11:52 UTC
5:45 and again if I head back to my email there you go, one from 10 minutes ago when we first sent it and one from one minute ago.
5:53 You can see the time difference there. So that is Heroku Scheduler, you can pretty much automate anything, just
6:00 anything that is in a script send it out and there you go, you can make log files, you can write to databases, you can send text messages, send emails.
6:11 Anything you can really think of that can be spun up in a quick dyno achieved and sent and ran, whatever you want to
6:18 call it, and then complete, close off. So single run scripts, and that's Heroku Scheduler. Let's take a look at another one.


Talk Python's Mastodon Michael Kennedy's Mastodon