2012-07-31 79 views
2

我已經寫了一個Python腳本來自動發送一些信息給我的朋友。我使用SMTPlib,如果我只發送給我或另外添加另一封郵件,效果很好。SMTP郵件發送給多個收件人,但不接受它

當我嘗試通過腳本發送到電子郵件17(包括我的發送者電子郵件),在網絡基礎的Gmail,然後我檢查發送的郵件時,我看到的郵件已發送,但我沒有收到。 只有第一個接收者收到該郵件。 如果我從郵件全部答覆,大家只拿到了該答覆。

我無法弄清楚他們爲什麼不接受它,當我從劇本送,我問我的垃圾郵件的朋友檢查,但她並沒有發現任何東西。

這是我的代碼:

#!/usr/bin/env python 
import smtplib 
import csv 
from datetime import datetime, timedelta 

SMTP_SERVER = 'smtp.gmail.com' 
SMTP_PORT = 587 

sender = '[email protected]' 

password = None 
with open('pass', 'rt') as f: 
    password = f.read().strip('\n') 


def send_mail(recipient, subject, body): 
    """ 
    Send happy bithday mail 
    """ 
    headers = ["From: " + sender, 
       "Subject: " + subject, 
       "To: " + recipient, 
       "MIME-Version: 1.0", 
       "Content-Type: text/html"] 

    headers = "\r\n".join(headers) 

    smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) 
    smtp.ehlo() 
    smtp.starttls() 
    smtp.ehlo 
    smtp.login(sender, password) 

    body = "" + body +"" 
    smtp.sendmail(sender, recipient, headers + "\r\n\r\n" + body) 
    print "Sent to ", 
    print recipient 

    smtp.quit() 

def send_happybirthday(recipient): 
    body = """Happy birthday to you! 
      \n<br/>From C2k8pro with love 
      """ 
    subject ='[BirthReminder] Happy birthday to you! from C2k8pro' 
    send_mail(recipient, subject, body) 


def send_notification(all_mails, names): 
    body = """Tomorrow is birthday of %s""" % names 
    send_mail(all_mails, body, body) 

def test_send_mail(): 

    notify_body = """Tomorrow is birthday of """ 
    recipients = ['[email protected]'] 

    today = datetime.now() 
    format = "%d-%m-%Y" 
    print today 
    today_in_str = datetime.strftime(today, format) 


def read_csv(): 
    FILENAME = 'mails.csv' 
    reader = csv.reader(open(FILENAME, 'rt'), delimiter=',') 

    today = datetime.now() 
    one_day = timedelta(days=1) 
    tomorrow = today + one_day 

    all_mails = [] 
    str_format = "%d/%m" 
    str_today = today.strftime(str_format) 
    str_tomorrow = tomorrow.strftime(str_format) 

    print 'Today is ', str_today 
    tomorrow_birth = [] 
    for row in reader: 
     name = row[1].strip() 
     dob = row[2] 
     dmy = dob.split("/") 
     mail = row[3] 
     all_mails.append(mail) 

     #TODO fix dob with only 1 digit 
     birth_date = dmy[0] + "/" + dmy[1] 
     if str_today == birth_date: 
      print 'Happy birthday %s' % name 

      try: 
       send_happybirthday(mail) 
      except Exception, e: 
       print e 

     elif str_tomorrow == birth_date: 
      tomorrow_birth.append(name) 
      print "Tomorrow is %s's birthday" % name 

    # Remove empty string 
    all_mails = filter(None, all_mails) 
    print 'All mails: ', len(all_mails) 
    str_all_mails = ', '.join(all_mails) 

    if tomorrow_birth: 
     all_tomorrow = ', '.join(tomorrow_birth) 
     send_notification(str_all_mails, all_tomorrow) 


def main(): 
    read_csv() 

if __name__ == "__main__": 
    main() 

誰能解釋這一點。謝謝!

+0

能否請您提供您的代碼? – Max 2012-07-31 17:15:47

+0

也許我從這裏找到了解決方案: http://stackoverflow.com/questions/6941811/send-email-to-multiple-recipients-from-txt-file-with-python-smtplib 我將相同的字符串傳遞給msg [「爲」]與sendmail(FROM_EMAIL,電子郵件,msg.as_string()) – HVNSweeting 2012-07-31 17:21:03

+0

Downvote:問題是不可能的,沒有看到代碼或錯誤消息的辦法。 – jforberg 2012-07-31 17:24:45

回答

相關問題