Ejemplo n.º 1
0
    def _send(self, message):
        """A helper method that does the actual sending."""
        if not message.recipients():
            return False
        try:
            recipients = ','.join(message.to)

            html_body = None
            if isinstance(message, EmailMultiAlternatives):
                for alt in message.alternatives:
                    if alt[1] == "text/html":
                        html_body = alt[0]
                        break

            reply_to = None
            custom_headers = {}
            if message.extra_headers and isinstance(message.extra_headers,
                                                    dict):
                if message.extra_headers.has_key('Reply-To'):
                    reply_to = message.extra_headers.pop('Reply-To')
                if len(message.extra_headers):
                    custom_headers = message.extra_headers
            attachments = []
            if message.attachments and isinstance(message.attachments, list):
                if len(message.attachments):
                    attachments = message.attachments

            postmark_message = PMMail(api_key=self.api_key,
                                      subject=message.subject,
                                      sender=message.from_email,
                                      to=recipients,
                                      text_body=message.body,
                                      html_body=html_body,
                                      reply_to=reply_to,
                                      custom_headers=custom_headers,
                                      attachments=attachments)

            postmark_message.tag = getattr(message, 'tag', None)

            postmark_message.send(test=self.test_mode)
        except:
            if self.fail_silently:
                return False
            raise
        return True
    def _send(self, message):
        """A helper method that does the actual sending."""
        if not message.recipients():
            return False            
        try:
            recipients = ','.join(message.to)
            
            html_body = None
            no_text = False
            if message.__class__.__name__ == 'EmailMultiAlternatives':
                for alt in message.alternatives:
                    if alt[1] == "text/html":
                        html_body=alt[0]
                        break
            elif message.content_subtype == "html":
                html_body = message.body
                no_text = True
            
            reply_to = None
            custom_headers = {}           
            if message.extra_headers and isinstance(message.extra_headers, dict):
                if message.extra_headers.has_key('Reply-To'):
                    reply_to = message.extra_headers.pop('Reply-To')
                if len(message.extra_headers):
                    custom_headers = message.extra_headers
            
            postmark_message = PMMail(api_key=self.api_key, 
                                  subject=message.subject,
                                  sender=message.from_email,
                                  to=recipients,
                                  text_body=None if no_text else message.body,
                                  html_body=html_body,
                                  reply_to=reply_to,
                                  custom_headers=custom_headers)

            postmark_message.send()
        except:
            if self.fail_silently:
                return False
            raise
        return True
Ejemplo n.º 3
0
 def _build_message(self, message):
     """A helper method to convert a PMEmailMessage to a PMMail"""
     if not message.recipients():
         return False            
     recipients = ','.join(message.to)
     recipients_bcc = ','.join(message.bcc)
     
     html_body = None
     if isinstance(message, EmailMultiAlternatives):
         for alt in message.alternatives:
             if alt[1] == "text/html":
                 html_body=alt[0]
                 break
     
     reply_to = None
     custom_headers = {}           
     if message.extra_headers and isinstance(message.extra_headers, dict):
         if message.extra_headers.has_key('Reply-To'):
             reply_to = message.extra_headers.pop('Reply-To')
         if len(message.extra_headers):
             custom_headers = message.extra_headers
     attachments = []
     if message.attachments and isinstance(message.attachments, list):
         if len(message.attachments):
             attachments = message.attachments
     
     postmark_message = PMMail(api_key=self.api_key, 
                           subject=message.subject,
                           sender=message.from_email,
                           to=recipients,
                           bcc=recipients_bcc,
                           text_body=message.body,
                           html_body=html_body,
                           reply_to=reply_to,
                           custom_headers=custom_headers,
                           attachments=attachments)
     
     postmark_message.tag = getattr(message, 'tag', None)
     return postmark_message