Python Forum
Send Email with attachment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send Email with attachment
#1
I found this code online to send emails with attachments. Currently I believe it sends "encoded" attachments as the data is unreadable. How can I send the text files as they are with no encoding.

Also I have this piece of code which create text extracts as per the ID given.
At the end of the code when I click y or n an email should be automatically sent out to the email provided as input.how can this be achieved.
Thanks

EMAIL CODE:
import smtplib
import base64

filename = "C:/Users/km041320/MKR/KM111111.txt"
fo = open(filename, "rb")
filecontent = fo.read()

sender = 'SENDER EMAIL'
reciever = 'RECIEVER EMAIL'

marker = "AUNIQUEMARKER"

body ="""
This is a test email to send an attachement.
"""
# Define the main headers.
part1 = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)

# Define the message action
part2 = """Content-Type: text/plain

%s
--%s
""" % (body,marker)

# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"

Content-Disposition: attachment; filename=%s

%s
--%s--
""" %(filename, filename, marker)
message = part1 + part2 + part3

try:
   smtpObj = smtplib.SMTP('smtplb.xxxxxx.com')
   smtpObj.sendmail(sender, reciever, message)
   print ("Successfully sent email")
except Exception:
   print ("Error: unable to send email")

MY CODE:
import datetime
from datetime import datetime
n = 5
while n > 0:
    id = check_id()
    with open("C:\\Users\\ABC\\DEF\\" "%s.txt" % id, "a") as file:
        name = check_name()
        Doctor = check_Doctor()
        ApptDate = check_date()
        phone = check_phone()
        EmailID = input("Please provide your email to confirm your appointment: ")
        dtstamp = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
        file.write(name+ "|" +Doctor+ "|" +ApptDate+  "|" +EmailID+ "|" +phone+ "|" +dtstamp+ "\n")
        MoreData = input("Do you need to make any new Appointments? y/n :")
        if MoreData != "y":
            n= -1
So if the ID is AB12345.It creates a text file AB12345.txt.
And as per the email provided above I should automatically pick the AB12345.txt file once the user clicks y or n at the end of the program and email the AB12345.txt to the email provided.

Also how can I create a small chat window and ask those details in the chat window.
Ideas are appreciated.

Thanks
Reply
#2
Perhaps this thread should be merged with your other one on sending emails, since it's clearly related. Also, please make sure to post code within the relevant tags (which you were told on the other thread), as it's difficult to read it without formatting or syntax highlighting.

In any case, are you setting the correct content type for the attachment?

Regarding GUIs, look at Tkinter in the standard library. The documentation page on the Python website says to keep the library reference under your pillow. You really should refer to it as your first port of call when you want to know if Python has facilities for doing a particular thing, as it offers lots of modules for various common tasks.
Reply
#3
Thanks.I am newbie to python and I am making slow progress every day.
I get stuck sometimes not finding answers easily and come here for advise.
Thanks I will look up Tkinter.
Reply
#4
Thanks
Reply
#5
Hi, with a quick Google search I was able to find this interesting article on sending emails using Python. Check it out and hope it helps :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send an email Jokoba 0 2,543 Mar-12-2020, 05:30 PM
Last Post: Jokoba
  Help send email by Python using smtplib hangme 6 6,184 Jan-25-2020, 03:31 AM
Last Post: shapeg
  Start tls - Unable to send email darunkumar 7 11,581 May-30-2018, 10:43 AM
Last Post: darunkumar

Forum Jump:

User Panel Messages

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