首页 > 代码库 > python发送邮件

python发送邮件

# -*- coding: UTF-8 -*-
‘‘‘
发送txt文本邮件
‘‘‘
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


msg = MIMEMultipart()
att1 = MIMEText(open(E:\自动对账.rar, rb).read(), base64, gb2312)
att1["Content-Type"] = application/octet-stream
att1["Content-Disposition"] = attachment; filename="123456.rar"#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)

text=MIMEText("hello how are you\ndddddd ")
# text.replace_header(‘Content-Transfer-Encoding‘,‘quoted-printable‘)
msg.attach(text)

mailto_list = [691408557@qq.com]
mail_host = "webmail.meryer.com"  # 设置服务器
mail_user = "market04"  # 用户名
mail_pass = "liu#123"  # 口令
mail_postfix = "meryer.com"  # 发件箱的后缀


def send_mail(to_list, sub, content):
    me = "hello" + "<" + mail_user + "@" + mail_postfix + ">"
    # msg = MIMEText(content, _subtype=‘plain‘, _charset=‘gb2312‘)
    msg[Subject] = sub
    msg[From] = me
    msg[To] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user, mail_pass)
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return True
    except Exception:
        print(错误)
        return False


if __name__ == __main__:
    if send_mail(mailto_list, "hello", "hello world"):
        print("发送成功")
    else:
        print("发送失败")

 

python发送邮件