def _send_username_email(self, user):
"""
Send username reminder email to given email address
"""
username = user['uid'][0]
base_url = urljoin(cherrypy.request.base, '/')
user['login_url'] = urljoin(base_url, '?username={0}'.format(username))
template = cherrypy.config.get('email', {}).get('template', 'email')
html = cherrypy.engine.publish('lookup-template', '{0}.html'.format(template)).pop()
txt = cherrypy.engine.publish('lookup-template', '{0}.txt'.format(template)).pop()
html_body = html.render(user=user)
txt_body = txt.render(user=user)
msg = MIMEMultipart('alternative')
msg['Subject'] = (' ').join([self.subject, 'Username Reminder'])
msg['From'] = self.fromaddr
msg['To'] = user['mail'][0]
part1 = MIMEText(txt_body, 'plain')
part2 = MIMEText(html_body, 'html')
msg.attach(part1)
msg.attach(part2)
mailclient = smtplib.SMTP(self.server, self.port)
try:
if self.user and self.password:
mailclient.login(self.user, self.password)
mailclient.sendmail(msg['From'], msg['To'], msg.as_string())
except (smtplib.SMTPHeloError,
smtplib.SMTPAuthenticationError,
smtplib.SMTPException
) as e:
self.bus.log('Unable to send email. Error: {0}'.format(e.message['desc'] if 'desc' in e.message else e), 40) # pylint: disable=C0301
finally:
mailclient.quit()
评论列表
文章目录