zoukankan      html  css  js  c++  java
  • 邮箱认证

    from email.header import Header
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    from email.mime.base import MIMEBase
    from email.mime.multipart import MIMEMultipart
    from email import encoders
    import smtplib
    import time
    
    
    class EmailPro:
        def send_mail(self, to_email, code):
            email_host = 'smtp.163.com'  # 服务器地址 163邮箱"smtp.163.com"  qq邮箱"smtp.qq.com"都需要开通smtp权限
            sender = 'xxx@163.com'  # 发件人(自己的邮箱)
            password = 'xxx'  # 邮箱授权码
            receiver = 'xxx@qq.com'  # 收件人
            msg = MIMEMultipart()
            now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
            subject = now + '邮箱激活'
    
            h = Header('发件人昵称自定义', 'utf-8')
            h.append('<xxx@163.com>', 'ascii')
            msg["From"] = h
            msg['Subject'] = subject  # 标题
            msg['To'] = 'xxx'  # ...收件人...
    
            signature = '''
        
    	 You are most welcome!
        
    	 点击下面的按钮激活邮箱
        '''
            # text = MIMEText(signature, 'plain')  # 签名
            # msg.attach(text)
    
            # 正文-图片 只能通过html格式来放图片,所以要注释25,26行
            mail_msg = f'''
            
        <p>
    	 You are most welcome!</p>
        <p>
    	 点击下面的按钮激活邮箱</p>
        <button style="background-color: #31708f; border-radius: 3px"><a href="http://127.0.0.1:8000/user/email/active/?email={to_email}&code={code}" style="color: white;font-size: 25px;text-decoration: none">激活邮箱</a></button>
        <p><img src="cid:image1"></p>
        '''
            msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
            # 指定图片为当前目录
            fp = open(r'E:
    ent_housemediaanner3.jpg', 'rb')
            msgImage = MIMEImage(fp.read())
            fp.close()
            # 定义图片 ID,在 HTML 文本中引用
            msgImage.add_header('Content-ID', '<image1>')
            msg.attach(msgImage)
    
            # ctype = 'application/octet-stream'
            # maintype, subtype = ctype.split('/', 1)
            # 附件-图片
            # image = MIMEImage(open(r'E:
    ent_housemediaanner3.jpg', 'rb').read(), _subtype=subtype)
            # image.add_header('Content-Disposition', 'attachment', filename='img.jpg')
            # msg.attach(image)
            # 附件-文件
            # file = MIMEBase(maintype, subtype)
            # file.set_payload(open(r'E:
    ent_houseappsutils
    esponse.py', 'rb').read())
            # file.add_header('Content-Disposition', 'attachment', filename='test.txt')
            # encoders.encode_base64(file)
            # msg.attach(file)
    
            # 发送
            smtp = smtplib.SMTP()
            smtp.connect(email_host, 25)
            smtp.login(sender, password)
            smtp.sendmail(sender, to_email, msg.as_string())
            smtp.quit()
            print('success')
    
    
    email_worker = EmailPro()
  • 相关阅读:
    ZOJ3329One Person Game(循环型 数学期望)
    ZOJ3551Bloodsucker (数学期望)
    HDU3853LOOPS (师傅逃亡系列•三)(基础概率DP)
    HDU4035 Maze(师傅逃亡系列•二)(循环型 经典的数学期望)
    阿里云DataV专业版发布,为可视化创造更多可能!
    Logtail提升采集性能
    达摩院首席数据库科学家李飞飞:云原生新战场,我们如何把握先机?
    什么是最佳的视频用户体验?阿里云视频服务四大体验优化实践
    Lambda plus: 云上大数据解决方案
    基于大数据的舆情分析系统架构
  • 原文地址:https://www.cnblogs.com/zhouze/p/11490949.html
Copyright © 2011-2022 走看看