mirror of
https://github.com/mskatoni/ni-mail.git
synced 2026-09-05 15:37:09 +08:00
feat(v1.1.0): add send mail support via Graph API and SMTP
This commit is contained in:
@@ -420,3 +420,32 @@ def delete_emails_graph(
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def send_mail_graph(access_token: str, to_email: str, subject: str, body_text: str, proxy_url: str = None) -> bool:
|
||||
"""使用 Microsoft Graph API 发送邮件"""
|
||||
url = "https://graph.microsoft.com/v1.0/me/sendMail"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"message": {
|
||||
"subject": subject,
|
||||
"body": {
|
||||
"contentType": "Text",
|
||||
"content": body_text
|
||||
},
|
||||
"toRecipients": [
|
||||
{
|
||||
"emailAddress": {
|
||||
"address": to_email
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"saveToSentItems": "true"
|
||||
}
|
||||
proxies = build_proxies(proxy_url) if "build_proxies" in globals() else ({"http": proxy_url, "https": proxy_url} if proxy_url else None)
|
||||
res = requests.post(url, json=payload, headers=headers, proxies=proxies, timeout=30)
|
||||
return res.status_code in (200, 202)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
|
||||
def send_mail_smtp(host: str, port: int, username: str, password: str, to_email: str, subject: str, body_text: str, use_tls: bool = True, use_ssl: bool = False) -> bool:
|
||||
"""使用 SMTP 协议发送邮件"""
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = username
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body_text)
|
||||
|
||||
if use_ssl:
|
||||
with smtplib.SMTP_SSL(host, port, timeout=20) as server:
|
||||
server.login(username, password)
|
||||
server.send_message(msg)
|
||||
else:
|
||||
with smtplib.SMTP(host, port, timeout=20) as server:
|
||||
if use_tls:
|
||||
server.starttls()
|
||||
server.login(username, password)
|
||||
server.send_message(msg)
|
||||
return True
|
||||
Reference in New Issue
Block a user