smtplib不发送邮件

时间:2013-11-14 22:16:54

标签: python email smtp gmail

我写了这个脚本,用于使用SMTP协议和GMail服务向手机发送电子邮件。但它不起作用,因为在发送消息时,收件人为空。为什么?问题在哪里?

Python代码:

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):
        debuglevel = 0

        self.server = smtplib.SMTP("smtp.gmail.com", 587)
        self.server.starttls()
        self.server.set_debuglevel(False)

        self.gmail_user = args.gmail_user
        gmail_password = args.gmail_password

        if len(gmail_password) < 1:
                print 'Insert a password!'
                gmail_password = getpass.getpass(prompt="Insert the GMail password: ")
        else:

                self.server.login(self.gmail_user, gmail_password)
                print 'Login successfully.'
                time.sleep(0.75)
                sms.select_country()


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

        ################# COUNTRY #################

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

            ################# CARRIERS #################


            italian_carriers = ['number@sms.vodafone.it',
                                '39number@timnet.com'] #0 VODAFONE 1 TIM

            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)
                sms.select_country()



            sms.send_message_normal(italian_carriers[number])

        else:
            sys.exit()



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

        sender = self.gmail_user
        to = carriers.replace('number',str(msg['telephone']))
        to_list = [to]
        print to
        print to_list

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


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

            try:

                self.server.sendmail(sender,to,text)
            except SMTPException:
                print 'Unable to send.'
                time.sleep(0.75)
                sms.select_country()


        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()


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

0 个答案:

没有答案