Using Python to email NASA APOD API data to a custom recipient [#1 NASA APOD Series]

I’m doing a mini-project as a learning experience. The first step here involved two sepearate functionalities that I’ve now combined and will explain here. Firstly, I had to write a few lines of code to actually use the API and save the image it returns to the current directory. This was really easy. I then had to learn how to use smtplib to send emails in python. I used this article on Real Python to do so. This post on Stack Overflow was also helpful in instructing me about attaching image files to an email. You can find the entire code in here, on my GitHub. I’ve been having some issues with WordPress plugins, so I’ve just attached screenshots of my code for this post.

right, so the first thing we’ll be looking at is accessing the NASA APOD API and storing the latest NASA Astronomy Picture of the Day Image in the current directory.

I firstly import the necessary modules. After that, we’ll access the API with our key. The url contains the link given in the offficial documentation for accessing latest image. The API returns a json object with keys pointing to a description of the image, a hd url, and a nominal quality url. I’ve used the urllib module to fetch and store the image to our current repo (stores to which repo we are in or we have changed to with os). Now comes the hard part.

You can take a look at all the modules I’ve imported in the GitHub link i’ve posted above.

Above, we start our smtp_server and initialize our port. I’ve used 587 because I’m using the TLS method, I think you’ll need to use 465 if you use the other method. I’ll be using mehulpy@gmail.com to send this email. This is a throwaway account I created with all security features turned off. I’m mailing it to mehuljangir42@gmail.com, which is one of the emails I use. We then input the password for the email we will be sending it from. Then, we’ll our message will be set to an instance of the MIMEMultipart objective with “alternative” as our input parameter. We then key in some values, like the email to use, the email to mail it to, and the Subject of our email. I’ve then specified html and plaintext messages for the email to contain.

I’ve attached both html and plaintext messages to our email. The procedure for attaching a .png file as an email attachmetn is very simple. Using the inbuilt open subroutine in python, we read the image. Then, we create an instance of the MIMEImage object where we key in the img_data we just read and input the path of our image file. os.path.basename returns your current path, kinda like the pythonic way of writing pwd.

A SSL context is created after that. Check out this Stack Overflow post to learn more about what an SSL context is. We then move into a try except condition, so that we can catch any errors that come our way. A server is established, and server.ehlo() is basically the equivalent of waving hi to your friend across the room. We then start tls, which is a method of encryption for safe and secure communication across the internet. Finally, as a method of the server instance, I send the email to the recipient. The finally block quits the server regardless of whether or not the email was sent and whether or not an error was thrown.

I’ll now create a webpage where you can input your email, and a python script sends the current NASA APOD image to your email. So stay hooked for #2!

Also, FYI, here’s what the email looks like: