def _generate_key(length=1024): # From: http://stackoverflow.com/questions/3504955/using-rsa-in-python try: from Crypto.PublicKey import RSA key = RSA.generate(length) return to_bytes(key.exportKey()), to_bytes(key.publickey().exportKey()) except ImportError: return PRIV_KEY, PUB_KEY
def __init__(self, selector, domain, privkey, ignore_sign_errors=True, **kwargs): self.ignore_sign_errors = ignore_sign_errors self._sign_params = kwargs if privkey and hasattr(privkey, 'read'): privkey = privkey.read() self._sign_params.update({'privkey':to_bytes(privkey), 'domain': to_bytes(domain), 'selector':to_bytes(selector)})
def sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]): if not to_addrs: return False if not isinstance(to_addrs, (list, tuple)): to_addrs = [to_addrs, ] #from_addr = sanitize_address(from_addr, email_message.encoding) #to_addrs = [sanitize_address(addr, email_message.encoding) for addr in to_addrs] #message = email_message.message() #charset = message.get_charset().get_output_charset() if message.get_charset() else 'utf-8' try: self.open() except (IOError, smtplib.SMTPException) as e: logger.exception("Error connecting smtp server") response = self.make_response(exception = e) if not self.fail_silently: response.raise_if_needed() return [response, ] _sendmail = self.retry_on_disconnect(self._sendmail_on_connection) response = _sendmail(from_addr=from_addr, to_addrs=to_addrs, msg=to_bytes(msg.as_string(), 'utf-8'), mail_options=mail_options, rcpt_options=rcpt_options) if not self.fail_silently: [ r.raise_if_needed() for r in response ] return response
def message(self): msg = self._build_message() if self._dkim_signer: msg_str = msg.as_string() dkim_header = self._dkim_signer.get_sign_header(to_bytes(msg_str)) if dkim_header: msg._headers.insert(0, dkim_header) return msg
def _check_dkim(message, pub_key=PUB_KEY): def _plain_public_key(s): return b"".join( [l for l in s.split(b'\n') if not l.startswith(b'---')]) message = message.as_string() o = emails.packages.dkim.DKIM(message=to_bytes(message)) return o.verify(dnsfunc=lambda name: b"".join( [b"v=DKIM1; p=", _plain_public_key(pub_key)]))
def as_string(self): # self.as_string() is not equialent self.message().as_string() # self.as_string() gets one less message-to-string conversions for dkim msg = self._build_message() r = msg.as_string() if self._dkim_signer: dkim_header = self._dkim_signer.get_sign(to_bytes(r)) if dkim_header: r = dkim_header + r return r
def mime(self): if self.content_disposition is None: return None _mime = getattr(self, "_cached_mime", None) if _mime is None: self._cached_mime = _mime = MIMEBase(*self.mime_type.split("/", 1)) _mime.set_payload(to_bytes(self.data)) encode_base64(_mime) _mime.add_header("Content-Disposition", self.content_disposition, filename=self.filename) if self.content_disposition == "inline": _mime.add_header("Content-ID", "<%s>" % self.filename) return _mime
def _check_dkim(message, pub_key=PUB_KEY): def _plain_public_key(s): return b"".join([l for l in s.split(b'\n') if not l.startswith(b'---')]) message = message.as_string() o = emails.packages.dkim.DKIM(message=to_bytes(message)) return o.verify(dnsfunc=lambda name: b"".join([b"v=DKIM1; p=", _plain_public_key(pub_key)]))