Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. -----my code------
  2.  
  3. filename = str("JDI Offline.txt").split('\')[-1:][0]
  4. filepath = "C:WindowsJDI Offline.txt"
  5.  
  6. fo = open(filepath, "rb")
  7. filecontent = fo.read()
  8. fo.close()
  9. encodedcontent = base64.b64encode(filecontent)
  10. sender = 'vusvarshil@gmail.com'
  11. receiver = 'vusvarshil@gmail.com'
  12.  
  13. marker = "AUNIQUEMARKER"
  14.  
  15. body = """
  16. Test to send attatchment
  17. """
  18.  
  19. part1 = """From: From Varshil<vusvarshil@gmail.com>
  20. To:me
  21. Subject: Sending attatchment
  22. MIME-Version: 1.0
  23. Content-Type: multipart/mixed; boundary=%s
  24. --%s
  25. """ % (marker, marker)
  26.  
  27. part2 = """Content-Type: text/plain
  28. Content-Transfer-Encoding:8bit
  29.  
  30. %s
  31. --%s
  32. """ % (body, marker)
  33.  
  34. part3 = """Content-Type:multipart/mixed; name="%s"
  35. Content-Transfer-Encoding:base64
  36. Content-Disposition: attatchment; filepath=%s
  37.  
  38. %s
  39. --%s--
  40. """ % (filename, filepath, encodedcontent, marker)
  41. message = part1 + part2 + part3
  42.  
  43. try:
  44. mail = smtplib.SMTP('smtp.gmail.com', 587)
  45. mail.ehlo()
  46. mail.starttls()
  47. mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
  48. mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
  49. print "Mail Sent successfully"
  50. except Exception:
  51. print "Error"
  52.  
  53. When I use this code in a class i get a blank mail
  54.  
  55. class sendmail:
  56. def send_mail():
  57. import smtplib
  58. import base64
  59.  
  60.  
  61.  
  62.  
  63. filename = str("JDI Offline.txt").split('\')[-1:][0]
  64. filepath = "C:WindowsJDI Offline.txt"
  65.  
  66. fo = open(filepath, "rb")
  67. filecontent = fo.read()
  68. fo.close()
  69. encodedcontent = base64.b64encode(filecontent)
  70. sender = 'vusvarshil@gmail.com'
  71. receiver = 'vusvarshil@gmail.com'
  72.  
  73. marker = "AUNIQUEMARKER"
  74.  
  75. body = """
  76. Test to send attatchment
  77. """
  78.  
  79. part1 = """From: From Varshil<vusvarshil@gmail.com>
  80. To:me
  81. Subject: Sending attatchment
  82. MIME-Version: 1.0
  83. Content-Type: multipart/mixed; boundary=%s
  84. --%s
  85. """ % (marker, marker)
  86.  
  87. part2 = """Content-Type: text/plain
  88. Content-Transfer-Encoding:8bit
  89.  
  90. %s
  91. --%s
  92. """ % (body, marker)
  93.  
  94. part3 = """Content-Type:multipart/mixed; name="%s"
  95. Content-Transfer-Encoding:base64
  96. Content-Disposition: attatchment; filepath=%s
  97.  
  98. %s
  99. --%s--
  100. """ % (filename, filepath, encodedcontent, marker)
  101. message = part1 + part2 + part3
  102.  
  103. try:
  104. mail = smtplib.SMTP('smtp.gmail.com', 587)
  105. mail.ehlo()
  106. mail.starttls()
  107. mail.login('vusvarshil@gmail.com', 'xxxxxxxx')
  108. mail.sendmail('vusvarshil@gmail.com', 'vusvarshil@gmail.com', message)
  109. print "Mail Sent successfully"
  110. except Exception:
  111. print "Error"
  112. ob=sendmail()
  113. ob.send_email()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement