Advertisement
Guest User

Untitled

a guest
May 7th, 2017
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #A simple test program for recieving and sending emails via python
  4. #For others reading this please keep in mind i had to use AUTH PLAIN because i have saslauth
  5. #on my server and no combinations of normal methods via smtplib would allow me to send mail.
  6. #Contact  jony123@jonys.info
  7. #Verson 1
  8. """
  9. I wrote the simple script for checking the status of the inbox
  10.  
  11. Verson 1.1
  12. I added the part that connects checks the status and if there emails  
  13. it fetchs them displays them and delets them.
  14.  
  15. Version 1.2
  16. Added support to email user back.  Added options for differnt auth  
  17. methods not supported by smtplib (i may be wrong)
  18.  
  19. Version 1.3
  20. Emails back with proper MIME stuff.
  21.  
  22. Version 1.4
  23. Phrases for subject and now can act upon it
  24.  
  25. Verson 1.5
  26. Added help command which returns the conents of any file in help/
  27.  
  28. Verson 1.6
  29. Added test command
  30. """
  31.  
  32. import poplib  #For pop
  33. import time   #Time libarary
  34. import smtplib          #The libary for smtp.
  35. from time import strftime    #This is for getting the date
  36. import base64           #This is for saslplaintext. It encodes the username and password
  37. username and password for the server.
  38. from email.mime.text import MIMEText              
  39. from email.mime.image import MIMEImage                 #These are the MIME auto-formating thingys        
  40. from email.mime.multipart import MIMEMultipart    
  41. # Setting for connecting to server.
  42. message = ''#Leave this                    
  43. server = 'someserver.tld'              #Pop server
  44. user = 'someuser'                      #User login for pop and smtp servers
  45. userpass = 'somepass'                  #Password to login to pop and smtp servers
  46. authmethod = "smtplib"                 #Authenication method See below.
  47. """
  48. Support is for smtplib, saslplaintext and none. smtplib uses the normal way to authenicate saslplaintext encodes the username and pass with base64.  Normally it willl probaly be smtplib for most.  none is for if you where sending from localhost and didn't need to authenicate anyway.
  49. """
  50. myemail = 'email@someserver.tld'        #Email address you want to give the bot.
  51.  
  52. ##########################
  53. ########functions#########
  54. ##########################
  55.  
  56. def phraseSmtp( data ):
  57.    userfrom = data[1][0].replace('<', '').replace('>', '').split()[1]         #->-- This function sorts the burst of data and returns the email address that sent the email.
  58.    return userfrom
  59.      
  60. def sendEmail( fromadd, toadd, msg):  
  61.    server = smtplib.SMTP(server,  587)
  62.    if authmethod == 'saslplaintext':  
  63.       server.ehlo()  
  64.       encoded = base64.b64encode('\0'+user+'\0'+userpass)   #Authenicating and sending email via smtp server.  
  65.       server.docmd('AUTH PLAIN '+encoded)                                                        
  66.    if authmethod ==  'smtplib':  
  67.       server.login(user, userpass)
  68.    if authmethod == 'none':
  69.       pass  
  70.    server.sendmail(myemail, fromadd, msg)
  71.    server.quit()
  72.  
  73. def writeEmail(to, fromuser, sub, msg1):  
  74.    # Create the container (outer) email message.
  75.    msg = MIMEText(msg1+"\r\n\nFrom\r\nPyMail")  
  76.    msg['Subject'] = sub      
  77.    msg['From'] = fromuser
  78.    msg['To'] = to
  79.    msg.preamble = msg1  
  80.    return msg
  81.  
  82. def getSubject(data):
  83.    loop = 0
  84.    for x in data:
  85.       found = x.find('Subject')
  86.       if found != -1:
  87.          break
  88.       loop = loop + 1
  89.    return data[loop]
  90.  
  91. def respond(subject):
  92.    phrase = subject.split()
  93.    print phrase
  94.    if phrase [0] == 'help':
  95.       try:
  96.          tore = gethelp(phrase[1])
  97.       except:
  98.          tore = gethelp(phrase[0])
  99.    elif phrase [0] == 'test':
  100.       tore = testfunc(phrase)
  101.    else:
  102.       tore = 'Sorry I\'m not sure what you want.  Next time email me a  
  103. single \'help\' in the subject line to list alll available commands'
  104.    return tore
  105.  
  106. def gethelp(withthis):
  107.    try:
  108.       f = open('help/'+withthis)
  109.       tore = f.read()
  110.       f.close()
  111.    except:
  112.       print "couldnt open help"
  113.       tore = 'Sorry I dont know what your talking about.  Next time  
  114. send me just \'help\' in the subject line to list all available  
  115. commands'
  116.       pass
  117.    return tore
  118.  
  119. def testfunc(text):
  120.    tore = ''
  121.    for x in text:                                                                
  122.       tore = tore+' '+x
  123.    return tore
  124.  
  125. ####################################
  126. ############End Functions###########
  127. ####################################
  128.  
  129. #Start the program
  130. while True:
  131.    conn = poplib.POP3(server)
  132.    print "Connecting to POP3 server"
  133.    conn.user(user)
  134.    conn.pass_(userpass)
  135.    status = conn.stat()
  136.    status = str(status)
  137.    print status
  138.    new = status.replace( '(', '').replace( ')', '').split( ',' ) [ 0 ]
  139.    print new
  140.    newint = int(new)
  141.    print newint
  142.    if newint > 0:
  143.       looper = 1
  144.       while looper <= newint:
  145.          print looper
  146.          data = conn.retr(looper)
  147.          conn.dele(looper)
  148.          print ''
  149.          print data
  150.          print ''
  151.          print phraseSmtp(data)
  152.          message = respond(getSubject(data[1]))
  153.          sendEmail(phraseSmtp(data), 'pymail@jonys.info', writeEmail
  154. (phraseSmtp(data), 'pymail@jonys.info', 'Re: '+getSubject(data[1]),  
  155. message).as_string())
  156.          print ''
  157.          looper = looper + 1
  158.    else:
  159.       print "No new email"
  160.  
  161.  
  162.  
  163.    conn.quit()
  164.    print "COnnection closed"
  165.    print 'Sleeping for 3 minutes'
  166.    print ' '
  167.    print ' '
  168.    time.sleep(180)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement