[英]Sending email with smtplib, no errors but no messages delivered
**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()
我無法重現您聲稱從代碼的早期版本中獲得的語法錯誤。 但是,您的嘗試還存在一些其他問題。
首先,要排除故障,請嘗試添加
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 中成為官方推薦版本。
而不是:地址 = 電話簿
嘗試使用:地址 = i
我認為問題可能在於您將地址變量設置為列表而不是列表中的單個項目。
請參考我的代碼。 我已經這樣做了,它的工作。 您可以通過參考我的代碼獲得一些線索。
檢查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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.