Python Forum
PYTHON - send email with attachment Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PYTHON - send email with attachment Error
#1
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


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()
Reply
#2
never mind i found the solution basically this did the trick


filename = 'file.txt'
attachment = open("file path" + filename, "rb")

#Search and you will Find
# I am getting there
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to download TLS Report attachment blason16 6 456 Feb-26-2024, 07:36 AM
Last Post: Pedroski55
  Extract PDF Attachment from Gmail jstaffon 0 536 Sep-10-2023, 01:55 PM
Last Post: jstaffon
  Send Error when attaching files JamesAinsworth 1 554 Aug-07-2023, 11:10 PM
Last Post: deanhystad
  Send email with smtp without using Mimetext mgallotti 0 678 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,267 Nov-14-2022, 09:31 PM
Last Post: malonn
  I get attachment paperclip on email without any attachments monika_v 5 1,914 Mar-19-2022, 10:20 PM
Last Post: cosmarchy
  Unable to send email attachments cosmarchy 7 2,445 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  Trying to determine attachment file type before saving off.. cubangt 1 2,095 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,018 Jan-07-2022, 07:38 PM
Last Post: cubangt
  How to make scraper send email notification just once themech25 0 1,362 Nov-08-2021, 01:51 PM
Last Post: themech25

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020