2012-09-14 6 views
1

Python 전자 메일 클라이언트를 사용하여 전자 메일을 보내려고합니다. 다음 코드를 작성했지만 첨부 파일이 아닌 본문으로 attachemnt를 보냅니다.Python : 전자 메일 본문에 첨부 파일이 표시됩니다.

누군가가 코드에 어떤 문제가 있는지 말해 줄래 :

# Import smtplib for the actual sending function 
import smtplib 

# Here are the email package modules we'll need 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email.mime.application import MIMEApplication 


EMAIL_LIST = ['[email protected]'] 

# Create the container (outer) email message. 
msg = MIMEMultipart() 
msg['Subject'] = 'THIS DOES NOT WORK' 
# me == the sender's email address 
# family = the list of all recipients' email addresses 
msg['From'] = '[email protected]' 
print EMAIL_LIST 
print '--------------------' 
print ', '.join(EMAIL_LIST) 
msg['To'] = ', '.join(EMAIL_LIST) 
msg.preamble = 'THIS DOES NOT WORK' 


fileName = 'c:\\p.trf' 
with open(fileName, 'r') as fp: 
    attachment = MIMEText(fp.read()) 
    fp.close() 
    msg.add_header('Content-Disposition', 'attachment', filename=fileName) 
    msg.attach(attachment) 


# Send the email via our own SMTP server. 
s = smtplib.SMTP('localhost') 
s.sendmail('[email protected]', EMAIL_LIST, msg.as_string()) 
s.quit() 

답변

0

첨부를 들어, 당신은 아마 사용해야합니다,이 같은 MIMEBase 일 :

import os 
from email import encoders 
from email.mime.base import MIMEBase 

with open(fileName,'r') as fp: 
    attachment = MIMEBase('application','octet-stream') 
    attachment.set_payload(fp.read()) 
    encoders.encode_base64(attachment) 
    attachment.add_header('Content-Disposition','attachment',filename=os.path.split(fileName)[1]) 
    msg.attach(attachment) 
관련 문제