0

我有这个代码:

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")}

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

4

2 回答 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) 
于 2013-11-05T16:40:22.543 回答
0

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

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

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

于 2021-07-08T18:44:34.007 回答