2014-10-06 2 views
0

왜 내 코드로 빈 메시지를 보내는 지 이해할 수 없습니다. 메시지 없음, 제목 없음.빈 메시지 smtplib과 함께 보내기

많은 샘플을 읽었지만 항상 같은 문제가 있습니다. 나는 가끔씩 우리가 .close() 또는 .quit()를 사용해야하는 이유조차도 몰랐다.

마침내 나는 길을 잃었다. 아래 마지막 코드를 참조하십시오.

### SEND EMAIL ### 
    sender = "[email protected]" 
    destination = user.email 
    html = '' 
    text = '' 
    if country is 'USA': 
     text = "your pin code:"+pin 
     html = """\ 
     <html> 
      <head></head> 
      <body> 
       <p> 
        Hi!<br> 
        How are you?<br> 
        Here is the pin code you wanted: ""+pin"" 
       </p> 
      </body> 
     </html> 
     """  
    if country is 'CAN': 
     text = "ton code pin:"+pin 
     html = """ 
     <html> 
      <head></head> 
      <body> 
       <p> 
        Bonjour !<br> 
        Ici le code pin: ""+pin"" 
       </p> 
      </body> 
     </html> 
     """ 

    try: 
     msg = MIMEMultipart('alternative') 
     if country is 'USA': 
      msg['Subject'] = "Registration" 
     if country is 'CAN': 
      msg['Subject'] = "Inscription" 
     msg['From'] = sender 
     msg['To'] = destination 
     part1 = MIMEText(text, 'plain', 'utf-8') 
     part2 = MIMEText(html, 'html', 'utf-8') 
     msg.attach(part1) 
     msg.attach(part2) 
     usernameEmail = '[email protected]' 
     passwordEmail = '123456' 
     conn = smtplib.SMTP('smtp.myserver.com') 
     conn.set_debuglevel(True) # Debug 
     conn.login(usernameEmail, passwordEmail) 
     try: 
      conn.sendmail(sender, destination, msg.as_string()) 
     finally: 
      conn.quit() 
    except SMTPException: 
     msg = 'unable to mail' 
     code = '503' 
     return { 
      "error": { 
      "message": msg, 
      "type": "myserverException", 
      "code": code 
      } 
     } 

답변

1

귀하의 국가 변수에 문제가있을 것입니다. "CAN"또는 "USA"가 아닌 다른 것으로 설정되면 메시지와 제목이 비어 있습니다.

당신은 아마이 대신처럼 뭔가를 구성하려면 : 당신은 또한 conn.sendmail에 대한 오류를 처리 할 수 ​​있습니다

# can country be lower case? try using .upper() 
if country is 'CAN': 
    # defining subject, text, and html in one block means you won't need to edit 
    # multiple spots if your logic changes. 
    subject = 'Inscription' 
    # yada 
else: # handle all cases, including unknowns. 
    subject = 'Registration' 

.

+0

아하하하. 당신 말이 맞아요. 하지만 .close() 또는 quit() – OlZ

관련 문제