使用 SMTP 邮件 推送消息。
(title: str, content: str)
| 677 | |
| 678 | |
| 679 | def smtp(title: str, content: str) -> None: |
| 680 | """ |
| 681 | 使用 SMTP 邮件 推送消息。 |
| 682 | """ |
| 683 | if ( |
| 684 | not push_config.get("SMTP_SERVER") |
| 685 | or not push_config.get("SMTP_SSL") |
| 686 | or not push_config.get("SMTP_EMAIL") |
| 687 | or not push_config.get("SMTP_PASSWORD") |
| 688 | or not push_config.get("SMTP_NAME") |
| 689 | ): |
| 690 | return |
| 691 | print("SMTP 邮件 服务启动") |
| 692 | |
| 693 | message = MIMEText(content, "plain", "utf-8") |
| 694 | message["From"] = formataddr( |
| 695 | ( |
| 696 | Header(push_config.get("SMTP_NAME"), "utf-8").encode(), |
| 697 | push_config.get("SMTP_EMAIL"), |
| 698 | ) |
| 699 | ) |
| 700 | message["To"] = formataddr( |
| 701 | ( |
| 702 | Header(push_config.get("SMTP_NAME"), "utf-8").encode(), |
| 703 | push_config.get("SMTP_EMAIL"), |
| 704 | ) |
| 705 | ) |
| 706 | message["Subject"] = Header(title, "utf-8") |
| 707 | |
| 708 | try: |
| 709 | smtp_server = ( |
| 710 | smtplib.SMTP_SSL(push_config.get("SMTP_SERVER")) |
| 711 | if push_config.get("SMTP_SSL") == "true" |
| 712 | else smtplib.SMTP(push_config.get("SMTP_SERVER")) |
| 713 | ) |
| 714 | smtp_server.login( |
| 715 | push_config.get("SMTP_EMAIL"), push_config.get("SMTP_PASSWORD") |
| 716 | ) |
| 717 | smtp_server.sendmail( |
| 718 | push_config.get("SMTP_EMAIL"), |
| 719 | push_config.get("SMTP_EMAIL"), |
| 720 | message.as_bytes(), |
| 721 | ) |
| 722 | smtp_server.close() |
| 723 | print("SMTP 邮件 推送成功!") |
| 724 | except Exception as e: |
| 725 | print(f"SMTP 邮件 推送失败!{e}") |
| 726 | |
| 727 | |
| 728 | def pushme(title: str, content: str) -> None: |