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()
Manage to fix the issue.
msg["From"] = email
msg["To"] = ', '.join(send_to_email)
msg['Cc'] = ', '.join(cc)
msg["Subject"] = subject
Related
I have a working send email script which takes txt file and send it encoded.
i want to be able to design (html) this encoded txt in the email body.
i know how generally design a text in email body but i cant design the encoded txt in the html.
i want to combine the two scripts and be able to design the encoded text ( first script output) into the html design of the second script
first script send email ( Works but undesigned body ):
import smtplib
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# mailing cofig:
ID = 'XX
PASSWORD = XX
email_reciever = XX
filename = r".\Reports\Report.txt"
def send_email(subject, msg):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(ID, PASSWORD)
message = 'Subject:{}\n\n{}'.format(subject, msg)
server.sendmail(ID, email_reciever, message)
# server.sendmail(ID, email_reciever, msg.as_string())
print('Succes')
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Your BeeHero Report'
msg['From'] = ID
msg['To'] = email_reciever
with open(filename, "r") as filename:
text = ''.join(filename.readlines()[1:])
msg.set_payload(text.encode())
subject = "BeeHero Report"
send_email(subject, msg)
output:
https://imgur.com/a/g6m0WKt
Second script ( designed body but not encoded as the first output)
# Sending email script Amit
import smtplib
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# mailing cofig:
ID = XX
PASSWORD = XX
email_reciever = XX
filename = r".\Reports\Report.txt"
def send_email(msg):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(ID, PASSWORD)
server.sendmail(ID, email_reciever, msg.as_string())
print('Succes')
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Your BeeHero Report'
msg['From'] = ID
msg['To'] = email_reciever
with open(filename, "r") as filename:
text = ''.join(filename.readlines()[1:])
html = (f"""
<!DOCTYPE html>
<html>
<body>
<h1 style="color:SlateGray;">{text}</h1>
</body>
</html>
""")
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
send_email(msg)
output:
https://imgur.com/a/5wqUoSa
I use bellow code I can send email to one account, but how can I send to multi account?
import smtplib
from email.mime.text import MIMEText
_user = "67676767#qq.com" #
_pwd = "orjxywulscbxsdwbaii" #
_to = linux213#126.com" #
msg = MIMEText("content") #
msg["Subject"] = "邮件主题测试" #
msg["From"] = _user
msg["To"] = _to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
print ("Success!")
except smtplib.SMTPException as e:
print ("Falied,%s" % e)
Try this:
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.qq.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me#example.com'
recipients = ['k.ankur#abc.com', 'a.smith#abc.co.in']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
I have made an application in python with tkinter GUI.
In this application I have a button and when I click it I want to email certain records from my database to a customer using the standard email library from python. However I seem to be able to only send 1 record...
Am I overlooking something?
def Send_OC_Worksheet_Mail(self, event):
fromaddr = "my email address"
toaddr = "customer email address"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "my subject"
Send_Jobs_List_Qry = (c.execute('SELECT Job_Description, Job_Date FROM Jobs WHERE Worksheet_Mailed=?', (0, ))).fetchall()
for Send_Jobs_List_Row in Send_Jobs_List_Qry:
Job_Descr = str(Send_Jobs_List_Row[0])
Job_Date = str(Send_Jobs_List_Row[1])
Job_Body = (Job_Date+':'+Job_Descr+'\n')
body = Job_Body
msg.attach(MIMEText(body, 'html'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
I am making a script which checks a website for auctions interested for me. If it finds an interested link, it adds this link to listalink with listalink.append(link). When I am sending an email I have this error:
AttributeError: 'list' object has no attribute 'encode'.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
# listalink example:
listalink = ["http://www.google.pl", "http://www.facebook.com", "http://amazon.com"]
def email_sender():
fromaddr = "test_e_mail#wp.pl"
toaddr = "myemail#gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "NEW INTERESTED AUCTIONS"
body = listalink
msg.attach(MIMEText(body, 'plain'))
server_ssl = smtplib.SMTP_SSL("smtp.wp.pl", 465)
server_ssl.ehlo()
server_ssl.login("test_e_mail#wp.pl", "password")
text = msg.as_string()
server_ssl.sendmail(fromaddr, toaddr, text)
server_ssl.close()
print 'E-mail sent'
the error occurs because you add list to email's body (the body must be str):
body = listalink
Solution:
listalink = ["http://www.google.pl", "http://www.facebook.com", "http://amazon.com"]
listalink = " ".join(listalink)
I am working on a python script to send email to my customer with a survey. I will send only one email with all my customers's emails in the BCC field so that I do not need to loop through all the emails. Everything works fine when I tested sending emails to my company's coleagues and also when I sent to my personal email, but whenever I send to a gmail account, the BCC field appears to not be hidden and show all the emails. I found this post Email Bcc recipients not hidden using Python smtplib and tried that solution as well, but as I am using a html body email, the emails were shown inside the body. Can anyone help me on this one?
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def send_survey_mail():
template_path = 'template.html'
background_path = 'image.png'
button_path = 'image2.png'
try:
body = open(template_path, 'r')
msg = MIMEMultipart()
msg['Subject'] = 'Customer Survey'
msg['To'] = ', '.join(['myemail#domain.com.br', 'myemail2#domain.com'])
msg['From'] = 'mycompany#mycompany.com.br'
msg['Bcc'] = 'customer#domain.com'
text = MIMEText(body.read(), 'html')
msg.attach(text)
fp = open(background_path, 'rb')
img = MIMEImage(fp.read())
fp.close()
fp2 = open(button_path, 'rb')
img2 = MIMEImage(fp2.read())
fp2.close()
img.add_header('Content-ID', '<image1>')
msg.attach(img)
img2.add_header('Content-ID', '<image2>')
msg.attach(img2)
s = smtplib.SMTP('smtpserver')
s.sendmail('mycompany#mycompany.com.br',
['myemail#domain.com.br', 'myemail2#domain.com', 'customer#domain.com'],
msg.as_string())
s.quit()
except Exception as ex:
raise ex
send_survey_mail()
I removed the following line from the code and tried again. Now the email is not sent to my customer's Gmail email.
msg['Bcc'] = 'customer#gmail.com'
Just do not mention bcc mails in msg['To'] or msg['Cc']. Do it only in server.sendmail()
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from_addr = "your#mail.com"
to_addr = ["to#mail.com", "to2#mail.com"]
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = "SUBJECT"
body = "BODY"
msg.attach(MIMEText(body, 'plain'))
filename = "FILE.pdf"
attachment = open('/home/FILE.pdf', "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('.....', 587)
server.starttls()
server.login(from_addr, 'yourpass')
text = msg.as_string()
server.sendmail(from_addr, to_addr + [bcc#mail.com], text)
server.quit()
Did you try not to define the msg['BCC'] field? Setting this field forces it to be included. It is sufficient that the BCC email address is in the sendmail command's destination address list. Take a look at this question.
MAIL_FROM = default#server.com
MAIL_DL = default#server.com
def send(to, cc, bcc, subject, text, html):
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = MAIL_FROM
message["To"] = to
message["Cc"] = cc + "," + MAIL_DL
if html is not None:
body = MIMEText(html, "html")
else:
body = MIMEText(text)
message.attach(body)
server = smtplib.SMTP(MAIL_SERVER)
server.set_debuglevel(1)
server.sendmail(MAIL_DL, to.split(",") + bcc.split(","), message.as_string())
server.quit()
return {
"to": to,
"cc": cc,
"bcc": bcc,
"subject": subject,
"text": text,
"html": html,
}