Operations 5 min read

Automate Weather Forecast Emails with Python, Jinja2, and Crontab

This tutorial walks through using Python's requests library to fetch weather data from the Heweather API, rendering it with a Jinja2 HTML template, constructing and sending the email via smtplib, and scheduling the whole process with crontab, while highlighting common pitfalls.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Weather Forecast Emails with Python, Jinja2, and Crontab

It's been a while since I wrote a blog, so I record the Python script that sends a QQ email on a schedule.

Use the requests library to call the Heweather weather‑forecast API and retrieve the data.

Create an HTML template with jinja2 to display the weather information.

Build the email using Python's email module and send it through smtplib .

Schedule the script with crontab so it runs automatically each night.

Heweather API

The API itself is straightforward: use requests to fetch the JSON, then extract the fields you need as described in the official documentation.

HTML Template

Write a Jinja2 template file in the same directory as the script.

After the template is ready, import it in the script and pass the weather data to render the final HTML.

Email Construction and Sending

Note:

1. Enable SMTP service in your QQ mailbox (usually port 465).

2. When constructing the email, the recipient address must be a string; when sending, it must be a list.

Crontab Scheduling

Typical crontab entry to run the script at 22:00 each night:

Common pitfalls:

Specify the full path to python3 (check with which python3).

If the script contains Chinese characters, set the environment variable, e.g., export LANG="zh_CN.UTF-8".

Avoid using if __name__ == '__main__' unless you have classes that need it.

Example crontab entry (shown as an image):

Email Preview

Source Code

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAPIJinja2crontabemail automationHeweather
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.