利用shunwang邮箱发邮件 带附件附件
import os import smtplib import time from email.header import Header from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText def sentemail(receiver): caodate = str(time.strftime('%Y-%m-%d', time.localtime())) # 设置发件服务器地址 host = 'mail.shunwang.com' # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式 port = 25 # 设置发件邮箱,一定要自己注册的邮箱 sender = 'opengl@shunwang.com' # 设置发件邮箱的密码,163邮箱的授权码,等会登陆会用到 pwd = 'Kol90120' # 设置邮件接收人,可以是扣扣邮箱 receiver0 = [receiver, '1064145110@qq.com'] # 设置邮件正文,这里是支持HTML的 body = '<h1> 请扫描二维码 登入 WeGame </h1>' # 设置正文为符合邮件格式的HTML内容 msg = MIMEText(body, 'html') message = MIMEMultipart() # 设置邮件标题 message['subject'] = Header('请扫描二维码 登入 WeGame', 'utf-8') # 设置发送人 message['from'] = Header(sender, 'utf-8') # 设置接收人 message['to'] = Header(sender, 'utf-8') message.attach(msg) # 构造附件1,传送当前目录下的 filename 文件 path = os.path.dirname(os.path.abspath(__file__)) filename = '{}/iamge/erweima.png'.format(path) # filename = '{}/iamge/程序接入.doc'.format(path) att1 = MIMEImage(open(filename, 'rb').read(), 'utf-8') # att1 = MIMEText(open(filename, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="{}"'.format(filename) # ewm.png message.attach(att1) try: # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL s = smtplib.SMTP(host, port) # 登陆邮箱 s.login(sender, pwd) # 发送邮件! s.sendmail(sender, receiver0, message.as_string()) print('邮件发送成功') except smtplib.SMTPException: print('邮件发送失败') if __name__ == '__main__': sentemail('cf.yu@shunwang.com')