Email attachments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Holden

    #1

    Email attachments

    I'm having some trouble getting attachments right for all recipients,
    and it seems like Apple's mail.app is the pickiest client at the moment.
    It doesn't handle attachments that both Thunderbird and Outlook find
    perfectly acceptable.

    Since the code I'm using is currently ugly and embedded, before I trim
    it down for posting could anyone who's successfully generated emails
    with attachments received by mail.app let me know if they had any
    problems along the way? Google hasn't given me much on this, but I'm not
    feeling very creative today.

    Note that this is an important application: it sends out the invoices
    for PyCon sponsorship!

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com

  • Max M

    #2
    Re: Email attachments

    Steve Holden skrev:
    I'm having some trouble getting attachments right for all recipients,
    and it seems like Apple's mail.app is the pickiest client at the moment.
    It doesn't handle attachments that both Thunderbird and Outlook find
    perfectly acceptable.
    >
    Since the code I'm using is currently ugly and embedded, before I trim
    it down for posting could anyone who's successfully generated emails
    with attachments received by mail.app let me know if they had any
    problems along the way? Google hasn't given me much on this, but I'm not
    feeling very creative today.

    This method is used in one of my Plone products, and there is also a bit
    of imap in there. But it is probably simple enough to extract the
    attachment relevant parts....


    security.declar eProtected(EDIT _CONTENTS_PERMI SSION, 'sendMessage')
    def sendMessage(sel f, to=None, cc=None, bcc=None, inReplyTo=None,
    subject=None, body='', attachments=Non e, mode=None,
    folderPath=None , uid=None, REQUEST=None):
    "Sends a message"
    site_encoding = self._site_enco ding()
    ############### ###
    # Create the message
    msg = Message()
    msg.set_payload (body, site_encoding)
    ############### ############### #######
    # if attachment, convert to multipart
    # file fields are posted even if empty, so we need to remove
    those :-s
    if attachments is None:
    attachments = []
    attachments = [a for a in attachments if a]
    if attachments or (mode == 'Forward'):
    mimeMsg = MIMEMultipart()
    mimeMsg.attach( msg)
    # If mode is Forward we get original message as attachment
    if mode == 'Forward' and folderPath and uid:
    original = self.fetchMessa geFromPath(fold erPath, uid)
    mimeMsg.attach( original)
    for attachment in attachments:
    # Add the attachment
    tmp = email.message_f rom_string(str( attachment.head ers))
    filename = tmp.get_param(' filename', 'Attachment',
    'Content-Disposition')
    # clean up IE paths
    filename = filename[max(filename.rf ind('/'),
    filename.rfind( '\\'),
    filename.rfind( ':')
    )+1:]
    contentType = tmp['Content-Type']
    attach_part = Message()
    attach_part.add _header('Conten t-Type', contentType,
    name=filename)
    attach_part.add _header('Conten t-Disposition',
    'attachment', filename=filena me)
    attach_part.set _payload(attach ment.read())
    Encoders.encode _base64(attach_ part)
    mimeMsg.attach( attach_part)
    msg = mimeMsg
    ############### #########
    # set headers on message
    from_ = self.getEmail()
    msg['From'] = from_
    msg['Reply-To'] = from_
    if to: msg['To'] = to
    if cc: msg['Cc'] = cc
    if bcc: msg['Bcc'] = bcc
    msg['Date'] = self.ZopeTime() .rfc822() # needed by some servers
    if inReplyTo:
    msg['In-Reply-To'] = inReplyTo
    msg['Subject'] = Header(subject, site_encoding)
    ############### ###
    # Send the message
    SMTPserver = self._mailhost( )
    outbox = smtplib.SMTP(SM TPserver)
    try:
    cleaner = lambda adresses: [adress.strip() for adress in
    adresses.split( ',') if adress.strip()]
    all_receivers = cleaner(to) + cleaner(cc) + cleaner(bcc)
    outbox.sendmail (from_, all_receivers, msg.as_string() )
    success = 1
    except:
    success = 0
    ############### ############### ########
    # append the message to the 'Sent' box
    if success:
    Sent = self.getSentbox Name()
    connection = self.getConnect ion()
    try:
    connection.appe nd(Sent, msg.as_string() , ('\\Seen',))
    except:
    # we don't want to not send messages just because the
    sentbox isn't set correctly
    pass
    ############### ############### ###
    # returns a portal status message
    if REQUEST:
    if success:
    message = 'Succes! The message was sent '
    else:
    message = 'Error! The message could not be sent'
    REQUEST.RESPONS E.redirect(self .absolute_url() +
    '/mxmImapClient_c ompose?portal_s tatus_message=% s' % message)



    --

    hilsen/regards Max M, Denmark


    IT's Mad Science

    Comment

    Working...