python发邮件

明天你好 2020-01-02

参考:

G:\文档\开发\python\发邮件

https://www.jianshu.com/p/bc057b304a14

# -*- coding: utf-8 -*-
from ufile import filemanager, bucketmanager
from ufile import config
import time
import os
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import smtplib
import MySQLdb
import json

sender = ‘‘
password = ‘xxx‘

def send_email(title, receivers, msg, attach_paths):
message = MIMEMultipart()
subject = title
message[‘Subject‘] = Header(subject, ‘utf-8‘)
message.attach(MIMEText(msg, ‘html‘, ‘utf-8‘))
for p in attach_paths:
if os.path.exists(p):
att = MIMEText(open(p, ‘r‘).read(), ‘plain‘, ‘utf-8‘)
att["Content-Type"] = ‘application/octet-stream‘
att["Content-Disposition"] = ‘attachment; filename="%s"‘ % p
message.attach(att)
try:
server = smtplib.SMTP_SSL(‘mail.opay-inc.com‘, 465)
# server.ehlo()
# server.starttls()
server.login(sender, password)
server.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException as e:
print(e.message)

相关推荐