smtplib 不向 CC 收件人发送邮件
smtplib not sending mails to CC recipients
尝试编写一个简单的代码来向一个收件人和另外两个收件人发送电子邮件。问题是当我将 cc 放入 server.sendmail(email, (send_to_email + cc), text) 时。它给我错误 TypeError: can only concatenate str (not "list") to str。如果我从 server.sendmail(email, send_to_email, text) 和 运行 代码中删除抄送,我的收件人会收到邮件并在电子邮件中显示其他 2 封抄送邮件,但 2 封抄送收件人没有收到邮件。有帮助吗?
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
cc = ['1mail@gmail.com','2mail@gmail.com']
email = "botemail@gmail.com" # the email where you sent the email
password = 'password123'
send_to_email = 'mymail@gmail.com' # for whom
subject = "Status BOT"
message = "Could not login"
print("Sending email to", send_to_email)
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, (send_to_email + cc), text)
server.quit()
你走在正确的轨道上
这里的问题是您正在尝试将 str
send_to_email 与 list
cc
连接起来
您可以将 send_to_email 更改为 list
,它应该可以正常工作
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
cc = ['1mail@gmail.com','2mail@gmail.com']
email = "botemail@gmail.com" # the email where you sent the email
password = 'password123'
send_to_email = ['mymail@gmail.com'] # for whom #### CHANGED HERE
subject = "Status BOT"
message = "Could not login"
print("Sending email to", send_to_email)
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, (send_to_email + cc), text)
server.quit()
设法解决问题。
msg["From"] = email
msg["To"] = ', '.join(send_to_email)
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject
尝试编写一个简单的代码来向一个收件人和另外两个收件人发送电子邮件。问题是当我将 cc 放入 server.sendmail(email, (send_to_email + cc), text) 时。它给我错误 TypeError: can only concatenate str (not "list") to str。如果我从 server.sendmail(email, send_to_email, text) 和 运行 代码中删除抄送,我的收件人会收到邮件并在电子邮件中显示其他 2 封抄送邮件,但 2 封抄送收件人没有收到邮件。有帮助吗?
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
cc = ['1mail@gmail.com','2mail@gmail.com']
email = "botemail@gmail.com" # the email where you sent the email
password = 'password123'
send_to_email = 'mymail@gmail.com' # for whom
subject = "Status BOT"
message = "Could not login"
print("Sending email to", send_to_email)
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, (send_to_email + cc), text)
server.quit()
你走在正确的轨道上
这里的问题是您正在尝试将 str
send_to_email 与 list
cc
您可以将 send_to_email 更改为 list
,它应该可以正常工作
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
cc = ['1mail@gmail.com','2mail@gmail.com']
email = "botemail@gmail.com" # the email where you sent the email
password = 'password123'
send_to_email = ['mymail@gmail.com'] # for whom #### CHANGED HERE
subject = "Status BOT"
message = "Could not login"
print("Sending email to", send_to_email)
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, (send_to_email + cc), text)
server.quit()
设法解决问题。
msg["From"] = email
msg["To"] = ', '.join(send_to_email)
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject