Trying to write a simple code to send out emails to one receiver and 2 other recipients. The issue is that when I put cc in server.sendmail(email, (send_to_email + cc), text). It gives me the error TypeError: can only concatenate str (not "list") to str. If I remove cc from server.sendmail(email, send_to_email, text) and run the code, my receiver gets the mail and shows the other 2 CC mails in the email but the 2 CC recipients do not get the mail. Any help?
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()
You are on the right track
The issue here is you are trying to concat a str
send_to_email with a list
cc
You can change the send_to_email as a list
and it should work fine
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()