我是使用 python 2.7.13
我從 python email 看
如果要夾帶多個檔案的話可以用 list 的方式存放附件名稱
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()
但是我在做的時候卻報錯
  File , line 107, in createMessageWithAttachment
    return {'raw': base64.urlsafe_b64encode(message.as_string())}
 (中間省略 需要再補上)
  File "C:\Python27\lib\email\quoprimime.py", line 97, in _max_append
    L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'
這裡的模組昰用 from email.mime.multipart import MIMEMultipart
附上我的 code, 這個昰 gmail 夾帶附件的 function,然後我留我想用的部分
我目前想夾帶的檔案類型昰 2個檔案(無附檔名)、2個png
def createMessageWithAttachment( to, cc, subject, msgHtml, attachmentFiles):
    message = MIMEMultipart('mixed')
    message['To'] = to
    message['From'] = 'xxx@gmail.com'
    message['Subject'] = subject
    message['Cc'] = cc
    messageR = MIMEMultipart('related')
    messageR.attach(MIMEText(msgHtml, 'html'))
    message.attach(messageR)
    for attachmentFile in attachmentFiles:
        print ("create_message_with_attachment: file:", attachmentFile)
        content_type, encoding = mimetypes.guess_type(attachmentFile)
        if content_type is None or encoding is not None:
            content_type = 'application/octet-stream'
        main_type, sub_type = content_type.split('/', 1)
        if main_type == 'text':
            fp = open(attachmentFile, 'rb')
            msg = MIMEText(fp.read(), _subtype=sub_type)
            fp.close()
        elif main_type == 'image':
            fp = open(attachmentFile, 'rb')
            msg = MIMEImage(fp.read(), _subtype=sub_type)
            fp.close()
        elif main_type == 'audio':
            fp = open(attachmentFile, 'rb')
            msg = MIMEAudio(fp.read(), _subtype=sub_type)
            fp.close()
        else:
            fp = open(attachmentFile, 'rb')
            msg = MIMEBase(main_type, sub_type)
            msg.set_payload(fp.read())
            fp.close()
        filename = os.path.basename(attachmentFile)
        msg.add_header('Content-Disposition', 'attachment', filename=filename)
        message.attach(msg)
    
    return {'raw': base64.urlsafe_b64encode(message.as_string())}
增加 問題 function
def SendMessage(to, attachmentFile):
    user_id = 'me'
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
    mg = '<br/>此信件為系統回覆,請勿回覆此信件'
    subject = 'title'
    msgHtml = 'hello' + mg
    cc = ['a@gmail.com','b@gmail.com']
    message = createMessageWithAttachment(
                to, cc, subject, msgHtml, attachmentFile)
    result = SendMessageInternal(service, user_id, message)
    return result