-2

Below is the program to send email with attachment:

I want to rename the file student.xlsx to student_MMDDYYYY.xlsx and send email with renamed file and after email is sent I want to delete that file. How can I do that?

Here is my code:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "MYEMAILID"
toaddr = "TOADDRESS" 
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Please find the attachment"
body = "HI" 
msg.attach(MIMEText(body, 'plain')) 
filename = "student.xlsx"
dt = str(datetime.datetime.now())
attachment = open("C:\\Users\\prashanth\\Desktop\\student.xlsx", "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('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "Password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Mohd
  • 5,523
  • 7
  • 19
  • 30
john
  • 119
  • 1
  • 8

1 Answers1

0

You can use os.rename() to rename a file, and os.remove() to remove a file. And to get the desired date format you can use strftime(), for example:

import os
from datetime import datetime
date_now = datetime.now().strftime('%m%d%Y')
file_one = "C:\\Users\\prashanth\\Desktop\\student.xlsx"
file_two = 'C:\\Users\\prashanth\\Desktop\\student_{}.xlsx'.format(date_now)
os.rename(file_one, file_two)
# send your email and attach the file
# and once you're done:
os.remove(file_two)

EDIT:

You need to close() the file before renaming or removing it, or better yet use with-statement to open the file and auto close it when you're done, for example:

attachment = open(file, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
attachment.close()

or

with open(file, "rb") as attachment:
    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
Mohd
  • 5,523
  • 7
  • 19
  • 30
  • Email is sent but am not able to remove the file :(Traceback (most recent call last): File "C:\Users\prashanth\Desktop\Phython program\gmail1.py", line 33, in os.remove(file_two) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\prashanth\\Desktop\\student_08152017.xlsx' – john Aug 15 '17 at 10:48
  • That is because you `open()` the file and didn't `close()` it, so you either use `close()` after reading the file or you can open the file using `with open(file) as attachment:` – Mohd Aug 15 '17 at 10:51
  • Check the edited answer – Mohd Aug 15 '17 at 10:56
  • It worked thanks a lot !! but the file which was sent was not renamed to date format and i used .format(date_now) in the following line and it worked ! filename = "student_{}.xlsx".format(date_now) – john Aug 15 '17 at 10:58
  • I thought you were referring to file_two when attaching, missed `filename=` line. In that case you don't need to `os.rename()` in the first place, `filename = "student_{}.xlsx".format(date_now)` would be enough – Mohd Aug 15 '17 at 11:06
  • thats great !! Thank u all so much :) – john Aug 15 '17 at 11:07
  • Glad to help :-) – Mohd Aug 15 '17 at 11:08