zoukankan      html  css  js  c++  java
  • python 邮件发送html文件

    import smtplib
    import os
    from email.header import Header
    from email.mime.text import MIMEText
    
    def sendemail(host,usernames,verify):
        # 发件人和收件人
        sender = 'tdxxxxx@163.com'
        # receiver = 'xxxxxx@qq.com'
        receiver = 'xxxx.com'
    
        # 所使用的用来发送邮件的SMTP服务器
        smtpserver = 'smtp.163.com'
    
        # 发送邮箱的用户名和授权码(不是登录邮箱的密码)
        username = 'tdxxxxxxxx@163.com'
        password = 'xxxx'
    
        # 邮件主题
        mail_title = '主题:LeapIn找回密码'
        send_user = 'ttttt'
        verify_url = "127.0.0.1"
        # 读取html文件内容
        # f = open('/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html', "r+", encoding="utf-8")  # HTML文件默认和当前文件在同一路径下,若不在同一路径下,需要指定要发送的HTML文件的路径
        # file_data = ""
        # data = f.read()
        # for line in data:
        #     if "userName" in data:
        #         line = line.replace("userName", send_user)
        #     elif "reset_link" in line:
        #         line = line.replace("reset_link", verify_url)
        #     elif "host" in line:
        #         line = line.replace("host","ddddddddddd")
        # f.write(data)
        # f.close()
    
        with open("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "r", encoding="utf-8") as f1,open("%s.bak" % "/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "w+", encoding="utf-8") as f2:
            # for line in f1.readlines():
            #     if '{host}' in line:
            #         line = line.replace('{host}', host)
            #     elif '{userName}' in line:
            #         line = line.replace('{userName}', usernames)
            #     elif 'reset_link' in line:
            #         line = line.replace('reset_link', verify)
            #     f2.write(line)
            #     data = f2.read()
            data = f1.read()
            data = data.replace("{host}",host).replace("{userName}", usernames).replace("reset_link", verify)
            f2.write(data)
        # os.remove("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html")
        # os.rename("%s.bak" % "/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html")
        # data = f1.read()
    
        # mail_body = f.read()
        # print(mail_body.replace('{userName}', "ssssssssss"))
        # print(mail_body.replace('{host}', "127.0.0.1"))
        # print(mail_body.replace('reset_link', "verify_url"))
    
    
        # 邮件内容, 格式, 编码
        message = MIMEText(data, 'html', 'utf-8')
        message['From'] = sender
        message['To'] = receiver
        message['Subject'] = Header(mail_title, 'utf-8')
    
        try:
            smtp = smtplib.SMTP()
            smtp.connect('smtp.163.com')
            smtp.login(username, password)
            smtp.sendmail(sender, receiver, message.as_string())
            print("发送邮件成功!!!")
            smtp.quit()
        except smtplib.SMTPException:
            print("发送邮件失败!!!")
    
    
    # import os
    # def alter(file,old_str,new_str):
    #     """
    #     将替换的字符串写到一个新的文件中,然后将原文件删除,新文件改为原来文件的名字
    #     :param file: 文件路径
    #     :param old_str: 需要替换的字符串
    #     :param new_str: 替换的字符串
    #     :return: None
    #     """
    #     with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
    #         for line in f1:
    #             if old_str in line:
    #                 line = line.replace(old_str, new_str)
    #             f2.write(line)
    #     os.remove(file)
    #     os.rename("%s.bak" % file, file)
    #
    # alter("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "host", "127.0.0.1")
    # alter("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "userName", "ssssss")
    # alter("/Users/echo/Desktop/login/leapin-api/leapin/static/mail.html", "reset_link", "verify")
    
    if __name__ == '__main__':
        sendemail("1234566.com", "bbbbbbbbb", "wwww.baidu.com")
    <!DOCTYPE html>
    <head>
        <title>Leapin-resetpassword</title>
        <style>
            .wrapper{
                min- 500px;
                 800px;
                height: auto;
                margin: 100px auto;
                margin: 20vh auto;
                background:rgba(255,255,255,1);
                box-shadow:0px 4px 16px 0px rgba(217,217,217,1);
                border-radius:16px;
                overflow: hidden;
            }
            .header{
                height:122px;
                background:linear-gradient(180deg,rgba(34,87,191,1) 0%,rgba(34,87,191,1) 100%);
                border-radius:2px;
                text-align: center;
                color:#fff;
                line-height: 122px;
                font-size: 40px;
                text-align: center;
            }
            .content{
                padding: 40px 60px 60px;
            }
            .content >div:nth-child(1){
                height:50px;
                font-size:36px;
                font-family:PingFangSC-Medium,PingFang SC;
                font-weight:500;
                color:rgba(31,31,31,1);
                line-height:50px;
                text-align: center;
            }
            .content >div:nth-child(2){
                font-size:24px;
                font-family:PingFangSC-Regular,PingFang SC;
                font-weight:400;
                color:rgba(31,31,31,1);
                line-height:33px;
                margin: 40px 0 60px;
            }
            .content >div:nth-child(3){
                text-align: center;
            }
            .content >div:nth-child(3) button{
                260px;
                height:56px;
                background:rgba(34,87,191,1);
                border-radius:4px;
                color:#fff;
                border: 0;
                outline: none;
            }
            .footer{
                padding: 30px 0;
                margin: 0 60px;
                border-top: 1px solid rgba(217,217,217,1);
                text-align: center;
            }
            .footer p {
                font-size:18px;
                font-family:PingFangSC-Regular,PingFang SC;
                font-weight:400;
                color:rgba(31,31,31,1);
                line-height:32px;
            }
            .footer div {
                font-size:16px;
                font-family:PingFangSC-Regular,PingFang SC;
                font-weight:400;
                color:rgba(122,132,153,1);
                line-height:22px;
            }
        </style>
    </head>
    
    <body>
    <div class="wrapper">
        <div class="header">
            LeapIn
        </div>
        <div  class="content">
            <div>重置密码</div>
            <div>
                {userName} 您好! <br/>
                您正在申请重置密码,请点击下方按钮进行重置。
            </div>
            <div>
                <a target="_blank" href="reset_link">
                    <button>重置密码</button>
                </a>
            </div>
        </div>
        <div class="footer">
            <P>领先的企业招聘解决方案 <br/>
                让人才更与企业文化相配</P>
            <div>{host}</div>
        </div>
    </div>
    </body><!DOCTYPE html>
  • 相关阅读:
    串口RS232和485通信的波形分析
    Ubuntu添加中文输入法
    虚拟机桥接模式联网方法,Xshell的连接与使用
    waitpid 函数详解
    linux for循环 fork() 产生子进程
    【LeetCode解题总结】动态规划篇
    【LeetCode解题总结】递归篇
    【LeetCode解题总结】排序篇
    【LeetCode解题总结】树/图篇
    【LeetCode解题总结】栈/队列篇
  • 原文地址:https://www.cnblogs.com/tangda/p/12131862.html
Copyright © 2011-2022 走看看