python smtplib: how to use Disposition-Notification-To - python

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?

Related

smtplib not sending mails to CC recipients

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

Displaying contents of list in body of email with Python and smtplib

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)

How to email multiple rows from sqlite3 query with python email widget

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()

Python: send email to multiple addresses

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!

Not able to change the value of instance in python

I am trying to use the MIME and send the email using the following code. But the python is not able to change the value of the msg['Subject']. What I am doing wrong? Any suggestions?
Thanks
msg = MIMEText('MAIL from my python programme')
msg['Subject'] = 'Testing '
msg['From'] = USERNAME
msg['To'] = MAILTO
test_var=1
if(test_var):
msg['Subject'] = 'Test variable is 1'
else:
msg['Subject'] = 'Test variable is 0'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()
print "sending email"
From what I can tell, you have to delete the Subject field (or any other) before you replace it. Otherwise, it just appends more subjects - which might be ignored, looking like the value doesn't change.
test_var=1
del msg['Subject']
if(test_var):
msg['Subject'] = 'Test variable is 1'
else:
msg['Subject'] = 'Test variable is 0'

Categories