2016-12-08 4 views
0

さまざまな数のインラインイメージを使用して電子メールを送信するためのPythonスクリプトがあります。実際には1つの例外を除いて意図したとおりに動作しています。各画像も添付ファイルとして送信され、各画像は2回表示されます。スクリプトの多くの部分を無効にした後でも、複製の原因を見つけることができませんでした。インラインイメージも添付されます

!/usr/bin/python 
    coding: utf-8 

    import os, sys 
    import mimetypes 
    from email.MIMEMultipart import MIMEMultipart 
    from email.MIMEText import MIMEText 
    from email.MIMEImage import MIMEImage 
    from email.mime.base import MIMEBase 
    from email import encoders import smtplib 

    try: 
strTo = ‘’’[email protected]’’’ 
strFrom = ‘’’[email protected]’’’ 

msg = MIMEMultipart() 
msg['Subject'] = ‘’’Photo Test’’’ 
msg['From'] = '''[email protected]''' 
msg['Reply-to'] = '''[email protected]''' 
msg['To'] = ‘’’[email protected]’’’ 
msg.preamble = 'This is a multi-part message in MIME format.' 

# Encapsulate the plain and HTML versions of the message body in an 
# 'alternative' part, so email agents can decide which they want to display. 
msgAlternative = MIMEMultipart('alternative') 
msg.attach(msgAlternative) 
msgText = MIMEText(''' words here, then the image followed by more words here''', 'plain') 
msgAlternative.attach(msgText) 
msgText = MIMEText('''<p>words here, then the image</p><p><img src="cid:image1"></p><p>followed by more words here</p><p></p>''','html') 
msgAlternative.attach(msgText) 
# Attach Any Images 
if '''/Users/jim/Desktop/003.jpg''' != "": 
    images = '''/Users/jim/Desktop/003.jpg'''.splitlines() 
    i=1 
    for image in images: 
     # print 'Image',i,': ',image,'\n' 
     fp = open(image, 'rb') 
     msgImage = MIMEImage(fp.read()) 
     fp.close() 
     # Define the image's ID as referenced above 
     msgImage.add_header('Content-ID', '<image'+str(i)+'>') 
     msg.attach(msgImage) 
     i+=1 

    #send the email 
smtp = smtplib.SMTP_SSL(‘’’mailserver.net''', '''465''') 
smtp.ehlo() 
smtp.login('''[email protected]''', ‘’’xxxxxx’’’) 
smtp.sendmail(strFrom,strTo.split(","), msg.as_string()) 
smtp.quit() except: print("Sending Error : "+strTo) 

画像の添付ファイルを防止するにはどうすればよいですか?一度見つけ

答えて

0

シンプルなソリューション、:

msg = MIMEMultipart() 

があるべき

msg = MIMEMultipart('related') 
関連する問題