smtplib中.join命令的问题



我正在尝试使用。join命令从smtp库发送电子邮件给多个收件人。当我运行python代码时,电子邮件发送并工作,但是它只发送电子邮件到RECIPIENT_ADDRESS下的第一个地址,而不是列出的所有地址。我该如何解决这个问题?非常感谢。我将添加下面的代码:

Python代码

MY_ADDRESS = "*****@gmail.com"         # Replace with yours
MY_PASSWORD = "*****"      # Replace with yours
RECIPIENT_ADDRESS = ['*****@gmail.com', '*****@gmail.com']  # Replace with yours
HOST_ADDRESS = 'smtp.gmail.com'   # Replace with yours
HOST_PORT = 587                          # Replace with yours

import smtplib
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

# Connection with the server
server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
server.starttls()
server.login(MY_ADDRESS, MY_PASSWORD)
# Creation of the MIMEMultipart Object
message = MIMEMultipart()
# Setup of MIMEMultipart Object Header
message['From'] = formataddr(('Management', '*****@gmail.com'))
message['To'] = ", ".join(RECIPIENT_ADDRESS)
message['Subject'] = "Test Bill - July"
# Creation of a MIMEText Part
textPart = MIMEText("Hello John & Johnny,nnAttached below is your rent bill for the month of July 2022. To receive your rent bill with a physical copy please email back requesting for us to do so. Otherwise, you will receive your rent bill through email. If you pay online and have scheduled a payment then just double check that the amount paid covers the amount due in the bill you receive.nnBest, Management", 'plain')
# Creation of a MIMEApplication Part
filename = "Test Bill - John.pdf"
filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
filePart["Content-Disposition"] = 'attachment; filename="%s' % filename
# Parts attachment
message.attach(textPart)
message.attach(filePart)
# Send Email and close connection
smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_obj.login(MY_ADDRESS, MY_PASSWORD)
smtp_obj.sendmail(message['From'], message['To'], message.as_string())
smtp_obj.quit()

尝试使用for循环:

MY_ADDRESS = "*****@gmail.com"         # Replace with yours
MY_PASSWORD = "*****"      # Replace with yours
RECIPIENT_ADDRESS = ['*****@gmail.com', '*****@gmail.com']  # Replace with yours
HOST_ADDRESS = 'smtp.gmail.com'   # Replace with yours
HOST_PORT = 587                          # Replace with yours

import smtplib
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

# Connection with the server
server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
server.starttls()
server.login(MY_ADDRESS, MY_PASSWORD)
# Creation of the MIMEMultipart Object
message = MIMEMultipart()
# Setup of MIMEMultipart Object Header
message['From'] = formataddr(('Management', '*****@gmail.com'))
message['Subject'] = "Test Bill - July"
# Creation of a MIMEText Part
textPart = MIMEText("Hello John & Johnny,nnAttached below is your rent bill for the month of July 2022. To receive your rent bill with a physical copy please email back requesting for us to do so. Otherwise, you will receive your rent bill through email. If you pay online and have scheduled a payment then just double check that the amount paid covers the amount due in the bill you receive.nnBest, Management", 'plain')
# Creation of a MIMEApplication Part
filename = "Test Bill - John.pdf"
filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
filePart["Content-Disposition"] = 'attachment; filename="%s' % filename
# Parts attachment
message.attach(textPart)
message.attach(filePart)
# Send Email and close connection
smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_obj.login(MY_ADDRESS, MY_PASSWORD)

for email in RECIPIENT_ADDRESS:
message['To'] = ", ".join(email)
smtp_obj.sendmail(message['From'], message['To'], message.as_string())
smtp_obj.quit()

此问题的解决方案是将message[' to ']部分替换为收件人电子邮件。例如message[' to '] = 'xxxx@gmail.com, yyyy@gmail.com'然后替换sendmail(message['To'])sendmail(消息(','].split (" ")). 因此,第二部分的完整代码行应该是smtp_obj。sendmail(消息("的")、消息(','].split (" "), message.as_string ()).

相关内容

  • 没有找到相关文章

最新更新