title: Python automated communication: sending emails and text messages description: Master the technology of using Python to automatically send emails (including attachments and HTML formatting) and integrate third-party SMS gateways. Realize a full-link automated closed-loop from document generation to message notification.
In the first two articles, we used Daoman Python AI to achieve one-click generation of Excel, Word, and PDF - but if the files lie on the server and no one collects them, it is equivalent to wasting work. Today, let’s get through the last mile of business automation: use Python to send emails and text messages, and completely weld the link “document output → message access”.
1. Send email: Use SMTP to directly talk to the mail server
You don't have to open a browser to send an email. Essentially, the program simulates an email client and uses SMTP (Simple Mail Transfer Protocol) to shake hands, encrypt, and deliver with the NetEase/QQ/enterprise mailbox server. Python built-insmtplibThese underlying details have been encapsulated for us.
1.1 Pre-security preparation: get the exclusive "authorization code"
Directly using the login password to log in to the mailbox in a third-party program is too risky. Now mainstream mailboxes (126, 163, QQ, Gmail, etc.) require the use of authorization code:
- Log in to the email web version
- Enter "Settings → Account → POP3/IMAP/SMTP/Exchange"
- Enable POP3/SMTP service
- Follow the prompts to bind your phone, verify the text message, and get a string of about 16-digit authorization codes (only displayed once, be sure to save them)
💡 The methods for obtaining authorization codes for corporate email are similar. Just contact IT or check internal documents.
1.2 The first email: Get started quickly with plain text emails
The content of the email is provided byemail.mimeIt is assembled from a series of modules. Use plain text email directlyMIMEText; If you want to add attachments, HTML text or images later, you need to use a "container"MIMEMultipart。
1.3 Upgraded version: Email with attachments + HTML formatting
Plain text is a bit dry, we can add HTML to improve the visual effect (tables, bold, company logo placeholder, etc.), and at the same time send the AI-generated Excel / PDF / Word as an attachment.
Core logic of attachment
Attachments are essentially binary files, and SMTP can only transmit text, so Base64 needs to be used to encode the binary into a character stream. At the same time, the Chinese file name may be garbled, so you need to useemail.headerMatching methods.
2. Send SMS: Find a third-party gateway to act as a "middleman"
Unlike email, which has a unified SMTP protocol, SMS does not have a universal programming interface - we can't build our own base station. Therefore, you need to use compliant third-party SMS service providers (such as Alibaba Cloud, Tencent Cloud, Screwcap, etc.) to send SMS messages through the HTTP API provided by them.
2.1 Standard Compliance Process for SMS Sending
The state has very strict control over the SMS business. Sending marketing SMS messages without a signature/without permission is a violation**. The formal process must go through the following steps:
- Register service provider account - Recommend big companies such as Alibaba Cloud and Tencent Cloud, with good compliance and in-place technical support
- Real-name authentication—Both companies or individuals need to submit information
- Apply for SMS signature - for example
【道满科技】, will appear at the beginning or end of the text message, and the service provider’s review standards are clear - Apply SMS Template - for example
您的验证码是${code},请于5分钟内输入。请勿泄露给他人!, the variable uses the placeholder specified by the service provider - Get API Key/Secret - equivalent to the credentials for calling the interface
2.2 Practical code (taking Alibaba Cloud SMS service as an example)
APIs from major manufacturers usually provide official SDKs, but here we use the generalrequestsLibrary demo to facilitate migration to other service providers.
3. Pitfall avoidance guide and safety suggestions
-
🔐 Safety first Authorization codes and API Key/Secret should never be hard-coded in the code (especially do not upload to a public repository). Recommended
python-dotenvRead environment variables, or use the cloud service provider's key management service (KMS). -
📧 Tips for anti-spam email
- Avoid sending identical emails in batches and add personalized content (such as a title at the beginning)
- Try to use business email for sending, as free email is more likely to be marked as spam.
- Attachments should be controlled within 10 MB. For large files, it is recommended to use cloud disk links instead.
- 📱 SMS Compliance Red Line
- Personal real-name users can only send verification codes and notification text messages, and are not allowed to send marketing text messages.
- Marketing text messages must carry the words "Unsubscribe back to T"
- Be sure to configure IP whitelist and call frequency limit to prevent the interface from being stolen
After mastering the automatic sending of emails and text messages, combined with the automatic generation of documents in the previous two articles, you can already use Daoman Python AI to create a complete "weekly/monthly report automatic generation + automatic notification" system. Go and give it a try~

