<s> import json <EOL> from django . core . mail . backends . base import BaseEmailBackend <EOL> from django . core . mail . message import EmailMultiAlternatives <EOL> import requests <EOL> from django . conf import settings <EOL> class PostmarkMessage ( dict ) : <EOL> def __init__ ( self , message , fail_silently = True ) : <EOL> try : <EOL> message_dict = { } <EOL> message_dict [ "<STR_LIT>" ] = message . from_email <EOL> message_dict [ "<STR_LIT>" ] = unicode ( message . subject ) <EOL> message_dict [ "<STR_LIT>" ] = unicode ( message . body ) <EOL> if message . to : <EOL> message_dict [ "<STR_LIT>" ] = "<STR_LIT:U+002C>" . join ( message . to ) <EOL> if message . cc : <EOL> message_dict [ "<STR_LIT>" ] = "<STR_LIT:U+002C>" . join ( message . cc ) <EOL> if message . bcc : <EOL> message_dict [ "<STR_LIT>" ] = "<STR_LIT:U+002C>" . join ( message . bcc ) <EOL> if isinstance ( message , EmailMultiAlternatives ) : <EOL> for alt in message . alternatives : <EOL> if alt [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> message_dict [ "<STR_LIT>" ] = unicode ( alt [ <NUM_LIT:0> ] ) <EOL> if len ( message . extra_headers ) : <EOL> message_dict [ "<STR_LIT>" ] = [ { "<STR_LIT:Name>" : x [ <NUM_LIT:0> ] , "<STR_LIT>" : x [ <NUM_LIT:1> ] } for x in message . extra_headers . items ( ) ] <EOL> if message . attachments and isinstance ( message . attachments , list ) : <EOL> if len ( message . attachments ) : <EOL> message_dict [ "<STR_LIT>" ] = message . attachments <EOL> except : <EOL> if fail_silently : <EOL> message_dict = { } <EOL> else : <EOL> raise <EOL> super ( PostmarkMessage , self ) . __init__ ( message_dict ) <EOL> class EmailBackend ( BaseEmailBackend ) : <EOL> def send_messages ( self , email_messages ) : <EOL> if not email_messages : <EOL> return <EOL> num_sent = <NUM_LIT:0> <EOL> try : <EOL> for message in email_messages : <EOL> sent = self . _send ( PostmarkMessage ( message , self . fail_silently ) ) <EOL> if sent : <EOL> num_sent += <NUM_LIT:1> <EOL> except : <EOL> if self . fail_silently : <EOL> pass <EOL> else : <EOL> raise <EOL> return num_sent <EOL> def _send ( self , message ) : <EOL> requests . post ( settings . POSTMARK_API_URL , <EOL> data = json . dumps ( message ) , <EOL> headers = { <EOL> "<STR_LIT>" : "<STR_LIT:application/json>" , <EOL> "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" , <EOL> "<STR_LIT>" : settings . POSTMARK_TOKEN <EOL> } ) </s>
