美文网首页
python发送邮件

python发送邮件

作者: 撸大师 | 来源:发表于2016-10-11 10:13 被阅读92次

1. 发一般文档


#!/usr/bin/env python

#coding:utf8

# 导入 smtplib 和 MIMEText

import sys,getpass

import smtplib

from email.mime.text import MIMEText

# 发送邮件函数

def send_mail(to_list, sub,context):

       me = mail_user + "<"+mail_user+"@"+mail_postfix+">"

       f = open(context)

       msg = MIMEText(f.read(),_charset="utf-8")

#msg = MIMEText(context)

       f.close()

       msg['Subject'] = sub

       msg['From'] = me

       msg['To'] = "".join(to_list)

       try:

               send_smtp = smtplib.SMTP()

               send_smtp.connect(mail_host)

               send_smtp.login(mail_user, mail_pass)

               send_smtp.sendmail(me, to_list, msg.as_string())

               send_smtp.close()

               return True

       except Exception, e:

               print str(e)

               return False

# 设置服务器名称、用户名、密码以及邮件后缀

mail_host = "smtp.126.com"

mail_user = "iphone3000@126.com"

mail_pass =getpass.getpass()    #隐藏输入密码

mail_postfix="126.com"

#mailto_list = ["1542141838@qq.com","jjyy@qq.com"]

mailto_list = sys.argv[1]

sub= sys.argv[2]

context=sys.argv[3]

if send_mail(mailto_list,sub,context):

       print "Send mail succed!"

else:

       print "Send mail failed!"

2. 发带附件邮件并包含主题


#!/usr/bin/env python

#coding:utf8

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email.mime.text import MIMEText

from email import encoders

import sys

def send_mail(to_list, sub, context, filename):

       me = mail_user + "<"+mail_user+"@"+mail_postfix+">"

       msg = MIMEMultipart()

       msg['Subject'] = sub

       msg['From'] = me

       msg['To'] = "".join(to_list)

       context = MIMEText(context,_subtype='plain',_charset='utf-8')  #解决乱码

       msg.attach(context)

       submsg = MIMEBase('application', 'x-xz')

       submsg.set_payload(open(filename,'rb').read())

       encoders.encode_base64(submsg)

       submsg.add_header('Content-Disposition', 'attachment', filename=filename)

       msg.attach(submsg)

       try:

               send_smtp = smtplib.SMTP()

               send_smtp.connect(mail_host)

               send_smtp.login(mail_user, mail_pass)

               send_smtp.sendmail(me, to_list, msg.as_string())

               send_smtp.close()

               return True

       except Exception, e:

               print str(e)[1]

               return False

# 设置服务器名称、用户名、密码以及邮件后缀

mail_host = "mail.weimob.com"

mail_user = "jianwen.lu@weimob.com"

mail_pass = "Jay446585569"

mail_postfix = "weimob.com"

#mailto_list = ["763836801@qq.com","jianwen.lu@weimob.com"]

mailto_lists = sys.argv[1]

mailto_list = mailto_lists.split(',')   #发送多人

sub= sys.argv[2]

context = sys.argv[3]

filename = sys.argv[4]

if send_mail(mailto_list, sub, context, filename):

       print "Send mail succed!"

else:

       print "Send mail failed!"

相关文章

网友评论

      本文标题:python发送邮件

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