Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import smtplib
  2. import mimetypes
  3. from email.MIMEMultipart import MIMEMultipart
  4. from email.MIMEBase import MIMEBase
  5. from email.MIMEText import MIMEText
  6. from email.mime.image import MIMEImage
  7. from email.Utils import COMMASPACE, formatdate
  8. from email import Encoders
  9.  
  10.  
  11. filePath = "IMAGE_1.PNG"#your file name
  12.  
  13. From = 'jimmypulikottil@gmail.com'
  14. To = 'e4emerging@gmail.com'
  15.  
  16. msg = MIMEMultipart()
  17. msg['From'] = From
  18. msg['To'] = To
  19. msg['Date'] = formatdate(localtime=True)
  20. msg['Subject'] = 'Sample subject'
  21.  
  22. msg.attach(MIMEText('Sample message'))
  23.  
  24. try:
  25.     smtp = smtplib.SMTP('smtp.gmail.com:587')
  26.     smtp.starttls()
  27.     smtp.login('jimmypulikottil@gmail.com', 'password')
  28. except:
  29.     i = 1
  30. else:
  31.     i = 0
  32.  
  33. if i == 0:
  34.     ctype, encoding = mimetypes.guess_type(filePath)
  35.     if ctype is None or encoding is not None:
  36.         # No guess could be made, or the file is encoded (compressed), so
  37.         # use a generic bag-of-bits type.
  38.         ctype = 'application/octet-stream'
  39.     maintype, subtype = ctype.split('/', 1)
  40.     if maintype == 'text':
  41.         fp = open(filePath)
  42.         # Note: we should handle calculating the charset
  43.         part = MIMEText(fp.read(), _subtype=subtype)
  44.         fp.close()
  45.     elif maintype == 'image':
  46.         fp = open(filePath, 'rb')
  47.         part = MIMEImage(fp.read(), _subtype=subtype)
  48.         fp.close()
  49.     elif maintype == 'audio':
  50.         fp = open(filePath, 'rb')
  51.         part = MIMEAudio(fp.read(), _subtype=subtype)
  52.         fp.close()
  53.     else:
  54.         fp = open(filePath, 'rb')
  55.         part = MIMEBase(maintype, subtype)
  56.         part.set_payload(fp.read())
  57.         fp.close()
  58.         # Encode the payload using Base64
  59.         Encoders.encode_base64(part)
  60.     part.add_header('Content-Disposition', 'attachment; filename="%s"' % filePath)
  61.     msg.attach(part)
  62.     try:
  63.         smtp.sendmail(From, To, msg.as_string())
  64.     except:
  65.         print "Mail not sent"
  66.     else:
  67.         print "Mail sent"
  68.     smtp.close()
  69. else:
  70.     print "Connection failed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement