def send_warning_emails(): """ :return: """ emailer = Emailer(host=SMTP_HOST, login_username=SMTP_USERNAME, login_password=SMTP_PASSWORD, tls_port=SMTP_TLS_PORT) warnings = MemberWarning.objects.filter(notified=False, acknowledged=False) for warning in warnings: recipients_string = warning.get_recipients_string() if recipients_string is None: continue subject, body = warning.get_subject_and_body() if subject is None or body is None: continue emailer.send_message( recipients_string, NOTIFICATION_EMAIL_ADDRESS, NOTIFICATION_EMAIL_SUBJECT_LEAD + " %s" % (subject, ), body) warning.notified = True warning.save()
def send_warning_emails(): """ :return: """ emailer = Emailer(host=SMTP_HOST, login_username=SMTP_USERNAME, login_password=SMTP_PASSWORD, tls_port=SMTP_TLS_PORT) warnings = MemberWarning.objects.filter(notified=False, acknowledged=False) for warning in warnings: recipients_string = warning.get_recipients_string() if recipients_string is None: continue subject, body = warning.get_subject_and_body() if subject is None or body is None: continue emailer.send_message(recipients_string, NOTIFICATION_EMAIL_ADDRESS, NOTIFICATION_EMAIL_SUBJECT_LEAD + " %s" % (subject,), body) warning.notified = True warning.save()
def send_exception_email(exception_message): """ :param exception_message: :return: """ emailer = Emailer(host=SMTP_HOST, login_username=SMTP_USERNAME, login_password=SMTP_PASSWORD, tls_port=SMTP_TLS_PORT) emailer.send_message(User.objects.get(username__iexact="admin").email, NOTIFICATION_EMAIL_ADDRESS, NOTIFICATION_EMAIL_SUBJECT_LEAD + " exception", exception_message)
def send_exception_email(exception_message): """ :param exception_message: :return: """ emailer = Emailer(host=SMTP_HOST, login_username=SMTP_USERNAME, login_password=SMTP_PASSWORD, tls_port=SMTP_TLS_PORT) emailer.send_message( User.objects.get(username__iexact="admin").email, NOTIFICATION_EMAIL_ADDRESS, NOTIFICATION_EMAIL_SUBJECT_LEAD + " exception", exception_message)
class RegistrationAPIView(generics.GenericAPIView): """Register new users.""" serializer_class = RegistrationSerializer renderer_classes = (UserJSONRenderer,) emailer = Emailer() def post(self, request): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() user_data = serializer.data message = [ request, user_data["email"] ] otp = generateOTP() cache.set(user_data["email"], otp, None) self.emailer.send_verify_email(user_data["email"], otp) response = { "data": { "user": dict(user_data), "message": "Account created successfully, Please verify your email Id", "status": "success" } } return Response(response, status=status.HTTP_201_CREATED)
class ForgotPasswordAPIView(generics.GenericAPIView): emailer = Emailer() def post(self, request): email = request.data["email"] otp = cache.get(email) if otp is None: otp = generateOTP() cache.set(email, otp, None) self.emailer.send_forgot_password_email(email, otp) response = { "data": { "message": "OTP has been send to your email account successfully", "status": "success" } } return Response(response, status=status.HTTP_200_OK)
class LoginAPIView(generics.GenericAPIView): """login a user via email""" serializer_class = LoginSerializer renderer_classes = (UserJSONRenderer,) emailer = Emailer() def post(self, request): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) user_data = serializer.data otp = cache.get(user_data["email"]) if otp is None: otp = generateOTP() cache.set(user_data["email"], otp, None) self.emailer.send_otp_email(user_data["email"], otp) response = { "data": { "user": dict(user_data), "message": "OTP has been send to your email account successfully", "status": "success" } } return Response(response, status=status.HTTP_200_OK)