Python发送邮件

#!/usr/bin/env python

# -*- coding: utf-8 -*-

#导入smtplib和MIMEText

import smtplib,sys

from email.mime.text import MIMEText

def send_mail(sub):

#####################要发给谁,这里发给1个人

        file_user = open('user_list.txt','r')

        user_list = file_user.read()

        mailto_list = user_list.replace("\n","").split(';')

        print mailto_list

#####################设置服务器,用户名、口令以及邮箱的后缀

        mail_host="smtp.exmail.qq.com"

        mail_name="Service Directory  Modify"

        mail_user="[email protected]"

        mail_pass="Xzh110.com"

        mail_postfix="email.qq.com"

########################################

#to_list:发给谁

#sub:主题

#content:内容

#send_mail("[email protected]","sub","content")

        file = open('/root/scripts/modif.txt','r')

        content = file.read()

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

        msg = MIMEText(content,_charset='gbk')

        msg['Subject'] = sub

        msg['From'] = me

        msg['To'] = ";".join(mailto_list)

        try:

                s = smtplib.SMTP()

                s.connect(mail_host)

                s.login(mail_user,mail_pass)

                s.sendmail(me, mailto_list, msg.as_string())

                s.close()

                return True

        except Exception, e:

                print str(e)

                return False

if __name__ == '__main__':

        if send_mail(u'Servcie Check'):

                print u'发送成功'

        else:

                print u'发送失败'

你可能感兴趣的:(Python发送邮件)