** 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()
答案 0 :(得分:1)
代替: 地址=电话簿
尝试使用: 地址= i
我认为问题可能在于您将地址变量设置为列表,而不是列表中的单个项目。
答案 1 :(得分:1)
我无法重现您声称从较早版本的代码中得到的语法错误。但是,您的尝试还有其他一些问题。
首先,要进行故障排除,请尝试添加
smtp.set_debuglevel(1)
准确显示您要发送的内容。要弄清最终的成绩单,需要对SMTP有一定的了解,但是如果将来您有类似的问题,包括此成绩单可能会很有价值。
其次,在这样的循环中重复发送相同的消息很成问题-这很浪费,并且可能触发自动反垃圾邮件控制。要将同一封邮件发送给多个收件人,只需列出对sendmail
的呼叫中的所有收件人(基本上将其作为Bcc:
收件人)。
第三,您实际上对电子邮件的外观有一个过于简单的模型。如果这两个部分都是琐碎的ASCII字符串,则仅包含Subject:
和简单文本正文的最小消息是有效的,但是现代电子邮件消息在这种情况下就不需要编码(例如Unicode字符串或二进制附件)。当然,您可以自己整理一条有效的消息,但这很乏味。您应该改用Python email
库。
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['From'] = email_address
msg['To'] = ', '.join(phone_book)
msg['Subject'] = subject
msg.set_content(body)
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(email_address, email_password)
smtp.send_message(msg)
smtp.quit()
有许多使用旧式email.message.Message
类的较旧的示例,但现在应避免使用。经过全面改进的email
库是在Python 3.3中引入的,并在3.5中成为正式和推荐的版本。
答案 2 :(得分:1)
请参考我的代码。我已经做到了,它正在工作。 您可以通过参考我的代码获得一些帮助。
检查main
功能代码。
import smtplib
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
MY_ADDRESS = 'xyz@gmail.com'
PASSWORD = 'YourPassword'
def get_contacts(filename):
names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
def read_template(filename):
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
def main():
names, emails = get_contacts('C:/Users/VAIBHAV/Desktop/mycontacts.txt') # read contacts
message_template = read_template('C:/Users/VAIBHAV/Desktop/message.txt')
s = smtplib.SMTP(host='smtp.gmail.com', port=587)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
for name, email in zip(names, emails):
msg = MIMEMultipart() # create a message
message = message_template.substitute(PERSON_NAME=name.title())
print(message)
msg['From'] = MY_ADDRESS
msg['To'] = email
msg['Subject'] = "Sending mail to all"
msg.attach(MIMEText(message, 'plain'))
s.send_message(msg)
del msg
s.quit()
if __name__ == '__main__':
main()