-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.py
60 lines (52 loc) · 1.8 KB
/
mail.py
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
56
57
58
59
60
# -*- coding: utf-8 -*-
from datetime import datetime
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL, SMTP
from settings import *
#from mail import send_email_qq
def send_email_qq(message):
#qq邮箱smtp服务器
smtp_server = QQ.get('smtpserver')
hour = datetime.now().hour
if hour in range(7, 25):
qq = QQ.get('qq')[0]
#pwd为qq邮箱的授权码
pwd = QQ.get('password')[0]
else:
qq = QQ.get('qq')[1]
pwd = QQ.get('password')[1]
# sender_qq为发件人的qq号码
sender_qq = qq
#发件人的邮箱
sender_qq_mail = qq + '@qq.com'
#收件人邮箱
receiver = QQ.get('receive')
#邮件的正文内容
mail_content = message
#邮件标题
mail_title = message #'你现在在进行一项用python登录qq邮箱发邮件的测试'
#ssl登录
smtp = SMTP_SSL(smtp_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
# smtp.set_debuglevel(1)
smtp.ehlo(smtp_server)
smtp.login(sender_qq, pwd)
msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = str(receiver)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
def send_email(message):
smtp_server = EmailConfig.get('smtpserver')
sender = EmailConfig.get('sender')
password = EmailConfig.get('password')
msg = MIMEText(message)
msg['Subject'] = 'Email by Python'
msg['From'] = EmailConfig.get('from')
msg['To'] = EmailConfig.get('to')
mailserver = SMTP(smtp_server, 25)
mailserver.login(sender, password)
mailserver.sendmail(msg['From'], [msg['To']], msg.as_string())
mailserver.quit()