#NEWLINE# def deliver( self , message ) : #NEWLINE# #INDENT# to = message . to #NEWLINE# if message . cc : #NEWLINE# #INDENT# to . extend ( message . cc ) #NEWLINE# #UNINDENT# args = dict ( { #NEWLINE# #INDENT# 'api_user' : self . user , #NEWLINE# 'api_key' : self . key , #NEWLINE# 'from' : [ fromaddr . address . encode ( message . encoding ) for fromaddr in message . author ] , #NEWLINE# 'fromname' : [ fromaddr . name . encode ( message . encoding ) for fromaddr in message . author ] , #NEWLINE# 'to' : [ toaddr . address . encode ( message . encoding ) for toaddr in to ] , #NEWLINE# 'toname' : [ toaddr . name . encode ( message . encoding ) for toaddr in to ] , #NEWLINE# 'subject' : message . subject . encode ( message . encoding ) , #NEWLINE# 'text' : message . plain . encode ( message . encoding ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# if message . bcc : #NEWLINE# #INDENT# args [ 'bcc' ] = [ bcc . address . encode ( message . encoding ) for bcc in message . bcc ] #NEWLINE# #UNINDENT# if message . reply : #NEWLINE# #INDENT# args [ 'replyto' ] = message . reply . encode ( message . encoding ) #NEWLINE# #UNINDENT# if message . rich : #NEWLINE# #INDENT# args [ 'html' ] = message . rich . encode ( message . encoding ) #NEWLINE# #UNINDENT# if message . attachments : #NEWLINE# #INDENT# "\n            attachments = []\n            \n            for attachment in message.attachments:\n                attachments.append((\n                        attachment['Content-Disposition'].partition(';')[2],\n                        attachment.get_payload(True)\n                    ))\n            \n            msg.attachments = attachments\n            " #NEWLINE# raise MailConfigurationException ( ) #NEWLINE# #UNINDENT# request = urllib2 . Request ( 'https://sendgrid.com/api/mail.send.json' , urllib . urlencode ( args , True ) ) #NEWLINE# try : #NEWLINE# #INDENT# response = urllib2 . urlopen ( request ) #NEWLINE# #UNINDENT# except ( urllib2 . HTTPError , urllib2 . URLError ) : #NEWLINE# #INDENT# raise DeliveryFailedException ( message , 'Could not connect to Sendgrid.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# respcode = response . getcode ( ) #NEWLINE# if ( ( respcode >= 400 ) and ( respcode <= 499 ) ) : #NEWLINE# #INDENT# raise MessageFailedException ( response . read ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( respcode >= 500 ) and ( respcode <= 599 ) ) : #NEWLINE# #INDENT# raise DeliveryFailedException ( message , 'Sendgrid service unavailable.' ) #NEWLINE# #UNINDENT# #UNINDENT# response . close ( )