mailflash.py
def as_string(self, default_from=None):
        """Creates the email"""

        encoding = self.charset or 'utf-8'

        attachments = self.attachments or []

        if len(attachments) == 0 and not self.html:
            # No html content and zero attachments means plain text
            msg = self._mimetext(self.body)
        elif len(attachments) > 0 and not self.html:
            # No html and at least one attachment means multipart
            msg = MIMEMultipart()
            msg.attach(self._mimetext(self.body))
        else:
            # Anything else
            msg = MIMEMultipart()
            alternative = MIMEMultipart('alternative')
            alternative.attach(self._mimetext(self.body, 'plain'))
            alternative.attach(self._mimetext(self.html, 'html'))
            msg.attach(alternative)

        if self.charset:
            msg['Subject'] = Header(self.subject, encoding)
        else:
            msg['Subject'] = self.subject

        sender = self.sender or default_from
        if sender is not None:
            msg['From'] = sanitize_address(sender, encoding)
        msg['To'] = ', '.join(list(set(sanitize_addresses(self.recipients, encoding))))

        msg['Date'] = formatdate(self.date, localtime=True)
        # see RFC 5322 section 3.6.4.
        msg['Message-ID'] = self.msgId

        if self.cc:
            msg['Cc'] = ', '.join(list(set(sanitize_addresses(self.cc, encoding))))

        if self.reply_to:
            msg['Reply-To'] = sanitize_address(self.reply_to, encoding)

        if self.extra_headers:
            for k, v in self.extra_headers.items():
                msg[k] = v

        for attachment in attachments:
            f = MIMEBase(*attachment.content_type.split('/'))
            f.set_payload(attachment.data)
            encode_base64(f)

            try:
                attachment.filename and attachment.filename.encode('ascii')
            except UnicodeEncodeError:
                filename = attachment.filename
                if not PY3:
                    filename = filename.encode('utf8')
                f.add_header('Content-Disposition', attachment.disposition,
                            filename=('UTF8', '', filename))
            else:
                f.add_header('Content-Disposition', '%s;filename=%s' %
                             (attachment.disposition, attachment.filename))

            for key, value in attachment.headers:
                f.add_header(key, value)

            msg.attach(f)

        return msg.as_string()
def as_string(self, default_from=None):
        encoding = self.charset or 'utf-8'

        attachments = self.attachments or []

        if len(attachments) == 0 and not self.html:
            # No html content and zero attachments means plain text
            msg = self._mimetext(self.body)
        elif len(attachments) > 0 and not self.html:
            # No html and at least one attachment means multipart
            msg = MIMEMultipart()
            msg.attach(self._mimetext(self.body))
        else:
            # Anything else
            msg = MIMEMultipart()
            alternative = MIMEMultipart('alternative')
            alternative.attach(self._mimetext(self.body, 'plain'))
            alternative.attach(self._mimetext(self.html, 'html'))
            msg.attach(alternative)

        if self.charset:
            msg['Subject'] = Header(self.subject, encoding)
        else:
            msg['Subject'] = self.subject

        sender = self.sender or default_from
        if sender is not None:
            msg['From'] = sanitize_address(sender, encoding)
        msg['To'] = ', '.join(list(set(sanitize_addresses(self.recipients, encoding))))

        msg['Date'] = formatdate(self.date, localtime=True)
        # see RFC 5322 section 3.6.4.
        msg['Message-ID'] = self.msgId

        if self.cc:
            msg['Cc'] = ', '.join(list(set(sanitize_addresses(self.cc, encoding))))

        if self.reply_to:
            msg['Reply-To'] = sanitize_address(self.reply_to, encoding)

        if self.extra_headers:
            for k, v in self.extra_headers.items():
                msg[k] = v

        for attachment in attachments:
            f = MIMEBase(*attachment.content_type.split('/'))
            f.set_payload(attachment.data)
            encode_base64(f)

            try:
                attachment.filename and attachment.filename.encode('ascii')
            except UnicodeEncodeError:
                filename = attachment.filename
                if not PY3:
                    filename = filename.encode('utf8')
                f.add_header('Content-Disposition', attachment.disposition,
                            filename=('UTF8', '', filename))
            else:
                f.add_header('Content-Disposition', '%s;filename=%s' %
                             (attachment.disposition, attachment.filename))

            for key, value in attachment.headers:
                f.add_header(key, value)

            msg.attach(f)

        return msg.as_string()
def as_string ( self , default_from = None ) : encoding = self . charset or 'utf-8' attachments = self . attachments or [ ] if len ( attachments ) == 0 and not self . html : # No html content and zero attachments means plain text msg = self . _mimetext ( self . body ) elif len ( attachments ) > 0 and not self . html : # No html and at least one attachment means multipart msg = MIMEMultipart ( ) msg . attach ( self . _mimetext ( self . body ) ) else : # Anything else msg = MIMEMultipart ( ) alternative = MIMEMultipart ( 'alternative' ) alternative . attach ( self . _mimetext ( self . body , 'plain' ) ) alternative . attach ( self . _mimetext ( self . html , 'html' ) ) msg . attach ( alternative ) if self . charset : msg [ 'Subject' ] = Header ( self . subject , encoding ) else : msg [ 'Subject' ] = self . subject sender = self . sender or default_from if sender is not None : msg [ 'From' ] = sanitize_address ( sender , encoding ) msg [ 'To' ] = ', ' . join ( list ( set ( sanitize_addresses ( self . recipients , encoding ) ) ) ) msg [ 'Date' ] = formatdate ( self . date , localtime = True ) # see RFC 5322 section 3.6.4. msg [ 'Message-ID' ] = self . msgId if self . cc : msg [ 'Cc' ] = ', ' . join ( list ( set ( sanitize_addresses ( self . cc , encoding ) ) ) ) if self . reply_to : msg [ 'Reply-To' ] = sanitize_address ( self . reply_to , encoding ) if self . extra_headers : for k , v in self . extra_headers . items ( ) : msg [ k ] = v for attachment in attachments : f = MIMEBase ( * attachment . content_type . split ( '/' ) ) f . set_payload ( attachment . data ) encode_base64 ( f ) try : attachment . filename and attachment . filename . encode ( 'ascii' ) except UnicodeEncodeError : filename = attachment . filename if not PY3 : filename = filename . encode ( 'utf8' ) f . add_header ( 'Content-Disposition' , attachment . disposition , filename = ( 'UTF8' , '' , filename ) ) else : f . add_header ( 'Content-Disposition' , '%s;filename=%s' % ( attachment . disposition , attachment . filename ) ) for key , value in attachment . headers : f . add_header ( key , value ) msg . attach ( f ) return msg . as_string ( )
Creates the email
Creates the email