用 Python 写了个小工具:轮询收件箱,如果收到的邮件满足某个条件,则向 IFTTT 发送触发邮件。
目前运行在树莓派上,但是办公室有时候会断电。现在想找个可靠又低成本的服务来运行这个代码。我听有些服务器会疯掉邮件发送端口。想请问各位老哥,哪家服务比较好呢?
import time
import email
import imaplib
import smtplib, ssl
from email.mime.text import MIMEText
import base64
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), ".env")
load_dotenv(dotenv_path)
PASSWORD = os.environ["EMAIL_PASSWORD"]
def fire_in_the_hole():
smtp_server = 'smtp.gmail.com'
port = 465
from_addr = '[email protected]'
to_addrs = ['[email protected]']
message = MIMEText('Hey big brother!')
message['subject'] = 'Alert received'
message['from'] = from_addr
message['to'] = ', '.join(to_addrs)
# we'll connect using SSL
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(from_addr, PASSWORD)
server.sendmail(from_addr, to_addrs, message.as_string())
def check_email():
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', PASSWORD)
mail.select('inbox')
status, data = mail.search(None, 'UNSEEN', 'FROM "[email protected]"')
mail_ids = []
for block in data:
mail_ids += block.split()
if not mail_ids:
return
for i in mail_ids:
status, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
message = email.message_from_bytes(response_part[1])
if message.is_multipart():
mail_content = ''
for part in message.get_payload():
mail_content += part.get_payload()
else:
mail_content = message.get_payload()
mail_content = base64.b64decode(mail_content).decode('utf-8')
if 'dropped' in mail_content:
fire_in_the_hole()
def task():
while True:
check_email()
time.sleep(300)
if __name__ == "__main__":
task()
1
makelove 2021-11-05 17:32:17 +08:00
国内的话,
至少阿里云是不行了,它们封 25 口,个人是没可能解的。 腾讯的可以,需要在设置里设置一下,不过有个巨坑是轻量的 VPS 不行。 百度的可以。 |
2
arischow 2021-11-05 17:45:12 +08:00
发肯定是 mailgun / ses 这样的服务比较好?
|
3
JamesR 2021-11-05 17:55:37 +08:00 1
不用 25 端口就行了,国内只封 25 端口,我看脚本里是 465 端口,放心用吧,啥服务器都行。
|
4
Qetesh 2021-11-05 17:59:38 +08:00 via iPhone
腾讯云和其他云都有 serverless 免费配额,足够了。场景也合适
|
6
illl 2021-11-05 21:18:20 +08:00 via iPhone
华为云提交工单可以解封,不过可以直接用 456 端口
|