def post(self, request): self.serializer_class = TestSMTPConfigSerializer if not SysOptions.smtp_config: return self.error("Please setup SMTP config at first") try: send_email(smtp_config=SysOptions.smtp_config, from_name=SysOptions.website_name_shortcut, to_name=request.user.username, to_email=request.data["email"], subject="You have successfully configured SMTP", content="You have successfully configured SMTP") except smtplib.SMTPResponseException as e: # guess error message encoding msg = b"Failed to send email" try: msg = e.smtp_error # qq mail msg = msg.decode("gbk") except Exception: msg = msg.decode("utf-8", "ignore") return self.error(msg) except Exception as e: msg = str(e) return self.error(msg) return self.success()
def send_email_async(from_name, to_email, to_name, subject, content): if not SysOptions.smtp_config: return try: send_email(smtp_config=SysOptions.smtp_config, from_name=from_name, to_email=to_email, to_name=to_name, subject=subject, content=content) except Exception as e: logger.exception(e)
def post(self, request): if not SysOptions.smtp_config: return self.error("Please setup SMTP config at first") try: send_email(smtp_config=SysOptions.smtp_config, from_name=SysOptions.website_name_shortcut, to_name=request.user.username, to_email=request.data["email"], subject="You have successfully configured SMTP", content="You have successfully configured SMTP") except smtplib.SMTPResponseException as e: # guess error message encoding msg = b"Failed to send email" try: msg = e.smtp_error # qq mail msg = msg.decode("gbk") except Exception: msg = msg.decode("utf-8", "ignore") return self.error(msg) except Exception as e: msg = str(e) return self.error(msg) return self.success()