發(fā)送普通文件以及添加附件
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
'''
發(fā)送郵件函數(shù)窝爪,默認(rèn)使用163smtp
:param mail_host: 郵箱服務(wù)器弛车,16郵箱host: smtp.163.com
:param port: 端口號,163郵箱的默認(rèn)端口是 25
:param username: 郵箱賬號 xx@163.com
:param passwd: 郵箱密碼(不是郵箱的登錄密碼齐媒,是郵箱的授權(quán)碼)
:param recv: 郵箱接收人地址,多個賬號以逗號隔開
:param title: 郵件標(biāo)題
:param content: 郵件內(nèi)容
:return:
'''
def send_mail(username, passwd, recv, title,content, mail_host='smtp.exmail.qq.com', port=25):
msg = MIMEMultipart()
msg['Subject'] = title # 郵件主題
msg['From'] = username # 發(fā)送者賬號
msg['To'] = recv # 接收者賬號列表
msg['Accept-Language'] = 'zh-CN'
msg['Accept-Charset'] = 'ISO-8859-1,utf-8'
format = 'plain'
puretext = MIMEText(content,format,'utf-8') # 郵件內(nèi)容
msg.attach(puretext)
# 郵箱附件
zippart = MIMEApplication(open('send_email.py','rb').read())
zippart.add_header('Content-Disposition', 'attachment', filename='send_email.py')
msg.attach(zippart)
smtp = smtplib.SMTP(mail_host, port=port) # 連接郵箱纷跛,傳入郵箱地址喻括,和端口號,smtp的端口號是25
smtp.login(username, passwd) # 登錄發(fā)送者的郵箱賬號贫奠,密碼
# 參數(shù)分別是 發(fā)送者唬血,接收者,第三個是把上面的發(fā)送郵件的 內(nèi)容變成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 發(fā)送完畢后退出smtp
print('email send success.')
if __name__ == '__main__':
email_user = 'xxxx@zhugefang.com' # 發(fā)送者賬號
email_pwd = 'xxxxxxxxxxx' # 發(fā)送者密碼,授權(quán)碼
maillist = 'xxxxx@zhugefang.com'
title = u'測試郵件標(biāo)題'
content = '這是抓取的1000圖片叮阅,分為10個文件夾'
send_mail(email_user, email_pwd, maillist, title, content)
發(fā)送html內(nèi)容格式的郵件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from Mydb import Mydb
import time,datetime,json
'''
發(fā)送郵件函數(shù)刁品,默認(rèn)使用163smtp
:param mail_host: 郵箱服務(wù)器,16郵箱host: smtp.163.com
:param port: 端口號,163郵箱的默認(rèn)端口是 25
:param username: 郵箱賬號 xx@163.com
:param passwd: 郵箱密碼(不是郵箱的登錄密碼浩姥,是郵箱的授權(quán)碼)
:param recv: 郵箱接收人地址,多個賬號以逗號隔開
:param title: 郵件標(biāo)題
:param content: 郵件內(nèi)容
:return:
'''
# 郵箱發(fā)送
def send_mail(username, passwd, recv, title,content, mail_host='mail.duozhuan.cn', port=25):
try:
msg = MIMEMultipart()
msg['Subject'] = title # 郵件主題
msg['From'] = username # 發(fā)送者賬號
msg['To'] = recv # 接收者賬號列表
msg['Accept-Language'] = 'zh-CN'
msg['Accept-Charset'] = 'ISO-8859-1,utf-8'
format = 'html'
puretext = MIMEText(content,format,'utf-8') # 郵件內(nèi)容
msg.attach(puretext)
smtp = smtplib.SMTP(mail_host, port=port) # 連接郵箱状您,傳入郵箱地址勒叠,和端口號,smtp的端口號是25
smtp.ehlo()
smtp.starttls()
smtp.login(username, passwd) # 登錄發(fā)送者的郵箱賬號膏孟,密碼
# 參數(shù)分別是 發(fā)送者眯分,接收者,第三個是把上面的發(fā)送郵件的 內(nèi)容變成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 發(fā)送完畢后退出smtp
print('email send success.')
except Exception as e:
print('發(fā)送失敗',e)
# 讀取數(shù)據(jù)庫查詢多出來的url鏈接
def read_url(mydb):
try:
now_time = int(time.time())
# 昨天凌晨的時間
today = datetime.date.today()
old_time = int(time.mktime(today.timetuple()))
sql = 'select * from wdty_url WHERE time_t>{}'.format(old_time)
res = mydb.query(sql)
if len(res) > 0:
content_1 = '''
<html>
<body>
<table border="1">
<tr>
<th>標(biāo)題</th>
<th>url</th>
</tr>
<tr>'''
content_2 = '''
</tr>
</table>
</body>
</html>
'''
html_statement = ''' '''
for i in res:
html_statement += '<tr><td>{title}</td><td>{url}</td></tr>'.format(title=i[2],url=i[1])
content = content_1 + html_statement + content_2
email_user = 'xxxxxx@duozhuan.cn' # 發(fā)送者賬號
email_pwd = 'xxxxxx' # 發(fā)送者密碼,授權(quán)碼
maillist = 'xxxxx@duozhuan.cn'
title = '測試郵件'
send_mail(email_user, email_pwd, maillist, title, content)
else:
print('沒有新的鏈接')
except Exception as e:
print(e)
if __name__ == '__main__':
mydb = Mydb('xxxxx','root','xxxxx','xxxxx')
res = read_url(mydb)