Build a Serverless Python Email Scheduler on Alibaba Cloud Function Compute
This tutorial explains the fundamentals of Serverless architecture, outlines its BaaS and FaaS components, presents three typical use cases, and provides a step‑by‑step guide—including Python code and deployment instructions—to create an automated daily email service on Alibaba Cloud Serverless.
Introduction to Serverless
Serverless ("无服务器") does not mean the absence of servers; it means developers no longer need to manage server infrastructure directly, allowing them to focus on product code while compute resources are offered as a service.
Serverless Architecture
The architecture consists of two layers:
BaaS (Backend as a Service)
FaaS (Function as a Service)
BaaS (Backend as a Service)
Provides ready‑made services such as object storage, cloud databases, API gateways, and message push, all hosted in the cloud.
FaaS (Function as a Service)
Abstracts compute capabilities so that functions can respond to events without any server management.
Three Typical Serverless Scenarios
Event‑triggered execution : Functions run only when specific events occur.
Burst traffic handling : Automatic scaling on sudden traffic spikes saves resources and balances load.
Big‑data processing : Upload core code to a function and process large data sets without provisioning servers.
Advantages of Serverless
Compared with traditional architectures, Serverless eliminates the need to provision or upgrade machines, reduces operational complexity, enables pay‑as‑you‑go pricing, and shortens development cycles through automatic scaling and rapid deployment.
Python Example: Automatic Email Sender
The following Python script fetches weather information and a random love quote, then sends an email using yagmail. It is scheduled to run daily via the schedule library.
import requests
import yagmail # email sending
import schedule # scheduling
from bs4 import BeautifulSoup
import re
ran = 0
url = 'https://tianqi.2345.com/cixian1d/70177.htm' # weather URL
loveurl = 'https://www.guaze.com/juzi/23389.html' # love quote URL
def email():
global ran
web = requests.get(url)
page = BeautifulSoup(web.text, "html.parser")
weather = page.find("div", class_="real-today")
web2 = requests.get(loveurl)
web2.encoding = 'gb2312'
page2 = BeautifulSoup(web2.text, "html.parser")
div = page2.find('div', class_="content")
div = str(div.text)
grep = re.compile(r"\d+、(.*)")
content = grep.findall(div)
yag = yagmail.SMTP(host='smtp.qq.com', user='[email protected]', password='xhaztrwpjffpbdhh', smtp_ssl=True)
mail_content = [weather.text, "每日情话:", content[ran], yagmail.inline(r"/.love.jpg")]
yag.send(to=['[email protected]'], subject='早鸭', contents=mail_content)
print("发送完成")
ran += 1
schedule.every().day.at("05:21").do(email)
while True:
schedule.run_pending()Deploying to Alibaba Cloud Serverless (Function Compute)
Log in to the Alibaba Cloud homepage.
Navigate: Products → Elastic Compute → Serverless → Function Compute .
Enter the console, go to Service & Functions → Create Function .
Upload the project folder containing the Python script and save.
Configure the function entry point (e.g., handler.main).
Add a trigger: Trigger Management → Create Trigger → Timer Trigger → set name and schedule (e.g., daily at 05:21).
Install required dependencies in the function directory:
pip3 install yagmail -t .
pip3 install schedule -t .
pip3 install bs4 -t .Save and click the top‑right Save & Deploy button.
Final Result
After clicking Test Function and checking the real‑time logs, the function sends an email containing the current weather and a love quote.
Conclusion
This hands‑on project deepens understanding of Serverless concepts, demonstrates how to combine Python, web‑scraping, and cloud functions, and showcases the rapid development and cost‑efficiency benefits of the Serverless model.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
