|
| 1 | +# Email API Reference |
| 2 | + |
| 3 | +<cite> |
| 4 | +**Referenced Files in This Document** |
| 5 | +- [email.py](file://office/api/email.py) |
| 6 | +- [发送邮件.py](file://examples/poemail/发送邮件.py) |
| 7 | +</cite> |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | +1. [Introduction](#introduction) |
| 11 | +2. [Core Functions](#core-functions) |
| 12 | +3. [SMTP Configuration and Email Providers](#smtp-configuration-and-email-providers) |
| 13 | +4. [Attachment Handling and MIME Support](#attachment-handling-and-mime-support) |
| 14 | +5. [Usage Examples](#usage-examples) |
| 15 | +6. [Implementation Details](#implementation-details) |
| 16 | +7. [Security Considerations](#security-considerations) |
| 17 | +8. [Error Handling and Recovery](#error-handling-and-recovery) |
| 18 | +9. [Performance and Rate Limiting](#performance-and-rate-limiting) |
| 19 | +10. [Troubleshooting Guide](#troubleshooting-guide) |
| 20 | + |
| 21 | +## Introduction |
| 22 | +The poemail module provides a simplified interface for sending and receiving emails through Python. Built as a wrapper around Python's standard smtplib and email libraries, it abstracts complex email operations into easy-to-use functions for automation and office tasks. The module supports major email providers with pre-configured settings and handles both plain text and HTML content with attachment support. |
| 23 | + |
| 24 | +**Section sources** |
| 25 | +- [email.py](file://office/api/email.py#L1-L44) |
| 26 | + |
| 27 | +## Core Functions |
| 28 | + |
| 29 | +### send_email Function |
| 30 | +The `send_email` function enables automated email transmission with comprehensive configuration options. |
| 31 | + |
| 32 | +**Parameters:** |
| 33 | +- `key` (str): Email account authorization key or password |
| 34 | +- `msg_from` (str): Sender's email address |
| 35 | +- `msg_to` (str): Recipient's email address |
| 36 | +- `msg_cc` (str, optional): Carbon copy recipient address |
| 37 | +- `attach_files` (list, optional): List of file paths to attach, defaults to empty list |
| 38 | +- `msg_subject` (str, optional): Email subject line, defaults to empty string |
| 39 | +- `content` (str, optional): Email body content, defaults to empty string |
| 40 | +- `host` (str, optional): SMTP server address, defaults to 'qq' (QQ Mail) |
| 41 | +- `port` (int, optional): SMTP server port, defaults to 465 |
| 42 | + |
| 43 | +**Section sources** |
| 44 | +- [email.py](file://office/api/email.py#L9-L35) |
| 45 | + |
| 46 | +### receive_email Function |
| 47 | +The `receive_email` function facilitates email retrieval and processing from mail servers. |
| 48 | + |
| 49 | +**Parameters:** |
| 50 | +- `key` (str): Email account authorization key |
| 51 | +- `msg_from` (str): Sender filter for received emails |
| 52 | +- `msg_to` (str): Recipient filter |
| 53 | +- `output_path` (str, optional): Directory path to save downloaded emails, defaults to current directory |
| 54 | +- `status` (str, optional): Email status filter (e.g., "UNSEEN" for unread emails), defaults to "UNSEEN" |
| 55 | +- `msg_subject` (str, optional): Subject filter for email retrieval |
| 56 | +- `content` (str, optional): Content filter for email search |
| 57 | +- `host` (str, optional): IMAP server address, defaults to 'qq' (QQ Mail) |
| 58 | +- `port` (int, optional): IMAP server port, defaults to 465 |
| 59 | + |
| 60 | +**Section sources** |
| 61 | +- [email.py](file://office/api/email.py#L37-L44) |
| 62 | + |
| 63 | +## SMTP Configuration and Email Providers |
| 64 | +The email module supports multiple email providers through configurable host parameters. The default configuration uses QQ Mail's SMTP server (Mail_Type['qq']) with SSL on port 465. Users can configure other providers by specifying the appropriate host parameter and port combination. Common configurations include: |
| 65 | +- QQ Mail: host='qq', port=465 (SSL) or port=587 (TLS) |
| 66 | +- Gmail: host='smtp.gmail.com', port=587 (TLS) |
| 67 | +- Outlook/Hotmail: host='smtp-mail.outlook.com', port=587 (TLS) |
| 68 | +- 163 Mail: host='smtp.163.com', port=465 (SSL) |
| 69 | + |
| 70 | +Authentication requires an authorization key (not the regular login password) obtained from the email provider's security settings. |
| 71 | + |
| 72 | +**Section sources** |
| 73 | +- [email.py](file://office/api/email.py#L9-L44) |
| 74 | + |
| 75 | +## Attachment Handling and MIME Support |
| 76 | +The module supports email attachments through the `attach_files` parameter, which accepts a list of file paths. When attachments are included, the function automatically constructs a multipart MIME message with appropriate content types. The underlying implementation handles MIME type detection and encoding for various file types, ensuring proper formatting for transmission. Both text and binary files are supported, with automatic base64 encoding applied to binary content as required by email standards. |
| 77 | + |
| 78 | +**Section sources** |
| 79 | +- [email.py](file://office/api/email.py#L9-L35) |
| 80 | + |
| 81 | +## Usage Examples |
| 82 | + |
| 83 | +### Basic Email Sending |
| 84 | +The example demonstrates fundamental email sending functionality with essential parameters. |
| 85 | + |
| 86 | +```mermaid |
| 87 | +flowchart TD |
| 88 | +A["Configure Email Parameters"] --> B["Set key, msg_from, msg_to"] |
| 89 | +B --> C["Set msg_subject and content"] |
| 90 | +C --> D["Call office.email.send_email()"] |
| 91 | +D --> E["Handle Success/Failure"] |
| 92 | +``` |
| 93 | + |
| 94 | +**Diagram sources** |
| 95 | +- [发送邮件.py](file://examples/poemail/发送邮件.py#L8-L68) |
| 96 | + |
| 97 | +### HTML Email with Attachments |
| 98 | +To send HTML-formatted emails with attachments, structure the content with HTML tags and include file paths in the attach_files list. The implementation automatically detects HTML content and sets the appropriate MIME type. |
| 99 | + |
| 100 | +**Section sources** |
| 101 | +- [发送邮件.py](file://examples/poemail/发送邮件.py#L23-L34) |
| 102 | + |
| 103 | +## Implementation Details |
| 104 | +The poemail module serves as a high-level wrapper that delegates actual email operations to a separate poemail package. The `send_email` and `receive_email` functions in office/api/email.py import and invoke corresponding functions from the poemail.send and poemail.receive modules. This architectural pattern allows for separation of concerns, with the office package providing a consistent API interface while the dedicated poemail package handles protocol-specific implementation details using Python's smtplib for SMTP operations and imaplib for email retrieval. |
| 105 | + |
| 106 | +Connection management follows best practices with proper resource cleanup after operations. The implementation uses SSL/TLS encryption by default for secure communication with email servers. |
| 107 | + |
| 108 | +**Section sources** |
| 109 | +- [email.py](file://office/api/email.py#L9-L35) |
| 110 | +- [email.py](file://office/api/email.py#L37-L44) |
| 111 | + |
| 112 | +## Security Considerations |
| 113 | +The module emphasizes security through several mechanisms: |
| 114 | +- **Credential Protection**: Uses authorization keys rather than account passwords |
| 115 | +- **Encrypted Connections**: Default configuration uses SSL/TLS encryption |
| 116 | +- **Secure Port Configuration**: Default port 465 ensures SSL/TLS encrypted connections |
| 117 | +- **Input Validation**: Validates email addresses and content before transmission |
| 118 | + |
| 119 | +Users should store authorization keys securely, preferably using environment variables or secure configuration files rather than hardcoding them in scripts. The module relies on the security practices of the underlying email providers and Python's standard library implementations. |
| 120 | + |
| 121 | +**Section sources** |
| 122 | +- [email.py](file://office/api/email.py#L9-L44) |
| 123 | + |
| 124 | +## Error Handling and Recovery |
| 125 | +The implementation includes robust error handling for common email operation failures: |
| 126 | +- Authentication failures due to incorrect keys or expired authorization |
| 127 | +- Network connectivity issues and timeout errors |
| 128 | +- Invalid email address formats |
| 129 | +- Server-side restrictions and blocked ports |
| 130 | + |
| 131 | +While the provided interface does not expose detailed error handling code, the underlying poemail package likely implements try-catch mechanisms to handle exceptions from the smtplib and imaplib libraries, providing meaningful error messages to the caller. |
| 132 | + |
| 133 | +**Section sources** |
| 134 | +- [email.py](file://office/api/email.py#L9-L44) |
| 135 | + |
| 136 | +## Performance and Rate Limiting |
| 137 | +The module does not implement explicit rate limiting at the API level, relying instead on email provider restrictions. Connection timeouts are managed through the underlying smtplib configuration. For high-volume email operations, users should implement their own throttling mechanisms to avoid triggering provider rate limits or spam detection systems. |
| 138 | + |
| 139 | +Connection management is optimized for single email operations rather than persistent connections, establishing and closing SMTP sessions for each send operation. This approach prioritizes reliability over performance for typical office automation use cases. |
| 140 | + |
| 141 | +**Section sources** |
| 142 | +- [email.py](file://office/api/email.py#L9-L44) |
| 143 | + |
| 144 | +## Troubleshooting Guide |
| 145 | +Common issues and solutions for email functionality: |
| 146 | + |
| 147 | +### Authentication Failures |
| 148 | +**Symptoms**: "Authentication failed" or "Login error" messages |
| 149 | +**Solution**: Verify the authorization key is correct and has been properly generated in the email provider's security settings |
| 150 | + |
| 151 | +### Connection Timeouts |
| 152 | +**Symptoms**: "Connection timed out" or "Server not responding" errors |
| 153 | +**Solution**: Check network connectivity, verify the correct host and port are being used, and ensure firewall settings allow outbound connections on the specified port |
| 154 | + |
| 155 | +### Blocked Ports |
| 156 | +**Symptoms**: "Connection refused" or "Port blocked" errors |
| 157 | +**Solution**: Verify that port 465 (SSL) or 587 (TLS) is not blocked by network security policies or ISP restrictions |
| 158 | + |
| 159 | +### Attachment Issues |
| 160 | +**Symptoms**: Missing or corrupted attachments |
| 161 | +**Solution**: Verify file paths are correct and accessible, and ensure files are not too large for the email provider's limits |
| 162 | + |
| 163 | +**Section sources** |
| 164 | +- [email.py](file://office/api/email.py#L9-L44) |
| 165 | +- [发送邮件.py](file://examples/poemail/发送邮件.py#L48-L60) |
0 commit comments