smtplib.SMTPRecipientsRefused 错误

问题描述 投票:0回答:2

我有这个代码:

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 = ['[email protected]',
                                    '[email protected]']

                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 我们 无法找到收件人域。请检查是否有任何 5.1.2 拼写错误,并确保您没有输入任何空格, 时期, 5.1.2 或收件人电子邮件后的其他标点符号 地址。 a6sm58887940eei.10 - gsmtp")}

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

python smtp gmail
2个回答
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) 

0
投票

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

就我而言,这封邮件已关闭,一旦被功能邮件更改,邮件就会像往常一样发出。

我直接在控制台进行测试,用普通文本更改数据,一一改,直到出来。

© www.soinside.com 2019 - 2024. All rights reserved.