(self)
return msg.as_string()
The message string.
The message string.
The message string .
def as_string(self):
        """The message string.
        """
        if self.date is None:
            self.date = time.time()

        if not self.html:
            if len(self.attachments) == 0:
                # plain text
                msg = MIMEText(self.body, 'plain', self.charset)
            elif len(self.attachments) > 0:
                # plain text with attachments
                msg = MIMEMultipart()
                msg.attach(MIMEText(self.body, 'plain', self.charset))
        else:
            msg = MIMEMultipart()
            alternative = MIMEMultipart('alternative')
            alternative.attach(MIMEText(self.body, 'plain', self.charset))
            alternative.attach(MIMEText(self.html, 'html', self.charset))
            msg.attach(alternative)

        msg['Subject'] = Header(self.subject, self.charset)
        msg['From'] = self.fromaddr
        msg['To'] = ', '.join(self.to)
        msg['Date'] = formatdate(self.date, localtime=True)
        msg['Message-ID'] = self.message_id
        if self.cc:
            msg['Cc'] = ', '.join(self.cc)
        if self.reply_to:
            msg['Reply-To'] = self.reply_to
        if self.extra_headers:
            for key, value in self.extra_headers.items():
                msg[key] = value

        for attachment in self.attachments:
            f = MIMEBase(*attachment.content_type.split('/'))
            f.set_payload(attachment.data)
            encode_base64(f)
            if attachment.filename is None:
                filename = str(None)
            else:
                filename = force_text(attachment.filename, self.charset)
            try:
                filename.encode('ascii')
            except UnicodeEncodeError:
                if PY2:
                    filename = filename.encode('utf-8')
                filename = ('UTF8', '', filename)
            f.add_header('Content-Disposition', attachment.disposition,
                         filename=filename)
            for key, value in attachment.headers.items():
                f.add_header(key, value)
            msg.attach(f)

        return msg.as_string()
def as_string ( self ) : if self . date is None : self . date = time . time ( ) if not self . html : if len ( self . attachments ) == 0 : # plain text msg = MIMEText ( self . body , 'plain' , self . charset ) elif len ( self . attachments ) > 0 : # plain text with attachments msg = MIMEMultipart ( ) msg . attach ( MIMEText ( self . body , 'plain' , self . charset ) ) else : msg = MIMEMultipart ( ) alternative = MIMEMultipart ( 'alternative' ) alternative . attach ( MIMEText ( self . body , 'plain' , self . charset ) ) alternative . attach ( MIMEText ( self . html , 'html' , self . charset ) ) msg . attach ( alternative ) msg [ 'Subject' ] = Header ( self . subject , self . charset ) msg [ 'From' ] = self . fromaddr msg [ 'To' ] = ', ' . join ( self . to ) msg [ 'Date' ] = formatdate ( self . date , localtime = True ) msg [ 'Message-ID' ] = self . message_id if self . cc : msg [ 'Cc' ] = ', ' . join ( self . cc ) if self . reply_to : msg [ 'Reply-To' ] = self . reply_to if self . extra_headers : for key , value in self . extra_headers . items ( ) : msg [ key ] = value for attachment in self . attachments : f = MIMEBase ( * attachment . content_type . split ( '/' ) ) f . set_payload ( attachment . data ) encode_base64 ( f ) if attachment . filename is None : filename = str ( None ) else : filename = force_text ( attachment . filename , self . charset ) try : filename . encode ( 'ascii' ) except UnicodeEncodeError : if PY2 : filename = filename . encode ( 'utf-8' ) filename = ( 'UTF8' , '' , filename ) f . add_header ( 'Content-Disposition' , attachment . disposition , filename = filename ) for key , value in attachment . headers . items ( ) : f . add_header ( key , value ) msg . attach ( f ) return msg . as_string ( )