-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_email_util.py
More file actions
55 lines (48 loc) · 1.56 KB
/
my_email_util.py
File metadata and controls
55 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from utils import get_data
import requests
def one() -> str:
"""
获取一条一言。
:return:
"""
try:
url = "https://v1.hitokoto.cn/"
res = requests.get(url).json()
return res["hitokoto"] + " ----" + res["from"]
except requests.exceptions.ConnectionError:
return ""
def sendMail(message, subject, to_addres=None, from_show='lustime', to_show='lbb', cc_show=None):
"""
:param message: str 邮件内容
:param subject: str 主题
:param from_show: str 发件人显示
:param to_show: str 收件人显示
:param cc_show: str 抄送人显示
:param to_addres: str 实际收件人
:return:
"""
data = get_data()
mail = data.get('MAIL')
user = mail.get('user')
password = mail.get('password')
if to_addres is None:
to_addres = mail.get('toAddres')
hitokoto = data.get('HITOKOTO')
text = one() if hitokoto else ''
message += '\n\n' + text
msg = MIMEText(message, 'plain', _charset="utf-8")
msg["Subject"] = subject
msg["from"] = from_show
msg["to"] = to_show
msg["Cc"] = cc_show
with SMTP_SSL(host="smtp.qq.com", port=465) as smtp:
smtp.login(user=user, password=password)
smtp.sendmail(from_addr=user, to_addrs=to_addres.split(','), msg=msg.as_string())
smtp.quit()
print('邮件通知完成')
if __name__ == '__main__':
message = 'Python 测试邮件...'
Subject = '主题测试'
sendMail(message, Subject, to_show='ailulu')