2016-10-26 17 views
3

すでにスタックオーバーフローに関するいくつかの質問がありますが、私の問題点の解決策を見つけることができませんでした。PythonでのHTML電子メールへの添付ファイルの送信

Pythonを使用して、.pdf添付ファイル付きのHTMLメールを送信しようとしています。 gmail.comでメールをチェックするとうまくいくようですが、Appleのメールプログラムでメッセージをチェックすると添付ファイルが表示されません。これを引き起こす原因は何ですか?

私のコードは以下の通りです。その多くは、スタックオーバーフロー上のさまざまな場所からコピーされたので、私は完全に各部分が何をしているかを理解していないが、それは(主に)動作するようです:

import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText      
from email.mime.application import MIMEApplication 
from os.path import basename 
import email 
import email.mime.application 

#plain text version 
text = "This is the plain text version." 

#html body 
html = """<html><p>This is some HTML</p></html>""" 

# Create message container - the correct MIME type is multipart/alternative. 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Deliverability Report" 
msg['From'] = "[email protected]" 
msg['To'] = "[email protected]" 

# Record the MIME types of both parts - text/plain and text/html 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 

# create PDF attachment 
filename='graph.pdf' 
fp=open(filename,'rb') 
att = email.mime.application.MIMEApplication(fp.read(),_subtype="pdf") 
fp.close() 
att.add_header('Content-Disposition','attachment',filename=filename) 

# Attach parts into message container. 
msg.attach(att) 
msg.attach(part1) 
msg.attach(part2) 

# Send the message via local SMTP server. 
s = smtplib.SMTP() 
s.connect('smtp.webfaction.com') 
s.login('NAME','PASSWORD') 
s.sendmail(msg['From'], msg['To'], msg.as_string()) 
s.quit() 

私はそれが関連しているかどうかわからないんだけど私はWebFactionサーバー上でこのコードを実行しています。

ありがとうございました!

答えて

0

代わりに '代替' の使用

msg = MIMEMultipart('mixed') 

関連する問題