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()
Related
Dears
I have a Code Connect to Oracle DB, get query as Daraframe and send email to receiver that exist in a column
As I Test When I set test_reciver argument to an email address test_reciver Email received
but when I set (email Receiver) to the column of data frame, Email not received
#send mail function
def send_email(subject,to,cc,body):
sender_email = 'test#test.com'
receivers = to
host = "bulkmail.test.com"
server = smtplib.SMTP(host)
msg = MIMEText(body)
msg['Subject'] = subject
msg['to'] = to
msg['cc'] = cc
server.sendmail(sender_email,to,msg.as_string())
server.quit()
query = "select * from sometable"
test_reciver = "myemail#test.com"
con = cx_Oracle.connect(user_db,password_db,dsn_tns,encoding="UTF-8", nencoding="UTF-8")
cc = 'emailcctest#test.com'
df = pd.read_sql_query(query,con, index_col=None).head(1)
df = df.reset_index()
for index, row in df.iterrows():
print(row['EMAIL'])
subject = 'please clarify your Workgroup in SDM '
to = row['EMAIL']
body = 'sample body'
send_email(subject, to, cc, body)
con.close()
it is solved by sending email as an object with send_message instead of sendmail for smtplib
#send mail function
def send_email(subject,to,cc,body):
host = "bulkmail.test.com"
server = smtplib.SMTP(host)
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = 'test#test.com'
msg['to'] = to
msg['cc'] = email_cc
serve.send_message(msg)
server.quit()
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
I would like to loop through a list and display each item on a separate line in my email.
The code works fine if I set body = "Just a string"
mylist = [first line,second line,third line,fourth line]
fromaddr = "EMAIL"
toaddr = "EMAIL"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "EMAIL SUBJECT"
body = mylist
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("EMAIL", "PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
I tried just making body = to mylist just to see how it looked currently but I get the following error: _text.encode('us-ascii')
AttributeError: 'list' object has no attribute 'encode'
What I would like ultimately is the body of the email to look this (when I receive the email):
first line
second line
third line
fourth line
Thanks to Michael Butscher for the push in the right direction.
This does the job.
body = "\r\n".join(mylist)
Hi there I've the following code:
msg = MIMEMultipart('mixed')
msg['Subject'] = "Test Email"
msg['From'] = "test#test.de"
msg['To'] = "test2#test.de"
msg['Disposition-Notification-To'] = "test#test.de"
if msgid is None:
msgid = make_msgid()
msg['Message-ID'] = msgid
msg.attach(MIMEText(message, content_type))
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo()
server_ssl.login("test#test.de", "test123")
server_ssl.sendmail(user.email, ["test#test.de"], msg.as_string())
server_ssl.close()
It is sending the mail as expected but there is no "delivery receipt". I also tried it with "Return-Receipt-To" which seems to be just another "inofficial" standard...
What am I doing wrong?
I have four python functions that I am using to send mail. If the program does one thing, it mails a set of results to a multiple recipients, if it does another thing, a separate function mails the results to one recipient.
def smail(to,sub,body):
addr_from = 'alert#example.com'
msg = MIMEMultipart()
msg['From'] = addr_from
msg['To'] = to
msg['Subject'] = sub
msg.attach(body)
s = smtplib.SMTP('webmail.example.com')
s.sendmail(addr_from, [to], msg.as_string())
s.quit()
def email_format2(results):
text = ""
text += 'The following applications in <environment> are non-compliant\n'
text += '<br>'
text += 'Here are there names and locations. Please inform the developer.'
text += '<br>'
text += '<br>'
table = pd.DataFrame.from_records(results)
table_str = table.to_html()
text += "%s" % table_str
return text
def mail_results_aq(results):
body = email_format2(results)
msg = MIMEText(body, 'html')
sub = "Placeholder subject"
to = 'email1#example.com, email2#example.com, email3#example.com'
smail(to, sub, msg)
def mail_results_prd(results):
body = email_format2(results)
msg = MIMEText(body, 'html')
sub = "Placeholder subject"
to = 'email4#example.com'
smail(to, sub, msg)
The mail_results_aq function will only email results to the first recipient (email1#example.com).
In other questions similar to this one, I've seen a the recipients being entered into a list i.e
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me#example.com'
recipients = ['john.doe#example.com', 'john.smith#example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
However, this use case only seems to work when used in the same function. How can I implement this feature across the four functions I have above?
Thanks in advance!