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