def prepare_email_message(self):
        plain = self.plain
        if not plain:
            return None

        # We definitely want unicode at this point.
        plain = su(plain)

        # We must choose the body charset manually.  Note that the
        # goal and effect of this loop is to determine the
        # body_charset.
        for body_charset in "US-ASCII", get_charset(), "UTF-8":
            try:
                plain.encode(body_charset)
            except UnicodeEncodeError:
                pass
            else:
                break
            # Encoding should work now; let's replace errors just in case.
        plain = plain.encode(body_charset, "replace")

        text_part = MIMEText(plain, "plain", body_charset)

        email_msg = MIMEMultipart("alternative")
        email_msg.epilogue = ""
        email_msg.attach(text_part)
        return email_msg
예제 #2
0
def simple_send_mail(message, addresses, subject, immediate=True):
    """Send a notification email to the list of addresses.

    The method is called 'simple' because all the clever stuff should
    already have been done by the caller.

    message is passed without change to the mail host.  It should
    probably be a correctly encoded Message or MIMEText.

    One mail with the given message and subject is sent for each address.

    Note that with Plone 4 (Zope 2.12) by default the sending is
    deferred to the end of the transaction.  This means an exception
    would roll back the transaction.  We usually do not want that, as
    the email sending is an extra: we do not mind too much if sending
    fails.  Luckily we have the option to send immediately, so we can
    catch and ignore exceptions.  In this method we do that.  You can
    override that by passing immediate=False.  Note that in Plone 3
    this has no effect at all.
    """
    mail_host = utils.get_mail_host()
    if mail_host is None:
        logger.warn("Cannot send notification email: please configure "
                    "MailHost correctly.")
        # We print some info, which is perfect for checking in unit
        # tests.
        print 'Subject =', subject
        print 'Addresses =', addresses
        print 'Message ='
        print message
        return

    mfrom = utils.get_mail_from_address()
    header_charset = utils.get_charset()

    for address in addresses:
        if not address:
            continue
        try:
            if USE_SECURE_SEND:
                mail_host.secureSend(message=message,
                                     mto=address,
                                     mfrom=mfrom,
                                     subject=subject,
                                     charset=header_charset)
            else:
                mail_host.send(message,
                               mto=address,
                               mfrom=mfrom,
                               subject=subject,
                               immediate=immediate,
                               charset=header_charset)
        except (socket.error, SMTPException, MailHostError):
            logger.warn('Could not send email to %s with subject %s',
                        address, subject)
        except:
            raise
예제 #3
0
def simple_send_mail(message, addresses, subject, immediate=False):
    """Send a notification email to the list of addresses.

    The method is called 'simple' because all the clever stuff should
    already have been done by the caller.

    message is passed without change to the mail host.  It should
    probably be a correctly encoded Message or MIMEText.

    One mail with the given message and subject is sent for each address.

    Starting with Plone 4 (Zope 2.12) by default the sending is deferred
    to the end of the transaction.  It seemed that this would mean that
    an exception during sending would roll back the transaction, so we
    passed immediate=True by default, catching the error and continuing.

    But this is not the case: Products/CMFPlone/patches/sendmail.py
    patches the email sending to not raise an error when the transaction
    is already finished.  So in case of problems the transaction is not
    rolled back.  (zope.sendmail 4.0 does this itself.)

    And that is fine for us: usually a problem with sending the email
    should not result in a transaction rollback or an error for the
    user.

    There is still the option to send immediately.  If you want this,
    you can pass immediate=False to this function.
    """
    mail_host = utils.get_mail_host()
    if mail_host is None:
        logger.warn("Cannot send notification email: please configure "
                    "MailHost correctly.")
        # We print some info, which is perfect for checking in unit
        # tests.
        print 'Subject =', subject
        print 'Addresses =', addresses
        print 'Message ='
        print message
        return

    mfrom = utils.get_mail_from_address()
    header_charset = utils.get_charset()

    for address in addresses:
        if not address:
            continue
        mail_host.send(
            message,
            mto=address,
            mfrom=mfrom,
            subject=subject,
            immediate=immediate,
            charset=header_charset)
예제 #4
0
def simple_send_mail(message, addresses, subject, immediate=False):
    """Send a notification email to the list of addresses.

    The method is called 'simple' because all the clever stuff should
    already have been done by the caller.

    message is passed without change to the mail host.  It should
    probably be a correctly encoded Message or MIMEText.

    One mail with the given message and subject is sent for each address.

    Starting with Plone 4 (Zope 2.12) by default the sending is deferred
    to the end of the transaction.  It seemed that this would mean that
    an exception during sending would roll back the transaction, so we
    passed immediate=True by default, catching the error and continuing.

    But this is not the case: Products/CMFPlone/patches/sendmail.py
    patches the email sending to not raise an error when the transaction
    is already finished.  So in case of problems the transaction is not
    rolled back.  (zope.sendmail 4.0 does this itself.)

    And that is fine for us: usually a problem with sending the email
    should not result in a transaction rollback or an error for the
    user.

    There is still the option to send immediately.  If you want this,
    you can pass immediate=False to this function.
    """
    mail_host = utils.get_mail_host()
    if mail_host is None:
        logger.warning("Cannot send notification email: please configure "
                       "MailHost correctly.")
        # We print some info, which is perfect for checking in unit
        # tests.
        print('Subject =', subject)
        print('Addresses =', addresses)
        print('Message =')
        print(message)
        return

    mfrom = utils.get_mail_from_address()
    header_charset = utils.get_charset()

    for address in addresses:
        if not address:
            continue
        mail_host.send(message,
                       mto=address,
                       mfrom=mfrom,
                       subject=subject,
                       immediate=immediate,
                       charset=header_charset)
예제 #5
0
def simple_send_mail(message, addresses, subject, immediate=True):
    """Send a notification email to the list of addresses.

    The method is called 'simple' because all the clever stuff should
    already have been done by the caller.

    message is passed without change to the mail host.  It should
    probably be a correctly encoded Message or MIMEText.

    One mail with the given message and subject is sent for each address.

    Note that with Plone 4 (Zope 2.12) by default the sending is
    deferred to the end of the transaction.  This means an exception
    would roll back the transaction.  We usually do not want that, as
    the email sending is an extra: we do not mind too much if sending
    fails.  Luckily we have the option to send immediately, so we can
    catch and ignore exceptions.  In this method we do that.  You can
    override that by passing immediate=False.  Note that in Plone 3
    this has no effect at all.
    """
    mail_host = utils.get_mail_host()
    if mail_host is None:
        logger.warn("Cannot send notification email: please configure "
                    "MailHost correctly.")
        # We print some info, which is perfect for checking in unit
        # tests.
        print 'Subject =', subject
        print 'Addresses =', addresses
        print 'Message ='
        print message
        return

    mfrom = utils.get_mail_from_address()
    header_charset = utils.get_charset()

    for address in addresses:
        if not address:
            continue
        try:
            mail_host.send(message,
                           mto=address,
                           mfrom=mfrom,
                           subject=subject,
                           immediate=immediate,
                           charset=header_charset)
        except (socket.error, SMTPException, MailHostError):
            logger.warn('Could not send email to %s with subject %s', address,
                        subject)
        except:
            raise
예제 #6
0
 def plain2rst(self):
     """Try to interpret the plain text as reStructuredText.
     """
     charset = get_charset()
     rstText = self.plain
     ignored, warnings = rst.render(
         rstText, input_encoding=charset, output_encoding=charset)
     if len(warnings.messages) == 0:
         body = rst.HTML(
             rstText, input_encoding=charset, output_encoding=charset)
     else:
         # There are warnings, so we keep it simple.
         body = '<pre>%s</pre>' % rstText
     return body
예제 #7
0
 def plain2rst(self):
     """Try to interpret the plain text as reStructuredText.
     """
     charset = get_charset()
     rstText = self.plain
     ignored, warnings = rst.render(rstText,
                                    input_encoding=charset,
                                    output_encoding=charset)
     if len(warnings.messages) == 0:
         body = rst.HTML(rstText,
                         input_encoding=charset,
                         output_encoding=charset)
     else:
         # There are warnings, so we keep it simple.
         body = '<pre>%s</pre>' % rstText
     return body
예제 #8
0
파일: browser.py 프로젝트: CGTIC/Plone_SP
    def prepare_email_message(self):
        plain = self.plain
        html = self.html
        if not plain and not html:
            return None

        # We definitely want unicode at this point.
        plain = utils.su(plain)
        html = utils.su(html)

        # We must choose the body charset manually.  Note that the
        # goal and effect of this loop is to determine the
        # body_charset.
        for body_charset in 'US-ASCII', utils.get_charset(), 'UTF-8':
            try:
                plain.encode(body_charset)
                html.encode(body_charset)
            except UnicodeEncodeError:
                pass
            else:
                break
        # Encoding should work now; let's replace errors just in case.
        plain = plain.encode(body_charset, 'replace')
        html = html.encode(body_charset, 'xmlcharrefreplace')

        text_part = MIMEText(plain, 'plain', body_charset)
        html_part = MIMEText(html, 'html', body_charset)

        # No sense in sending plain text and html when we only have
        # one of those:
        if not plain:
            return html_part
        if not html:
            return text_part

        # Okay, we send both plain text and html
        email_msg = MIMEMultipart('alternative')
        email_msg.epilogue = ''
        email_msg.attach(text_part)
        email_msg.attach(html_part)
        return email_msg
예제 #9
0
    def prepare_email_message(self):
        plain = self.plain
        html = self.html
        if not plain and not html:
            return None

        # We definitely want unicode at this point.
        plain = utils.su(plain)
        html = utils.su(html)

        # We must choose the body charset manually.  Note that the
        # goal and effect of this loop is to determine the
        # body_charset.
        for body_charset in 'US-ASCII', utils.get_charset(), 'UTF-8':
            try:
                plain.encode(body_charset)
                html.encode(body_charset)
            except UnicodeEncodeError:
                pass
            else:
                break
        # Encoding should work now; let's replace errors just in case.
        plain = plain.encode(body_charset, 'replace')
        html = html.encode(body_charset, 'xmlcharrefreplace')

        text_part = MIMEText(plain, 'plain', body_charset)
        html_part = MIMEText(html, 'html', body_charset)

        # No sense in sending plain text and html when we only have
        # one of those:
        if not plain:
            return html_part
        if not html:
            return text_part

        # Okay, we send both plain text and html
        email_msg = MIMEMultipart('alternative')
        email_msg.epilogue = ''
        email_msg.attach(text_part)
        email_msg.attach(html_part)
        return email_msg