comparison src/plugins/plugin_misc_account.py @ 1898:31b8a58cfcf5

plugin misc_account: fixes sending email (twisted.mail.smtp.sendmail doesn't want unicode)
author souliane <souliane@mailoo.org>
date Wed, 09 Mar 2016 11:52:18 +0100
parents e15c7d71ae76
children faa8427dd032
comparison
equal deleted inserted replaced
1897:e15c7d71ae76 1898:31b8a58cfcf5
277 email_password = self.getConfig("email_password") or None 277 email_password = self.getConfig("email_password") or None
278 email_starttls = C.bool(self.getConfig("email_starttls")) 278 email_starttls = C.bool(self.getConfig("email_starttls"))
279 email_auth = C.bool(self.getConfig("email_auth")) 279 email_auth = C.bool(self.getConfig("email_auth"))
280 domain = self.getConfig('new_account_domain') 280 domain = self.getConfig('new_account_domain')
281 281
282 # email to the administrator 282 def sendEmail(recipients, msg):
283 return sendmail(email_host.encode("utf-8"),
284 email_from.encode("utf-8"),
285 [email.encode("utf-8") for email in recipients],
286 msg.as_string(),
287 senderDomainName=email_sender_domain.encode("utf-8"),
288 port=email_port,
289 username=email_username.encode("utf-8"),
290 password=email_password.encode("utf-8"),
291 requireAuthentication=email_starttls,
292 requireTransportSecurity=email_auth)
293
294 # email to the administrators
283 admins_emails = self.getConfig('email_admins_list') 295 admins_emails = self.getConfig('email_admins_list')
284 if not admins_emails: 296 if not admins_emails:
285 log.warning(u"No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter") 297 log.warning(u"No known admin email, we can't send email to administrator(s).\nPlease fill email_admins_list parameter")
286 d_admin = defer.fail(exceptions.DataError("no admin email")) 298 d_admin = defer.fail(exceptions.DataError("no admin email"))
287 else: 299 else:
289 msg = MIMEText(body, 'plain', 'UTF-8') 301 msg = MIMEText(body, 'plain', 'UTF-8')
290 msg['Subject'] = _('New Libervia account created') 302 msg['Subject'] = _('New Libervia account created')
291 msg['From'] = email_from 303 msg['From'] = email_from
292 msg['To'] = ", ".join(admins_emails) 304 msg['To'] = ", ".join(admins_emails)
293 305
294 d_admin = sendmail(email_host, email_from, admins_emails, msg.as_string(), 306 d_admin = sendEmail(admins_emails, msg)
295 senderDomainName=email_sender_domain,
296 port=email_port,
297 username=email_username,
298 password=email_password,
299 requireAuthentication=email_starttls,
300 requireTransportSecurity=email_auth)
301 admins_emails_txt = u', '.join([u"<{}>".format(addr) for addr in admins_emails]) 307 admins_emails_txt = u', '.join([u"<{}>".format(addr) for addr in admins_emails])
302 d_admin.addCallbacks(lambda dummy: log.debug(u"Account creation notification sent to admin(s) {}".format(admins_emails_txt)), 308 d_admin.addCallbacks(lambda dummy: log.debug(u"Account creation notification sent to admin(s) {}".format(admins_emails_txt)),
303 lambda dummy: log.error(u"Failed to send account creation notification to admin {}".format(admins_emails_txt))) 309 lambda dummy: log.error(u"Failed to send account creation notification to admin {}".format(admins_emails_txt)))
304 if not email: 310 if not email:
305 return d_admin 311 return d_admin
333 log.error(u"Failed to send account creation confirmation to <%s>" % email) 339 log.error(u"Failed to send account creation confirmation to <%s>" % email)
334 340
335 # XXX: this will not fail when the email address doesn't exist 341 # XXX: this will not fail when the email address doesn't exist
336 # FIXME: check email reception to validate email given by the user 342 # FIXME: check email reception to validate email given by the user
337 # FIXME: delete the profile if the email could not been sent? 343 # FIXME: delete the profile if the email could not been sent?
338 d_user = sendmail(email_host, email_from, email, msg.as_string(), 344 d_user = sendEmail([email], msg)
339 senderDomainName=email_sender_domain,
340 port=email_port,
341 username=email_username,
342 password=email_password,
343 requireAuthentication=email_starttls,
344 requireTransportSecurity=email_auth)
345 d_user.addCallbacks(lambda dummy: log.debug(u"Account creation confirmation sent to <%s>" % email), 345 d_user.addCallbacks(lambda dummy: log.debug(u"Account creation confirmation sent to <%s>" % email),
346 email_ko) 346 email_ko)
347 return defer.DeferredList([d_user, d_admin]) 347 return defer.DeferredList([d_user, d_admin])
348 348
349 def getNewAccountDomain(self): 349 def getNewAccountDomain(self):