def send_email(): from apps import mail, Message # must import mail after Blurprint init success!! msg = Message('Hello', sender='*****@*****.**', recipients=['*****@*****.**']) msg.body = 'testind body' msg.html = '<b>HTML</b> body' mail.send(msg) return '<h1>send email success!</h1>'
def send_mail(self, instance=None): token = uuid.uuid4().hex data = {"token": token, "user": instance.id} db.session.add(PasswordReset(**data)) db.session.commit() from flask_mail import Message msg = Message("Reset Password", recipients=[instance.email, ]) msg.body = "Click the following link to reset your password" msg.html = "<p>Click <a href='http://127.0.0.1:5000/api/v1/users/password-reset/?email="+instance.email+"&&token="+token+"'>here</a> " \ "to reset your password</p>" mail.send(msg)
def get_verification_code(): data = json.loads(request.form.get("data")) c = re.compile(r"^\w+@(\w+\.)+(com|cn|net)$") email_addr = c.search(data["email_addr"]).string if email_addr: chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] x = random.choice(chars), random.choice(chars), random.choice( chars), random.choice(chars) verify_code = ''.join(x) session["verify_code"] = verify_code #发送验证码 msg = Message("验证码", sender="*****@*****.**", recipients=[email_addr]) msg.body = "验证码为: " + verify_code mail.send(msg) return "200" #验证码发送成功 else: return "100" #邮箱未填写或格式错误
def get_verification_code2(): data = json.loads(request.form.get("data")) user = GeneralUser.query.filter_by(userName=data["username"]).first() email_addr = user.email print(email_addr) if email_addr: chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] x = random.choice(chars), random.choice(chars), random.choice( chars), random.choice(chars) verify_code = ''.join(x) session["verify_code"] = verify_code print(session["verify_code"]) #发送验证码 msg = Message("验证码", sender="*****@*****.**", recipients=[email_addr]) msg.body = "验证码为: " + verify_code mail.send(msg) return "200" #验证码发送成功 else: return "100" #用户名不存在
def send_async_email(app, msg, db): with app.app_context(): try: r = mail.send(msg) if not r: status = 0 except: status = -1 log = { 'status':status, 'subject':msg.subject, 'from':msg.sender, 'to':list(msg.send_to), 'date':msg.date, 'body':msg.body, 'html':msg.html, 'msgid':msg.msgId, 'time':time.time() } db.email_log.insert(log)
def send_async_mail(app, msg): with app.app_context(): mail.send(msg)
def sync_send_mail(newapp, msg): with newapp.app_context(): try: mail.send(msg) except Exception as e: pass