iT邦幫忙

0

python email 夾帶多個檔案(已解決)

饅頭 2018-04-27 15:58:1310790 瀏覽

我是使用 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
看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2018-04-27 16:04:22 檢舉
L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'

s你確定是字串?
debug告訴你他是list,但你用了lstrip這個字串才有的方法。
饅頭 iT邦新手 4 級 ‧ 2018-04-27 16:29:42 檢舉
這我有點疑惑...
因為我 print message 但是沒有東西岀來
但是 範例的 msg.as_string() 沒這個問題
froce iT邦大師 1 級 ‧ 2018-04-27 16:45:46 檢舉
你在L.append(s.lstrip())前加個print s看看s到底是什麼吧。
froce iT邦大師 1 級 ‧ 2018-04-27 20:07:58 檢舉
你把整個程式碼丟出來吧,感覺錯不在createMessageWithAttachment。
饅頭 iT邦新手 4 級 ‧ 2018-04-30 09:02:12 檢舉
print s 出來後發現 昰斷在我 cc 因為是多個所以設成 list
夾帶檔案的方式沒有問題
抱歉>< 我是一開始找問題的方向錯誤了
我先補上問題的程式碼
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答