smtplib.SMTPRecipients在Python脚本上发生错误

时间:2013-11-05 11:18:39

标签: python smtp gmail

我有这段代码:

import smtplib
import os 
import time
import sys
import argparse
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText


    class smsGatewaying:


        def login_gmail(self,user,password):
            self.server = smtplib.SMTP("smtp.gmail.com", 587)
            self.server.starttls()

            try:
                gmail_user = args.gmail_user
                gmail_password = args.gmail_password
            except SMTPAuthenticationError:
                print "SMTP authentication went wrong. Most probably the server didn't accept the username/password combination provided."
            finally:
                if gmail_password < '1':
                    print 'Insert a password!'
                    gmail_password = getpass.getpass(prompt="Insert the GMail password: ")
                else:
                    self.server.login(gmail_user, gmail_password)
                    print 'Login successfully.'
                    time.sleep(0.75)
                    x.select_country()


        def select_country(self):
            print 'Insert country: '
            country = raw_input()

            if country == 'Italy' or country == 'italy':

                italian_carriers = ['number@sms.vodafone.it',
                                    '39number@timnet.com']

                select_carriers = raw_input("Select carriers: ")

                if select_carriers == 'Vodafone' or select_carriers == 'vodafone':
                    number = 0
                elif select_carriers == 'TIM' or select_carriers == 'tim' or select_carriers == 'Tim':
                    number = 1
                else:
                    print "L'operatore telefonico selezionato non è disponibile."
                    time.sleep(0.80)
                    x.select_country()



                x.send_message_normal(italian_carriers[number])

            else:
                sys.exit()



        def send_message_normal(self, carriers):
            msg = MIMEMultipart()
            msg['sender'] = raw_input("Insert sender: ")
            msg['telephone'] = input("Insert telephone number: ")
            text = raw_input("Insert text: ")
            msg.attach = (MIMEText(text))

            carriers.replace('number',str(msg['telephone']))

            final = raw_input("Are you sure?[Y/N] ")


            if final == 'y' or final == 'Y':

                self.server.sendmail(msg['sender'],str(msg['telephone']),text) 

            elif final == 'n' or final == 'N':
                exit_ = raw_input("Do you want to exit?[Y/N] ")

                if exit_ == 'Y' or exit_ == 'y':

                    print 'Run main script...'
                    newWorkingDirectory = '../BRES.py'
                    os.path.join(os.path.abspath(sys.path[0]), newWorkingDirectory)
                    os.system('python BRES.py')


    if __name__ == '__main__':
        parser = argparse.ArgumentParser()
        parser.add_argument("gmail_user", type=str)
        parser.add_argument("gmail_password",type=str)

        args = parser.parse_args()


        x = smsGatewaying()
        print 'Welcome to SMS Gatewaying service! Multiple countries and multiple carriers are available.'
        time.sleep(1)
        x.login_gmail(args.gmail_user,args.gmail_password)

尝试向数字发送消息后,我在shell上遇到了这个错误:

  

smtplib.SMTPRecipientsRefused:{'29403983292209':( 553,“5.1.2我们   无法找到收件人域名。请检查\ n5.1.2   拼写错误,并确保您没有输入任何空格,   期限,\ n5.1.2或收件人电子邮件后的其他标点符号   地址。 a6sm58887940eei.10 - gsmtp“)}

我尝试了所有,但没有解决方案:(

2 个答案:

答案 0 :(得分:0)

考虑这一行:

self.server.sendmail(msg['sender'],str(msg['telephone']),text) 

您认为此时msg['telephone']的价值是什么?尝试将每个参数的值打印到self.server.sendmail()。我相信您会发现msg[telephone]是电话号码。 是电子邮件地址。

尝试使用这两行而不是你拥有的那些:

to = carriers.replace('number',str(msg['telephone']))

self.server.sendmail(msg['sender'],to,text) 

答案 1 :(得分:0)

经过大量搜索和哭泣之后……错误是由您尝试发送的电子邮件引起的。

在我的情况下,这个是停机的,一旦被功能性的改变,邮件就会像往常一样出来。

我直接在console里做了测试,把数据改成普通文本,一一改,直到出来。