<s> from email . MIMEBase import MIMEBase <EOL> from django . core . mail . backends . base import BaseEmailBackend <EOL> from django . utils . encoding import smart_unicode <EOL> from database_email_backend . models import Email , Attachment <EOL> class DatabaseEmailBackend ( BaseEmailBackend ) : <EOL> def send_messages ( self , email_messages ) : <EOL> for message in email_messages : <EOL> email = Email . objects . create ( <EOL> from_email = u'<STR_LIT:%s>' % message . from_email , <EOL> to_emails = u'<STR_LIT:U+002CU+0020>' . join ( message . to ) , <EOL> cc_emails = u'<STR_LIT:U+002CU+0020>' . join ( message . cc ) , <EOL> bcc_emails = u'<STR_LIT:U+002CU+0020>' . join ( message . bcc ) , <EOL> all_recipients = u'<STR_LIT:U+002CU+0020>' . join ( message . recipients ( ) ) , <EOL> subject = u'<STR_LIT:%s>' % message . subject , <EOL> body = u'<STR_LIT:%s>' % message . body , <EOL> raw = u'<STR_LIT:%s>' % smart_unicode ( message . message ( ) . as_string ( ) ) <EOL> ) <EOL> for attachment in message . attachments : <EOL> if isinstance ( attachment , tuple ) : <EOL> filename , content , mimetype = attachment <EOL> elif isinstance ( attachment , MIMEBase ) : <EOL> filename = attachment . get_filename ( ) <EOL> content = attachment . get_payload ( decode = True ) <EOL> mimetype = None <EOL> else : <EOL> continue <EOL> Attachment . objects . create ( <EOL> email = email , <EOL> filename = filename , <EOL> content = content , <EOL> mimetype = mimetype <EOL> ) <EOL> return len ( email_messages ) </s>
