Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----my code------
- filename = str("JDI Offline.txt").split('\')[-1:][0]
- filepath = "C:WindowsJDI Offline.txt"
- fo = open(filepath, "rb")
- filecontent = fo.read()
- fo.close()
- encodedcontent = base64.b64encode(filecontent)
- sender = 'vusvarshil@gmail.com'
- receiver = 'vusvarshil@gmail.com'
- marker = "AUNIQUEMARKER"
- body = """
- Test to send attatchment
- """
- part1 = """From: From Varshil<vusvarshil@gmail.com>
- To:me
- Subject: Sending attatchment
- MIME-Version: 1.0
- Content-Type: multipart/mixed; boundary=%s
- --%s
- """ % (marker, marker)
- part2 = """Content-Type: text/plain
- Content-Transfer-Encoding:8bit
- %s
- --%s
- """ % (body, marker)
- part3 = """Content-Type:multipart/mixed; name="%s"
- Content-Transfer-Encoding:base64
- Content-Disposition: attatchment; filepath=%s
- %s
- --%s--
- """ % (filename, filepath, encodedcontent, marker)
- message = part1 + part2 + part3
- try:
- mail = smtplib.SMTP('smtp.gmail.com', 587)
- mail.ehlo()
- mail.starttls()
- mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
- mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
- print "Mail Sent successfully"
- except Exception:
- print "Error"
- When I use this code in a class i get a blank mail
- class sendmail:
- def send_mail():
- import smtplib
- import base64
- filename = str("JDI Offline.txt").split('\')[-1:][0]
- filepath = "C:WindowsJDI Offline.txt"
- fo = open(filepath, "rb")
- filecontent = fo.read()
- fo.close()
- encodedcontent = base64.b64encode(filecontent)
- sender = 'vusvarshil@gmail.com'
- receiver = 'vusvarshil@gmail.com'
- marker = "AUNIQUEMARKER"
- body = """
- Test to send attatchment
- """
- part1 = """From: From Varshil<vusvarshil@gmail.com>
- To:me
- Subject: Sending attatchment
- MIME-Version: 1.0
- Content-Type: multipart/mixed; boundary=%s
- --%s
- """ % (marker, marker)
- part2 = """Content-Type: text/plain
- Content-Transfer-Encoding:8bit
- %s
- --%s
- """ % (body, marker)
- part3 = """Content-Type:multipart/mixed; name="%s"
- Content-Transfer-Encoding:base64
- Content-Disposition: attatchment; filepath=%s
- %s
- --%s--
- """ % (filename, filepath, encodedcontent, marker)
- message = part1 + part2 + part3
- try:
- mail = smtplib.SMTP('smtp.gmail.com', 587)
- mail.ehlo()
- mail.starttls()
- mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
- mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
- print "Mail Sent successfully"
- except Exception:
- print "Error"
- ob=sendmail()
- ob.send_email()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement