2016-07-25 1 views
0

파이썬 스크립트 (다음 내용)는 ".txt"첨부 파일을 보낼 수 있지만 불행히도 수신 된 첨부 파일은 "\ n"을 잃어 버려 모든 줄이 함께 있습니다. 내 칼럼 형식을 망쳤습니다. 아무도 내게 도움을 줄 수 있습니까? 고마워요!Python 전자 메일 첨부 파일 끝에 줄 바꿈 문자가 없습니다. " n"

msg['From'] = send_from 
msg['To'] = COMMASPACE.join(send_to) 
msg['Date'] = formatdate(localtime=True) 
msg['Subject'] = 'Subject of Email4' 

mailbody = "This is the content of Email4" 
msg.attach(MIMEText(mailbody)) 

with open("regresult.txt", "r") as fil: 
    part = MIMEApplication(
     fil.read(), 
     Name=basename("regresult.txt") 
    ) 
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename('regresult.txt') 
    msg.attach(part) 

업데이트 : (VIM 원격 유닉스 서버 오픈) 원본 파일은 다음과 같이이다 : original file format

수신 된 파일 형식은 다음과 같이이다 : @ gixxer의에 received

+0

결과 첨부 파일 텍스트를 표시 할 수 있습니까? 원본 텍스트? –

+0

문제는 텍스트 파일 자체에 있다고 생각합니다. 위의 코드는 꽤 간단합니다. 'MIMEApplication'은 단순히 base64를 사용하여 텍스트 파일을 인코딩하고 이메일을 보는 이메일 클라이언트는 그것을 디코드합니다. 거기에는 아무런 합병증도 없습니다. Windows에서 텍스트 파일을 여는 데 몇 가지 문제가 있었고 새 줄 문자가 없어졌습니다. .txt 파일을 첨부하기 전에'unix2dos' 또는'dos2unix'를 사용해보십시오. – gixxer

+0

@joelgoldstick, 두 형식의 이미지 만 업로드 할 수 있습니다. .txt 첨부 파일을 업로드 할 장소를 찾을 수 없습니다. – leoking87

답변

0
import smtplib 
import io 

sender = '[email protected]' 
receivers = ['[email protected]'] 
file = io.open('newfile.txt', 'r') 

message = file.readlines() 

try: 
    smtpObj = smtplib.SMTP('localhost') 
    smtpObj.sendmail(sender, receivers, message) 
    print("Successfully sent email") 
except Exception: 
    print("Error: unable to send email") 
0

감사합니다 힌트. txt 파일 자체의 형식 문제입니다. 원본 txt의 줄 끝 문자는 "\ n"이며 Unix 시스템에서는 잘 작동하지만 Windows에서는 제대로 작동하지 않습니다. Windows는 대신 "\ r \ n"을 사용합니다. 그래서 "\ r \ n"을 원본 파일의 각 줄 끝에 추가하면됩니다.