美文网首页
python send mail

python send mail

作者: 乔伊斯 | 来源:发表于2016-07-28 15:57 被阅读0次

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email.mime.text import MIMEText

from email.utils import COMMASPACE,formatdate

from email import encoders

import os

defsend_mail(server, fro, to, subject, text, files=[]):

asserttype(server) ==dict

asserttype(to) ==list

asserttype(files) ==list

msg = MIMEMultipart()

msg['From'] = fro

msg['Subject'] = subject

msg['To'] = COMMASPACE.join(to)#COMMASPACE==', '

msg['Date'] = formatdate(localtime=True)

msg.attach(MIMEText(text))

forfinfiles:

part = MIMEBase('application','octet-stream')#'octet-stream': binary data

part.set_payload(open(f,'rb').read())

encoders.encode_base64(part)

part.add_header('Content-Disposition','attachment; filename="%s"'% os.path.basename(f))

msg.attach(part)

importsmtplib

smtp = smtplib.SMTP(server['name'], server['port'])

smtp.ehlo()

smtp.starttls()

smtp.ehlo()

smtp.login(server['user'], server['passwd'])

smtp.sendmail(fro, to, msg.as_string())

smtp.close()

if__name__=='__main__':

server = {'name':'smtp.qq.com','user':'1020121001','passwd':'101010101','port':25}

fro ='1020121001@qq.com'

to = ['joyesong@qq.com']

subject ='脚本运行提醒'

text ='mail content'

files = ['top_category.txt']

send_mail(server, fro, to, subject, text,files=files)

相关文章

网友评论

      本文标题:python send mail

      本文链接:https://www.haomeiwen.com/subject/fopajttx.html