I came up with the following python to send an email with attachment. the text file is located c:/python/20180227.txt (the dated file is changed daily based on current date, so tomorroow it would be 20180228.txt and so forth). When i run the below the attached text is showing
up IN MY EMAIL AS "CPYTHON20180227.txt". instead of 20180227.txt . Can someone please assist
up IN MY EMAIL AS "CPYTHON20180227.txt". instead of 20180227.txt . Can someone please assist
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders from datetime import datetime datestring = datetime.strftime(datetime.now(), '%Y%M%D') email_user = '[email protected]' email_send = '[email protected]' subject = 'Python!' msg = MIMEMultipart() msg['From'] = email_user msg['To'] = email_send msg['Subject'] = subject body = 'Just testing the email server from Python' msg.attach(MIMEText(body,'plain')) filename = "C:/PYTHON/" + datestring + '.txt' #### i believe this is where my error is ????? attachment = open(filename, 'rb') #enter file path as filename part = MIMEBase('application','octet-stream') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header('Content-Disposition' "attachment; filename= "+filename) msg.attach(part) text= msg.as_string() server = smtplib.SMTP('email server', port) server.starttls() server.login(email_user, "password" ) message = 'Test from Python' server.sendmail(email_user, email_send, text) server.quit()