发送带有smtplib的电子邮件,没有错误,但没有传递邮件

问题描述 投票:1回答:1

** UPDATE2:在V2的最后代码中,我将smtp.sendmail(email_address, address, msg,)替换为smtp.sendmail(email_address, phone_book, msg,),直接访问phone_book似乎已经解决了该问题。这是正在寻找的任何人的工作代码:

import smtplib

email_address = '' # Your email goes here
email_password = '' # Your password goes here
phone_book = [''] # List of receivers


def Add_Email():
    client_email = input('Email of receiver? ')
    phone_book.append(client_email)

def Add_Subject_Message_Send():    
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(email_address, email_password)

            subject = input('Enter your subject here: ')
            body = input('Enter your message here: ')

            msg = f'Subject: {subject}\n\n{body}'

            for i in phone_book:
                address = i
                smtp.sendmail(email_address, phone_book, msg,)


Add_Email()
Add_Subject_Message_Send()

**

**更新:我将代码替换为没有GUI的最简单版本。 V1在代码中定义了主体和主体时起作用。当用户定义主题和正文时,V2不起作用。现在V2出现此错误消息:

Traceback (most recent call last):
  File "c:/Users/Me/Desktop/work/infosend/test4.py", line 33, in <module>
    Add_Subject_Message_Send()
  File "c:/Users/Me/Desktop/work/infosend/test4.py", line 29, in Add_Subject_Message_Send
    smtp.sendmail(email_address, address, msg,)
  File "C:\python\lib\smtplib.py", line 885, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'': (555, b'5.5.2 Syntax error. l15sm65056407wrv.39 - gsmtp')}

**

我正在使用smtplib发送电子邮件。只要在代码中定义了邮件的主题和正文,一切都将起作用,并且电子邮件将被发送。如果代码中未定义主题或正文,则不会显示任何错误,但不会传递消息。

我想创建一个函数,该函数使我可以编写主题和消息,而不是在代码中进行定义。我的功能似乎正常工作,但是没有传递任何消息,也没有收到错误消息。

附加我的代码的两个版本。

第一个版本有效。它定义了主题和主体。

第二版本不起作用。包括定义主题和身体的功能。终端没有收到错误。

V1

import smtplib

email_address = '' # Enter your email address here
email_password = '' # Enter your email password here

phone_book = [''] # Here enter the email of receiver

with smtplib.SMTP('smtp.gmail.com', 587) as smtp: # Connects with GMAIL
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(email_address, email_password)

            subject = 'test3' # Subject and body defined in code = works
            body = 'test3'

            msg = f'Subject: {subject}\n\n{body}'

            for i in phone_book:
                address = i
                smtp.sendmail(email_address, address, msg,)

V2

    import smtplib

email_address = '' # Your email goes here
email_password = '' # Your password goes here
phone_book = [''] # List of receivers


def Add_Email():
    client_email = input('Email of receiver? ')
    phone_book.append(client_email)

def Add_Subject_Message_Send():    
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(email_address, email_password)

            subject = input('Enter your subject here: ')
            body = input('Enter your message here: ')

            msg = f'Subject: {subject}\n\n{body}'

            for i in phone_book:
                address = i
                smtp.sendmail(email_address, address, msg,)


Add_Email()
Add_Subject_Message_Send()
python smtplib pysimplegui
1个回答
0
投票

而不是:地址=电话簿

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