def send_mail(self): from app.tasks import send_email token = self.generate_auth_token() token_url = url_for('main.confirm', token=token, _external=True) send_email.delay(self.email, "Confirm Your Account", 'auth/confirm', username=self.username, token_url=token_url)
def resend_verification_email(): email_template, subject = generate_url_and_email_template( current_user.email, current_user.username, current_user.first_name, current_user.last_name, email_category="verify_email", ) send_email.delay(current_user.email, subject, email_template) flash("A new verification email has been sent", "success") return redirect(url_for("home_blueprint.index"))
def lostpassword(): email = request.json.get("email") user = User.query.filter_by(email=email).first() if user: token = uuid.uuid4().hex user.set_lost_password(token) db.session.add(user) db.session.commit() message = "Hello,\n\nYou requested to reset your password, please follow this link: https://trackit.io/#/lostpassword/" + token send_email.delay(email, "Forgotten password", message, False, bypass_debug=True) return jsonify(message="A request was sent") return jsonify(error="Wrong email"), 400
def send_email(to, subject, email_type, **kwargs): if not isinstance(to, (list, tuple)): to = [to] msg = Message(subject, sender=current_app.config['WMJ_MAIL_SENDER'], recipients=to) if email_type == VENDOR_EMAIL_CONFIRM: msg.html = render_template('site/vendor_email_confirm.html', url=kwargs['url']) elif email_type == USER_EMAIL_CONFIRM: msg.html = render_template('site/user_email_confirm.html', url=kwargs['url']) elif email_type == ADMIN_EMAIL_REMINDS: msg.html = '<p>有新的厂家注册了, 快去审核!</p>' # elif email_type == USER_REGISTER: # msg.html = render_template('site/user_register.html', url=kwargs['url']) async_email.delay(msg) elif email_type == USER_EMAIL_RESET_PASSWORD: msg.html = render_template('site/user_reset_password.html', url=kwargs['url']) elif email_type == USER_FEEDBACK: msg.body = '用户反馈内容: %s\n用户联系方式: %s\n用户绑定手机号: %s' % (kwargs['feedback'], kwargs['contact'], kwargs['mobile']) async_email.delay(msg)
def handle(self, *args, **options): """ Choice winner and send e-mail. :param args: :param options: :return: """ date_range = [ datetime.datetime.now() - datetime.timedelta(days=1), datetime.datetime.now() ] max_views = PhotoOpening.objects.filter(date_view__range=date_range).\ values('photo_id').annotate(views=Count('photo_id')).order_by('-views')[:3] for view in max_views: user = User.objects.get(photo=view['photo_id']) message = Setting.objects.get(name='daily_notification').value % ( user.first_name, user.last_name) send_email.delay('Your photo in TOP 3', message, '*****@*****.**', user.email)
def request_password_reset(): form = RequestResetPasswordForm() if current_user.is_authenticated: logout_user() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() email_template, subject = generate_url_and_email_template( user.email, user.username, user.first_name, user.last_name, email_category="password_reset", ) send_email.delay(user.email, subject, email_template) flash("An link to reset you password has been sent to your email.", "success") return redirect(url_for("home_blueprint.index")) return render_template("accounts/forgot_password.html", form=form)
def register(): form = CreateAccountForm() if form.validate_on_submit(): user = User(**request.form) db.session.add(user) db.session.commit() email_template, subject = generate_url_and_email_template( form.email.data, form.username.data, form.first_name.data, form.last_name.data, email_category="verify_email", ) send_email.delay(form.email.data, subject, email_template) flash( "Your account has been created. Check your inbox to verify your email.", "success", ) return redirect(url_for("base_blueprint.login")) return render_template("accounts/register.html", form=form)
def register(request): if request.method == 'GET': # get请求展示注册页面 return render(request, 'user/register.html') elif request.method == 'POST': # post请求提交注册的用户数据 username = request.POST.get('username') email = request.POST.get('email') password = request.POST.get('password') password = make_password(password) # 使用django系统自带的加密方法加密密码 icon = request.FILES.get('icon') # 文件获取使用FILES方法 user = AXFUser() user.u_username = username user.u_password = password user.u_email = email user.u_icon = icon user.save() u_token = uuid.uuid4().hex # 生成token给激活链接使用 cache.set(u_token, user.id, timeout=60 * 60 * 2) # 使用redis缓存,一般需要根据token找userid,所以token要放在前面 # from app.view_helper import send_email #这里调用的是未做异步处理的发送邮件方法 # send_email(email,username,u_token)#这里发送邮件未做异步处理,需要等邮件发送出去才会跳转到登陆页面 from app.tasks import send_email send_email.delay(email, username, u_token) # 使用celery进行异步处理邮件发送 # return redirect(reverse('app:login')) return render(request, 'user/login.html')
def send_email(to, subject, email_type, **kwargs): if not isinstance(to, (list, tuple)): to = [to] msg = Message(subject, sender=current_app.config['WMJ_MAIL_SENDER'], recipients=to) if email_type == VENDOR_EMAIL_CONFIRM: msg.html = render_template('site/vendor_email_confirm.html', url=kwargs['url']) elif email_type == USER_EMAIL_CONFIRM: msg.html = render_template('site/user_email_confirm.html', url=kwargs['url']) elif email_type == ADMIN_EMAIL_REMINDS: msg.html = '<p>有新的厂家注册了, 快去审核!</p>' # elif email_type == USER_REGISTER: # msg.html = render_template('site/user_register.html', url=kwargs['url']) async_email.delay(msg) elif email_type == USER_EMAIL_RESET_PASSWORD: msg.html = render_template('site/user_reset_password.html', url=kwargs['url']) elif email_type == USER_FEEDBACK: msg.body = '用户反馈内容: %s\n用户联系方式: %s\n用户绑定手机号: %s' % ( kwargs['feedback'], kwargs['contact'], kwargs['mobile']) async_email.delay(msg)
def user_profile(): form = UserProfileUpdateForm(obj=current_user) modal = ConfirmAccountDeletionForm() if request.method == "POST" and form.validate_on_submit(): try: if (request.files.get("profile_photo").filename != ""): # Check if a profile image has been uploaded # Delete the previous profile image if it exists delete_profile_image.delay( profile_image_upload_dir, current_user.profile_photo, s3_bucket_name=aws_s3_bucket_name, ) file = request.files.get("profile_photo") filename = save_image_to_redis(file) profile_image_process.delay(filename, photo_type="profile") current_user.profile_photo = filename current_user.prof_photo_loc = image_server_config if (request.files.get("cover_photo").filename != ""): # Check if a cover image has been uploaded # Delete the previous profile image if it exists delete_profile_image.delay( cover_image_upload_dir, current_user.cover_photo, s3_bucket_name=aws_s3_bucket_name, ) file = request.files.get("cover_photo") filename = save_image_to_redis(file) profile_image_process.delay(filename, photo_type="cover") current_user.cover_photo = filename current_user.cover_photo_loc = image_server_config # AttributeError: 'NoneType' object has no attribute 'filename'. This error can result from the request not # containing 'profile_photo' and 'cover_photo' stream data in the request body. The error is likely to occur # testing this route with flask.test_client() and not with a browser. except AttributeError: pass flash_message, css_class = ( "Your account information has been updated.", "success", ) for key, value in request.form.items(): if hasattr(value, "__iter__") and not isinstance(value, str): value = value[0] if key == "email": # check if user has updated email, then set the is_verified attribute to False so that the user can verify the new email if current_user.email != value: setattr(current_user, key, value) current_user.is_verified = False email = value email_template, subject = generate_url_and_email_template( email, current_user.username, current_user.first_name, current_user.last_name, email_category="verify_email", ) send_email.delay(value, subject, email_template) flash_message, css_class = ( "Your account information has been updated. Check your inbox to verify your new email.", "success", ) if key == "password": # Check if password is in the form if value: value = generate_password_hash( value) # Hash the password from the form else: # Maintain the old user's password in the database if password was not in form value = current_user.password setattr(current_user, key, value) db.session.commit() flash(flash_message, css_class) return redirect( url_for("base_blueprint.user_profile", user_id=current_user.id)) elif request.method == "GET": # Populates the form fields with user data. form.populate_obj(current_user) return render_template( "accounts/settings.html", form=form, modal=modal, s3_image_url=s3_image_url, profile_image_dir=profile_image_upload_dir, cover_image_dir=cover_image_upload_dir, )
def email_api(request): result = send_email.delay() # 发送邮件 return HttpResponse("邮件发送成功 !!! ")