python send email

#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import datetime


mail_list = ['[email protected]']
mail_host = "xxxxxxxxx"      #Mail Server
mail_user = "xxxxxxxxxx"
mail_postfix = "xxxxxxx"


def send_mail_task(mail_host, from_who, recive_list, content):
    import smtplib
    server = smtplib.SMTP()
    server.connect(mail_host)
    recive_list = list(set(recive_list))
    server.sendmail(from_who, recive_list, content)
    server.close()


def send_mail(title, context, send_list):
    """
    Send Email
    :param title:  Mail title
    :param context: Mail content
    :param send_list: A list of receiver
    :return: dict
    """
    from email.mime.text import MIMEText
    from bom_maintenance.celery.tasks import send_mail_task
    me = "<" + mail_user + "@" + mail_postfix + ">"
    msg = MIMEText(context, _subtype='plain', _charset='utf-8')
    msg['Subject'] = title
    msg['From'] = me
    msg['To'] = ";".join(send_list)
    try:
        send_mail_task(mail_host=mail_host, from_who=me, recive_list=send_list, content=msg.as_string())
        return {'info': 'Send ok!', 'code': 0}
    except Exception, e:
        return {'info': str(e), 'code': 1}

你可能感兴趣的:(python send email)