class Mailer: def __init__(self, from_address, username, password): self.conn = SMTPAsync() self.username = username self.password = password self.from_address = from_address async def connect(self): await self.conn.connect("smtp.gmail.com", 587) await self.conn.starttls() await self.conn.login(self.username, self.password) __aenter__ = connect async def send(self, msg: Message, to: str): mime = MIMEMultipart('alternative') mime['Subject'] = msg.subject mime['From'] = self.from_address mime['To'] = to mime.attach(MIMEText(msg.text_content, 'plain')) mime.attach(MIMEText(msg.content, 'html')) self.conn.sendmail(self.from_address, to, mime.as_string()) async def disconnect(self): self.conn.quit() def __aexit__(self, exc_type, exc, tb): self.disconnect()
def send_confirm_mail(self, user): s = SMTPAsync() yield s.connect(mail_settings['host'], mail_settings['port']) yield s.starttls() yield s.login(mail_settings['email'], mail_settings['password']) me = mail_settings['email'] msg = MIMEMultipart('alternative') msg['Subject'] = "【友拍平台】摄影师注册确认" msg['From'] = me msg['To'] = user.email confirm_url = "http://{host}/user/{uid}/confirmation/{token}"\ .format(host=self.request.host, uid=user.id.hex, token=self.generate_confirmation_token(user).decode()) text = """ <html> <body> 尊敬的摄影师您好,恭喜您注册友拍成功,请<a href="{}">点击链接</a> 验证您的邮箱,并完善您的信息,您可以上传至少两套照片以便于我们审核。审核的结果会以邮件形式通知您。<br/> 感激您对友拍的信任。要约拍,来友拍。 </body> </html> """\ .format(confirm_url) content = MIMEText(text, "html") msg.attach(content) yield s.sendmail(me, user.email, msg.as_string()) yield s.quit()
def send_activate_mail(self, user, is_activate=True): subjects = ("【友拍平台】摄影师审核失败", "【友拍平台】摄影师审核通过") s = SMTPAsync() yield s.connect(mail_settings['host'], mail_settings['port']) yield s.starttls() yield s.login(mail_settings['email'], mail_settings['password']) me = mail_settings['email'] msg = MIMEMultipart('alternative') msg['Subject'] = subjects[is_activate] msg['From'] = me msg['To'] = user.email texts = (""" <html> <body> 尊敬的摄影师您好,很遗憾您没有获得友拍的入驻资格,原因不是在您,而是由于友拍平台的发展限制,我们还在萌芽阶段, 我们希望能够创造一个足够精致的约拍环境,满足所有人追求美的心愿,衷心的希望平台能和您一同成长,在未来的某一天, 能一同为华科里想拍照的人带来方便。要约拍,来友拍。谢谢! </body> </html> """, """ <html> <body> 尊敬的摄影师您好,恭喜您已经获得了友拍平台(华科约拍平台)的入驻资格,友拍非常欣 喜能迎来您这样以为优秀的摄影师,祝愿您能够充分的利用好友拍平台,拍摄更多佳作,得到更多收获 。为了让您更方便使用友拍,请注意以下几点:<br/> 首先请您添加我们的微信客服友小拍拉您进入友拍入驻摄影师群,所有的友拍摄影师都在那里等您。友小拍微信号:18162591091<br/> 第二:您可以使用我们的管理页面管理您的照片,欢迎您常常更新~。<br/> 我们希望有更多的摄影师加入我们,提高平台的质量,来吸引大量的用户,如果您身边有我们还未发掘的优秀摄影师,那请 一定介绍给我们,友拍还在萌芽阶段,衷心感谢各位摄影师的支持与帮助。要约拍,来友拍,美照恒久远,一张永流传。谢谢! </body> </html> """) content = MIMEText(texts[is_activate], "html") msg.attach(content) yield s.sendmail(me, user.email, msg.as_string()) yield s.quit()