def urlencode(query, doseq=0): """ A version of Python's urllib.urlencode() function that can operate on unicode strings. The parameters are first case to UTF-8 encoded strings and then encoded as per normal. """ if hasattr(query, 'items'): query = query.items() return urllib.urlencode( [(smart_str(k), isinstance(v, (list, tuple)) and [smart_str(i) for i in v] or smart_str(v)) for k, v in query], doseq)
def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.quote(smart_str(url), safe))
def message(self): self._extend_recipients() self._perform_override() encoding = self.encoding or settings.default.charset msg = SafeMIMEText(smart_str(self.body, settings.default.charset), self.content_subtype, encoding) if self.attachments: body_msg = msg msg = SafeMIMEMultipart(_subtype=self.multipart_subtype) if self.body: msg.attach(body_msg) for attachment in self.attachments: if isinstance(attachment, MIMEBase): msg.attach(attachment) else: msg.attach(self._create_attachment(*attachment)) msg['Subject'] = self.subject if not self.from_email: raise SettingsError( 'email must have a from address or settings.emails.from_default must be set' ) msg['From'] = self.from_email if self.to: msg['To'] = ', '.join(self.to) if self.cc: msg['Cc'] = ', '.join(self.cc) # Email header names are case-insensitive (RFC 2045), so we have to # accommodate that when doing comparisons. header_names = [key.lower() for key in self.extra_headers] if 'date' not in header_names: msg['Date'] = formatdate() if 'message-id' not in header_names: msg['Message-ID'] = make_msgid() if self.reply_to: msg['Reply-To'] = self.reply_to for name, value in self.extra_headers.items(): msg[name] = value return msg
def _create_attachment(self, filename, content, mimetype=None): """ Converts the filename, content, mimetype triple into a MIME attachment object. """ if mimetype is None: mimetype, _ = mimetypes.guess_type(filename) if mimetype is None: mimetype = DEFAULT_ATTACHMENT_MIME_TYPE basetype, subtype = mimetype.split('/', 1) if basetype == 'text': attachment = SafeMIMEText( smart_str(content, settings.default.charset), subtype, settings.default.charset ) else: # Encode non-text attachments with base64. attachment = MIMEBase(basetype, subtype) attachment.set_payload(content) encoders.encode_base64(attachment) if filename: attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment
def _create_attachment(self, filename, content, mimetype=None): """ Converts the filename, content, mimetype triple into a MIME attachment object. """ if mimetype is None: mimetype, _ = mimetypes.guess_type(filename) if mimetype is None: mimetype = DEFAULT_ATTACHMENT_MIME_TYPE basetype, subtype = mimetype.split('/', 1) if basetype == 'text': attachment = SafeMIMEText( smart_str(content, settings.default.charset), subtype, settings.default.charset) else: # Encode non-text attachments with base64. attachment = MIMEBase(basetype, subtype) attachment.set_payload(content) encoders.encode_base64(attachment) if filename: attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment