2011-09-15 6 views
1

나는 이상한 버그가 있습니다. smtplib을 사용하여 Gmail 계정에서 이메일을 보내고 있습니다. 스크립트를 실행할 때마다 보내는 데 사용하고있는 Gmail 계정의 "보낸 사람"탭에 메시지가 표시됩니다. 그러나 때로는 수신 측에서 메시지를받지 못합니다.gmail을 사용하는 python smtplib : 약 35 자 이상의 본문이있는 메시지는 수신되지 않습니다.

나는 메시지 본문의 길이와 그것이 수신되는지 여부 사이의 상관 관계에 주목했습니다. "foo"라는 메시지는 매번 전달되고 원하는 메시지는 약 200 자 길이되지 않습니다. 나는 몸 길이가 0에서 60 사이 인 메시지를 보내려고 노력했다. 두 번 모두 60 개의 쇼를 보내는 gmail 계정의 '보낸'폴더에 표시했지만 수신 전자 메일 계정에는 다음과 같이 표시된다.

enter image description here 메일 기능 여기

for i in range(100): 
     mail('[email protected]','testing limit',str(i) + "a"*i) 

그리고있다 :

def mail(to, subject, text, attach=None): 

"""Sends an email, formatted as HTML to list of senders with an optional attachment. 
Specifically, the 'to' argument must be a comma seperated list of email addresses. 
'subject' and 'text' are what appear in the email subject and body, respectively and 
'attach' is a path to an attachment. 
""" 
msg = MIMEMultipart() 

# build the email header 
msg['From'] = 'A Concerned Robot' 
msg['To'] = to 
msg['Subject'] = subject 

# attach our body text as html 
msg.attach(MIMEText(text,'html')) 

# attach the attachment if its there 
part = MIMEBase('application', 'octet-stream') 
if attach is not None: 
    part.set_payload(open(attach, 'rb').read()) 
    Encoders.encode_base64(part) 
    part.add_header('Content-Disposition', 
     'attachment; filename="%s"' % os.path.basename(attach)) 
    msg.attach(part) 

# open up a line with the server 
mailServer = smtplib.SMTP("smtp.gmail.com", 587) 
mailServer.ehlo() 
mailServer.starttls() 
mailServer.ehlo() 

# login, send email, logout 
mailServer.login(conf.user, conf.pw) 
mailServer.sendmail(conf.user, to, msg.as_string()) 
mailServer.close() 
+0

나는 이것이 이상한 버그라고 말해야 만합니다. 나는 지난 며칠 동안 smtplib 모듈을 사용해 왔지만 결코이 문제에 부딪치지 않았다 ... 그러나 나는 당신이 생각해내는 결과와 함께 당신의 예제를 실행했다. 첨부 파일을 스트라이프 처리하고 일반 텍스트를 모두 전송하는 것과 같은 몇 가지 작업을 시도했습니다. +1 이상하고 자극적 인 버그. –

+0

사이드 노트 : 무료 Google 계정을 사용 중이라면 24 시간 동안 500 개의 이메일로 제한됩니다.따라서 테스트 할 때 보낼 메시지 수를 줄이려고 할 수 있습니다. –

답변

2

가능성이 가장 높은 폭발물 여기

내가 메시지를 보낼 때 사용하는 코드입니다 보내는 메시지의 하위 집합 만 수신하는 것은 대상에서 메시지를 삭제한다는 것입니다 (스팸 폴더를 확인 했습니까?). FWIW, 스크립트를 사용하면 100 개의 메시지가 모두 도착합니다.

메시지를 보낼 때 Google에서 거부하거나 배달을 수락 할 수 있습니다. Google에서 메시지 배달을 시도하면 거부 될 수 있습니다 (이 경우 Google로부터 배달 상태 통지를 받아야 함). 일부 메일 서버는 메시지를 수락하지만 배달하지 않을 수도 있습니다 (폐기하거나 일종의 격리 저장소로 라우팅).

이렇게하면 원래의 문제로 되돌아갑니다. 200 문자 메시지를 보낼 수없는 이유는 무엇입니까? (냈다 일부 헤더)이 받아 들여졌다

>>> import smtplib 
>>> s = smtplib.SMTP("smtp.gmail.com", 587) 
>>> s.ehlo() 
(250, 'mx.google.com at your service, [60.234.179.13]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES') 
>>> s.starttls() 
(220, '2.0.0 Ready to start TLS') 
>>> s.ehlo() 
(250, 'mx.google.com at your service, [60.234.179.13]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH\nENHANCEDSTATUSCODES') 
>>> s.login("username", "password") 
(235, '2.7.0 Accepted') 
>>> s.mail("sender") 
(250, '2.1.0 OK n2sm1693666ybe.6') 
>>> s.rcpt("recipient") 
(250, '2.1.5 OK n2sm1693666ybe.6') 
>>> s.data("Subject: " + ("a" * 200) + "\n\nThis is a test message.") 
(250, '2.0.0 OK 1316147451 n2sm1693666ybe.6') 
>>> s.quit() 
(221, '2.0.0 closing connection n2sm1693666ybe.6') 

(그래서 문제가없는), 또한 도착 : 이제 우리가 그 메시지를 보내려고하면 어떻게되는지 보자

Received: by 10.223.158.77 with SMTP id e13cs10409fax; 
     Thu, 15 Sep 2011 21:30:54 -0700 (PDT) 
Received: by 10.100.214.1 with SMTP id m1mr1823145ang.134.1316147453266; 
     Thu, 15 Sep 2011 21:30:53 -0700 (PDT) 
Received: from mail-gx0-f178.google.com (mail-gx0-f178.google.com [209.85.161.178]) 
     by mx.google.com with ESMTPS id l19si1913755anm.182.2011.09.15.21.30.51 
     (version=TLSv1/SSLv3 cipher=OTHER); 
     Thu, 15 Sep 2011 21:30:52 -0700 (PDT) 
Received: by mail-gx0-f178.google.com with SMTP id 21so2167851gxk.23 
     for <[email protected]>; Thu, 15 Sep 2011 21:30:51 -0700 (PDT) 
Received: by 10.151.43.6 with SMTP id v6mr2088425ybj.402.1316147451688; 
     Thu, 15 Sep 2011 21:30:51 -0700 (PDT) 
Received: from somewhere ([60.234.179.13]) 
     by mx.google.com with ESMTPS id n2sm1693666ybe.6.2011.09.15.21.30.19 
     (version=TLSv1/SSLv3 cipher=OTHER); 
     Thu, 15 Sep 2011 21:30:51 -0700 (PDT) 
Message-ID: <[email protected]> 
Date: Thu, 15 Sep 2011 21:30:51 -0700 (PDT) 
Sender: Tony Meyer <address> 
From: someone 
Subject: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 

This is a test message. 

따라서 잠재적으로 코드가 작동하고 메시지가 도착합니다. 문제는 전자 메일 메시지의 대상과 관련이 있습니다. 불행하게도 메시지를 무시하고 메시지를 버리면 이유를 알기가 어렵습니다 (이유가있는 DSN을받는 것과 달리)입니다.

코드에 첨부 파일을 포함하는 섹션이 포함됩니다. 전송이 실패 할 때 포함되어 있습니까? 문제의 원인 일 수 있습니다. RFC 은 일 수 있고은 최대 998 일 수 있지만 은 최대 78 자 여야합니다. 목적지가 더 엄격한 값을 적용하고 있습니까? (한 줄에 200자를 가정하십시오.) 그렇다면 본문을 인코딩 (예 : quoted-printable 또는 base64)하거나 연속을 사용하여 주제를 줄 바꿈 할 수 있습니다.

관련 문제