发送普通文件以及添加附件
# -*- 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
'''
发送邮件函数,默认使用163smtp
:param mail_host: 邮箱服务器,16邮箱host: smtp.163.com
:param port: 端口号,163邮箱的默认端口是 25
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱密码(不是邮箱的登录密码,是邮箱的授权码)
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
: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 # 发送者账号
msg['To'] = recv # 接收者账号列表
msg['Accept-Language'] = 'zh-CN'
msg['Accept-Charset'] = 'ISO-8859-1,utf-8'
format = 'plain'
puretext = MIMEText(content,format,'utf-8') # 邮件内容
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) # 登录发送者的邮箱账号,密码
# 参数分别是 发送者,接收者,第三个是把上面的发送邮件的 内容变成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 发送完毕后退出smtp
print('email send success.')
if __name__ == '__main__':
email_user = 'xxxx@zhugefang.com' # 发送者账号
email_pwd = 'xxxxxxxxxxx' # 发送者密码,授权码
maillist = 'xxxxx@zhugefang.com'
title = u'测试邮件标题'
content = '这是抓取的1000图片,分为10个文件夹'
send_mail(email_user, email_pwd, maillist, title, content)
发送html内容格式的邮件
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
'''
发送邮件函数,默认使用163smtp
:param mail_host: 邮箱服务器,16邮箱host: smtp.163.com
:param port: 端口号,163邮箱的默认端口是 25
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱密码(不是邮箱的登录密码,是邮箱的授权码)
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:return:
'''
# 邮箱发送
def send_mail(username, passwd, recv, title,content, mail_host='mail.duozhuan.cn', port=25):
try:
msg = MIMEMultipart()
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
msg['Accept-Language'] = 'zh-CN'
msg['Accept-Charset'] = 'ISO-8859-1,utf-8'
format = 'html'
puretext = MIMEText(content,format,'utf-8') # 邮件内容
msg.attach(puretext)
smtp = smtplib.SMTP(mail_host, port=port) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.ehlo()
smtp.starttls()
smtp.login(username, passwd) # 登录发送者的邮箱账号,密码
# 参数分别是 发送者,接收者,第三个是把上面的发送邮件的 内容变成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 发送完毕后退出smtp
print('email send success.')
except Exception as e:
print('发送失败',e)
# 读取数据库查询多出来的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>标题</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' # 发送者账号
email_pwd = 'xxxxxx' # 发送者密码,授权码
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)
网友评论