Exemplo n.º 1
0
def send_mail(subject, body, addresslist, 
              sender = settings.DEFAULT_FROM_EMAIL,
              doAppendSig=True, bcclist = None, reply_to = None):
  """ wrapper for Django's send_mail method """
  # Always prepend prefix to the subject
  subject = ''.join(subject.splitlines()) # *must not* contain newlines
  subject = "%s %s" % (settings.EMAIL_SUBJECT_PREFIX, subject)
  body = unicode(body) # may come in as a django.utils.safestring.SafeUnicode

  # Always include a signature
  if doAppendSig:
    body = "%s\n%s" % (body, settings.EMAIL_SIGNATURE)

  logging.debug("mailer.send_mail: addresslist %s bcclist %s body:\n%s" %
                (addresslist, bcclist, body))

  email = GoogleEmailMessage(subject  = subject, 
                             body     = body, 
                             sender   = sender,
                             to       = addresslist)
  if bcclist is not None and len(bcclist) > 0:
    email.bcc = bcclist
  if reply_to is not None:
    email.reply_to = reply_to

  from tasks import deliver_email_task
  # Note: For some reason synchronous works, asynchronous isn't working.
  # I'm not going to worry about that for now.
  # TODO(dan): Fix deferred.defer.
  deliver_email_task(email)
  #deferred.defer(deliver_email_task, email)

  # for testing, appengine patch keeps a list of mails
  if hasattr(mail, 'outbox') and not on_production_server:
    mail.outbox.append(email) #@UndefinedVariable (ignore py checker warnings)
Exemplo n.º 2
0
    def translate_message(self, msg):
        sender = msg.get_unixfrom() or msg['From']
        if not sender:
            if self.fix_sender:
                sender = self.default_sender
            else:
                raise BadMessageError("No sender specified")
        to = msg['To']
        if not to:
            raise BadMessageError("No destination addresses specified")
        message = EmailMessage(sender=sender or msg['From'], to=to)
        # Go through all the headers which Google will let us use
        cc = msg['Cc']
        if cc:
            message.cc = cc
        bcc = msg['Bcc']
        if bcc:
            message.bcc = cc
        reply_to = msg['Reply-To']
        if reply_to:
            message.reply_to = reply_to
        subject = msg['Subject']
        if subject:
            message.subject = subject

        # If there's just a plain text body, use that, otherwise
        # iterate over all the attachments
        payload = msg.get_payload(decode=True)
        if isinstance(payload, basestring):
            message.body = payload
        else:
            body = ''
            html = ''
            attachments = []
            # GAE demands we specify the body explicitly - we use the first text/plain attachment we find.
            # Similarly, we pull in the first html we find and use that for message.html
            # We pull in any other attachments we find; but we ignore the multipart structure,
            # because GAE doesn't give us enough control there.
            for part in msg.walk():
                if part.get_content_type() == 'text/plain' and not body:
                    body = part.get_payload(decode=True)
                elif part.get_content_type() == 'text/html' and not html:
                    html = part.get_payload(decode=True)
                elif not part.get_content_type().startswith('multipart'):
                    attachments.append(
                        (get_filename(part), part.get_payload(decode=True)))
            if not body:
                raise BadMessageError("No message body specified")
            message.body = body
            if html:
                message.html = html
            if attachments:
                message.attachments = attachments
        return message
Exemplo n.º 3
0
    def translate_message(self, msg):
        sender = msg.get_unixfrom() or msg["From"]
        if not sender:
            if self.fix_sender:
                sender = self.default_sender
            else:
                raise BadMessageError("No sender specified")
        to = msg["To"]
        if not to:
            raise BadMessageError("No destination addresses specified")
        message = EmailMessage(sender=sender or msg["From"], to=to)
        # Go through all the headers which Google will let us use
        cc = msg["Cc"]
        if cc:
            message.cc = cc
        bcc = msg["Bcc"]
        if bcc:
            message.bcc = cc
        reply_to = msg["Reply-To"]
        if reply_to:
            message.reply_to = reply_to
        subject = msg["Subject"]
        if subject:
            message.subject = subject

        # If there's just a plain text body, use that, otherwise
        # iterate over all the attachments
        payload = msg.get_payload(decode=True)
        if isinstance(payload, basestring):
            message.body = payload
        else:
            body = ""
            html = ""
            attachments = []
            # GAE demands we specify the body explicitly - we use the first text/plain attachment we find.
            # Similarly, we pull in the first html we find and use that for message.html
            # We pull in any other attachments we find; but we ignore the multipart structure,
            # because GAE doesn't give us enough control there.
            for part in msg.walk():
                if part.get_content_type() == "text/plain" and not body:
                    body = part.get_payload(decode=True)
                elif part.get_content_type() == "text/html" and not html:
                    html = part.get_payload(decode=True)
                elif not part.get_content_type().startswith("multipart"):
                    attachments.append((get_filename(part), part.get_payload(decode=True)))
            if not body:
                raise BadMessageError("No message body specified")
            message.body = body
            if html:
                message.html = html
            if attachments:
                message.attachments = attachments
        return message
Exemplo n.º 4
0
def _gae_send_mail(sender, to, cc=None, bcc=None, reply_to=None, subject='', body='', html='', attachments=[], headers={}):
    email = EmailMessage()
    email.sender = sender
    email.to = to
    email.subject = subject
    email.body = body
    if cc: email.cc = cc
    if bcc: email.bcc = bcc
    if reply_to: email.reply_to = reply_to
    if html: email.html = html
    if attachments: email.attachments = attachments
    if headers: email.headers = headers
    
    return email.send()
Exemplo n.º 5
0
def _gae_send_mail(sender, to, cc=None, bcc=None, reply_to=None, subject='', body='', html='', attachments=[], headers={}):
    email = EmailMessage()
    email.sender = sender
    email.to = to
    email.subject = subject
    email.body = body
    if cc: email.cc = cc
    if bcc: email.bcc = bcc
    if reply_to: email.reply_to = reply_to
    if html: email.html = html
    if attachments: email.attachments = attachments
    if headers: email.headers = headers
    
    return email.send()
Exemplo n.º 6
0
    def send(self):

        """
        Build and send an email message from this model instance
        """

        # create new email message
        new_email = EmailMessage()

        # set sender address
        new_email.sender = self.get_sender()

        # set to addresses
        new_email.to = self.to

        # set cc addresses
        if self.cc:
            new_email.cc = self.cc

        # set bcc addresses
        if self.bcc:
            new_email.bcc = self.bcc

        # set reply to address
        if self.reply_to:
            new_email.reply_to = self.reply_to
        else:
            new_email.reply_to = self.get_sender()

        # set email subject
        new_email.subject = self.subject

        # set plaintext body
        if self.body_plain:
            new_email.body = self.body_plain

        # set html body
        if self.body_html:
            new_email.html = self.body_html

        # check that the email has been properly initialized and send
        new_email.check_initialized()
        new_email.send()