标签:扫描 成功 def 邮件 两种 163邮箱 ica orm 发送
发邮件带图片附件
import os import smtplib import time 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 = ‘smtp.163.com‘ # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式 port = 465 # 设置发件邮箱,一定要自己注册的邮箱 sender = ‘17682303516@163.com‘ # 设置发件邮箱的密码,163邮箱的授权码,等会登陆会用到 pwd = ‘yu17682303516‘ # 设置邮件接收人,可以是扣扣邮箱 receiver0 = ‘{}@qq.com‘.format(receiver) # receiver1 = ‘54400407@qq.com‘ # 设置邮件正文,这里是支持HTML的 body = ‘<h1> 请扫描二维码 登入 WeGame </h1>‘ # 设置正文为符合邮件格式的HTML内容 msg = MIMEText(body, ‘html‘) message = MIMEMultipart() # 设置邮件标题 message[‘subject‘] = ‘请扫描二维码 登入 WeGame‘ # 设置发送人 message[‘from‘] = sender # 设置接收人 message[‘to‘] = receiver0 message.attach(msg) # 构造附件1,传送当前目录下的 filename 文件 path = os.path.dirname(os.path.abspath(__file__)) filename = ‘{}/iamges/erweima.png‘.format(path) # att1 = MIMEText(open(filename, ‘rb‘).read(), ‘utf-8‘) att1 = MIMEImage(open(filename, ‘rb‘).read(), ‘utf-8‘) att1["Content-Type"] = ‘application/octet-stream‘ # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = ‘attachment; filename="{}"‘.format(‘ewm.png‘) message.attach(att1) try: # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL s = smtplib.SMTP_SSL(host, port) # 登陆邮箱 s.login(sender, pwd) # 发送邮件! s.sendmail(sender, receiver0, message.as_string()) # 发送第二人邮件 # s.sendmail(sender, receiver1, message.as_string()) print(‘邮件发送成功‘) except smtplib.SMTPException: print(‘邮件发送失败‘) if __name__ == ‘__main__‘: sentemail(‘2649942575‘)
标签:扫描 成功 def 邮件 两种 163邮箱 ica orm 发送
原文地址:https://www.cnblogs.com/yoyo1216/p/12074484.html