def verify_pass(self): '''Verify the password we got works for SMTPAUTH.''' try: smtp = smtplib.SMTP(self.host, self.port) except socket.error as msg: raise MailSendError(msg) # NOTE WELL: SECURITY IMPORTANT NOTE! # In python 2.6 if you attempt to starttls() and the server doesn't # understand an exception is raised. However before that, it just carried on # and one could attempt to auth over a plain-text session. This is BAD! # # So, in order be secure on older pythons we ehlo() and then check the # response before attempting startls. try: smtp.ehlo() if not smtp.has_extn('STARTTLS'): # Emulate 2.6 behavior raise smtplib.SMTPException('Server does not support STARTTLS') smtp.starttls() # must ehlo after startls smtp.ehlo() smtp.login(self.user, self.password) except smtplib.SMTPAuthenticationError: return False except (smtplib.SMTPException, socket.error) as msg: raise MailSendError(msg) finally: smtp.quit() return True
def _send_mail(self, to, msg): '''Given a to and Message object, send email.''' # We don't duplicate the header logic in the sub functions, we # do that here debug("send_mail called with to (%s), subject (%s)" % (to, msg['subject'])) if self.display_name: msg['From'] = self.display_name + ' <' + self.mail + '>' else: msg['From'] = self.mail if self.address_override: msg['To'] = self.address_override else: msg['To'] = to msg['Date'] = formatdate(localtime=True) try: smtp = smtplib.SMTP(self.host, self.port) if self.tls: # NOTE WELL: SECURITY IMPORTANT NOTE! # In python 2.6 if you attempt to starttls() and the server doesn't # understand an exception is raised. However before that, it just # carried on and one could attempt to auth over a plain-text session. # This is BAD! # # So, in order be secure on older pythons we ehlo() and then check the # response before attempting startls. smtp.ehlo() if not smtp.has_extn('STARTTLS'): # Emulate 2.6 behavior raise smtplib.SMTPException( 'Server does not support STARTTLS') smtp.starttls() # must re-ehlo after STARTTLS smtp.ehlo() # Don't want to send auth information unless we're TLS'd if self.user: smtp.login(self.user, self.password) if self.address_override: env_to = self.address_override else: # BCC the user... env_to = [msg['To'], self.mail] smtp.sendmail(self.mail, env_to, msg.as_string()) smtp.quit() except smtplib.SMTPException as emsg: raise MailSendError(emsg) except socket.error as emsg: raise MailSendError(emsg)
def send_sig_mail(self, signer, keyid, uid_data, psign): '''Send the encrypted uid off to the user.''' try: if self.no_pgp_mime: msg = self._generate_non_pgp_mime_email( signer, uid_data['email'], keyid, uid_data['enc_file']) else: msg = self._generate_pgp_mime_email(signer, uid_data['email'], keyid, uid_data['file'], psign) except EncryptionKeyError: msg = ('Failed to generate the email to the user. This is most' ' likely due to the user having no encryption subkey.') raise MailSendError(msg) msg['Subject'] = 'Your signed PGP key' self._send_mail(uid_data['email'], msg)