Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #A simple test program for recieving and sending emails via python
- #For others reading this please keep in mind i had to use AUTH PLAIN because i have saslauth
- #on my server and no combinations of normal methods via smtplib would allow me to send mail.
- #Contact jony123@jonys.info
- #Verson 1
- """
- I wrote the simple script for checking the status of the inbox
- Verson 1.1
- I added the part that connects checks the status and if there emails
- it fetchs them displays them and delets them.
- Version 1.2
- Added support to email user back. Added options for differnt auth
- methods not supported by smtplib (i may be wrong)
- Version 1.3
- Emails back with proper MIME stuff.
- Version 1.4
- Phrases for subject and now can act upon it
- Verson 1.5
- Added help command which returns the conents of any file in help/
- Verson 1.6
- Added test command
- """
- import poplib #For pop
- import time #Time libarary
- import smtplib #The libary for smtp.
- from time import strftime #This is for getting the date
- import base64 #This is for saslplaintext. It encodes the username and password
- username and password for the server.
- from email.mime.text import MIMEText
- from email.mime.image import MIMEImage #These are the MIME auto-formating thingys
- from email.mime.multipart import MIMEMultipart
- # Setting for connecting to server.
- message = ''#Leave this
- server = 'someserver.tld' #Pop server
- user = 'someuser' #User login for pop and smtp servers
- userpass = 'somepass' #Password to login to pop and smtp servers
- authmethod = "smtplib" #Authenication method See below.
- """
- 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.
- """
- myemail = 'email@someserver.tld' #Email address you want to give the bot.
- ##########################
- ########functions#########
- ##########################
- def phraseSmtp( data ):
- userfrom = data[1][0].replace('<', '').replace('>', '').split()[1] #->-- This function sorts the burst of data and returns the email address that sent the email.
- return userfrom
- def sendEmail( fromadd, toadd, msg):
- server = smtplib.SMTP(server, 587)
- if authmethod == 'saslplaintext':
- server.ehlo()
- encoded = base64.b64encode('\0'+user+'\0'+userpass) #Authenicating and sending email via smtp server.
- server.docmd('AUTH PLAIN '+encoded)
- if authmethod == 'smtplib':
- server.login(user, userpass)
- if authmethod == 'none':
- pass
- server.sendmail(myemail, fromadd, msg)
- server.quit()
- def writeEmail(to, fromuser, sub, msg1):
- # Create the container (outer) email message.
- msg = MIMEText(msg1+"\r\n\nFrom\r\nPyMail")
- msg['Subject'] = sub
- msg['From'] = fromuser
- msg['To'] = to
- msg.preamble = msg1
- return msg
- def getSubject(data):
- loop = 0
- for x in data:
- found = x.find('Subject')
- if found != -1:
- break
- loop = loop + 1
- return data[loop]
- def respond(subject):
- phrase = subject.split()
- print phrase
- if phrase [0] == 'help':
- try:
- tore = gethelp(phrase[1])
- except:
- tore = gethelp(phrase[0])
- elif phrase [0] == 'test':
- tore = testfunc(phrase)
- else:
- tore = 'Sorry I\'m not sure what you want. Next time email me a
- single \'help\' in the subject line to list alll available commands'
- return tore
- def gethelp(withthis):
- try:
- f = open('help/'+withthis)
- tore = f.read()
- f.close()
- except:
- print "couldnt open help"
- tore = 'Sorry I dont know what your talking about. Next time
- send me just \'help\' in the subject line to list all available
- commands'
- pass
- return tore
- def testfunc(text):
- tore = ''
- for x in text:
- tore = tore+' '+x
- return tore
- ####################################
- ############End Functions###########
- ####################################
- #Start the program
- while True:
- conn = poplib.POP3(server)
- print "Connecting to POP3 server"
- conn.user(user)
- conn.pass_(userpass)
- status = conn.stat()
- status = str(status)
- print status
- new = status.replace( '(', '').replace( ')', '').split( ',' ) [ 0 ]
- print new
- newint = int(new)
- print newint
- if newint > 0:
- looper = 1
- while looper <= newint:
- print looper
- data = conn.retr(looper)
- conn.dele(looper)
- print ''
- print data
- print ''
- print phraseSmtp(data)
- message = respond(getSubject(data[1]))
- sendEmail(phraseSmtp(data), 'pymail@jonys.info', writeEmail
- (phraseSmtp(data), 'pymail@jonys.info', 'Re: '+getSubject(data[1]),
- message).as_string())
- print ''
- looper = looper + 1
- else:
- print "No new email"
- conn.quit()
- print "COnnection closed"
- print 'Sleeping for 3 minutes'
- print ' '
- print ' '
- time.sleep(180)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement