Beispiel #1
0
def create_message(sender, recipient, subject, body, attachments=None):

    from email.header import Header
    from email.mime.base import MIMEBase as BaseMessage
    from email.mime.multipart import MIMEMultipart as MultipartMessage
    from email.utils import formatdate

    if sys.hexversion < 0x3000000:
        sender = _unicode_encode(sender,
                                 encoding=_encodings['content'],
                                 errors='strict')
        recipient = _unicode_encode(recipient,
                                    encoding=_encodings['content'],
                                    errors='strict')
        subject = _unicode_encode(subject,
                                  encoding=_encodings['content'],
                                  errors='backslashreplace')
        body = _unicode_encode(body,
                               encoding=_encodings['content'],
                               errors='backslashreplace')

    if attachments == None:
        mymessage = TextMessage(body)
    else:
        mymessage = MultipartMessage()
        mymessage.attach(TextMessage(body))
        for x in attachments:
            if isinstance(x, BaseMessage):
                mymessage.attach(x)
            elif isinstance(x, basestring):
                if sys.hexversion < 0x3000000:
                    x = _unicode_encode(x,
                                        encoding=_encodings['content'],
                                        errors='backslashreplace')
                mymessage.attach(TextMessage(x))
            else:
                raise portage.exception.PortageException(
                    _("Can't handle type of attachment: %s") % type(x))

    mymessage.set_unixfrom(sender)
    mymessage["To"] = recipient
    mymessage["From"] = sender

    # Use Header as a workaround so that long subject lines are wrapped
    # correctly by <=python-2.6 (gentoo bug #263370, python issue #1974).
    # Also, need to force ascii for python3, in order to avoid
    # UnicodeEncodeError with non-ascii characters:
    #  File "/usr/lib/python3.1/email/header.py", line 189, in __init__
    #    self.append(s, charset, errors)
    #  File "/usr/lib/python3.1/email/header.py", line 262, in append
    #    input_bytes = s.encode(input_charset, errors)
    #UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)
    mymessage["Subject"] = Header(_force_ascii_if_necessary(subject))
    mymessage["Date"] = formatdate(localtime=True)

    return mymessage
Beispiel #2
0
def create_message(sender, recipient, subject, body, attachments=None):

    from email.header import Header
    from email.mime.base import MIMEBase as BaseMessage
    from email.mime.multipart import MIMEMultipart as MultipartMessage

    if sys.hexversion < 0x3000000:
        sender = _unicode_encode(sender,
                                 encoding=_encodings['content'],
                                 errors='strict')
        recipient = _unicode_encode(recipient,
                                    encoding=_encodings['content'],
                                    errors='strict')
        subject = _unicode_encode(subject,
                                  encoding=_encodings['content'],
                                  errors='backslashreplace')
        body = _unicode_encode(body,
                               encoding=_encodings['content'],
                               errors='backslashreplace')

    if attachments == None:
        mymessage = TextMessage(body)
    else:
        mymessage = MultipartMessage()
        mymessage.attach(TextMessage(body))
        for x in attachments:
            if isinstance(x, BaseMessage):
                mymessage.attach(x)
            elif isinstance(x, basestring):
                if sys.hexversion < 0x3000000:
                    x = _unicode_encode(x,
                                        encoding=_encodings['content'],
                                        errors='backslashreplace')
                mymessage.attach(TextMessage(x))
            else:
                raise portage.exception.PortageException(
                    _("Can't handle type of attachment: %s") % type(x))

    mymessage.set_unixfrom(sender)
    mymessage["To"] = recipient
    mymessage["From"] = sender
    # Use Header as a workaround so that long subject lines are wrapped
    # correctly by <=python-2.6 (gentoo bug #263370, python issue #1974).
    mymessage["Subject"] = Header(subject)
    mymessage["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S %z")

    return mymessage