I am trying to attach a txt file to my email I am creating in python. Anyone know how I can attach a txt file to the email?
#....snip
outFile = open(r"C:\Users\\TestFile.text", "w")
# return all rows
with arcpy.da.SearchCursor(fc, ['*'], where_clause=expression) as cursor:
# return specific rows
#with arcpy.da.SearchCursor(fc, [class_field, name_field], where_clause=expression) as cursor:
for row in cursor:
## Print the name of the residential road
#print('{0}, {1}'.format(row[0], row[1]))
print(row)
zval = str('{0}, {1}'.format(row[0], row[1]))
outFile.write(zval + "\n")
outFile.close()
#===SET EMAIL To AND From==============================
FROMADDR = "me@somewherea.gov"
TOADDRS = ["me@somewherea.gov"]
#===BUILD EMAIL========================================
def msgEmail():
msg = "Test"
server = smtplib.SMTP('smtp0.sitevision.com',25)
server.set_debuglevel(1)
server.ehlo()
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
return "Email Sent but Import Aborted"
#===PROCESS EMAIL======================================
getMsgP = msgEmail()
print getMsgP
Solved! Go to Solution.
Here's an example, but really all I'm doing here is doing your web search for you:
email - Attach a txt file in Python smtplib - Stack Overflow
What have you tried so far? I see lots of search results searching for "python email attachment".
#===BUILD EMAIL========================================
def msgEmail():
#msg = "Test"
msg = MIMEMultipart()
msg = "Test"
part = MIMEBase('application', "octet-stream")
part.set_payload(open("TestFile.txt", "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="TestFile.txt"')
msg.attach(part)
server = smtplib.SMTP('smtp0.xx.com',25)
server.set_debuglevel(1)
server.ehlo()
#server.sendmail(FROMADDR, TOADDRS, msg)
server.sendmail(FROMADDR, TOADDRS, msg.as_string())
server.quit()
return "Email Sent but Import Aborted"
#===PROCESS EMAIL======================================
getMsgP = msgEmail()
print getMsgP
Here's an example, but really all I'm doing here is doing your web search for you:
email - Attach a txt file in Python smtplib - Stack Overflow
I was all over that example earlier...just could not get the syntax right....
Think I got it running...Is there a way to release the txt document after I run the code? The only way I can delete it is if I close my Python Window. On Delete attempt I get "The Action cant be completed because the file is open in pythonw.exe"
Working Code:
filename = "TestFile.txt"
f = file(filename)
#===SET EMAIL To AND From==============================
FROMADDR = "xxx@xxx.gov"
TOADDRS = ["xxx@xxx.gov"]
#===BUILD EMAIL========================================
def msgEmail():
#msg = "Test"
msg = MIMEMultipart()
#msg = "Test"
body = "This is the message"
content = MIMEText(body, 'plain')
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attachment)
server = smtplib.SMTP('smtp0.xx.com',25)
server.set_debuglevel(1)
server.ehlo()
#server.sendmail(FROMADDR, TOADDRS, msg)
server.sendmail(FROMADDR, TOADDRS, msg.as_string())
server.quit()
return "Email Sent but Import Aborted"
#===PROCESS EMAIL======================================
getMsgP = msgEmail()
print getMsgP
Have you tried f.close()?