Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Annoy those who annoy you

By: a guest on Feb 24th, 2013  |  syntax: Python  |  size: 7.27 KB  |  views: 2,803  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. '''
  2. This program was written to send one string of text as numerous text messages through a gmail account.
  3. THIS REQUIRES A GMAIL ACCOUNT FOR THIS TO WORK.
  4. Also make sure there is a providers file in the same folder as gmail.py and annoy.py
  5. The information needed for the providers.txt file can be found at:
  6. https://en.wikipedia.org/wiki/List_of_SMS_gateways
  7.  
  8. An example of the format of the providers.txt:
  9. att=@txt.att.net
  10.  
  11. To correctly use the GUI, just hit enter after you have filled in the text box. After enter is pressed on the last text box,
  12. the program will run, and once it is finished the GUI will close.
  13. '''
  14.  
  15. #imports needed for mail()
  16. import smtplib
  17. from email.MIMEMultipart import MIMEMultipart
  18. from email.MIMEText import MIMEText
  19.  
  20. #imports needed for the GUI
  21. import wx
  22.  
  23. #setting up variables
  24. userItems=[]
  25. items=[]
  26. userInfo=[]
  27. service={}
  28.  
  29. class getUserData(wx.Frame):
  30.     """ Get the information to send a text """
  31.     def __init__(self, parent, title):
  32.         wx.Frame.__init__(self, parent, title=title, size=(300,300))
  33.        
  34.         menuBar=wx.MenuBar()
  35.         first=wx.Menu()
  36.         second=wx.Menu()
  37.        
  38.         first.Append(wx.NewId(),'New Window','This is a new Window')
  39.         first.Append(wx.NewId(),'Open...','This opens a new menu')
  40.        
  41.         menuBar.Append(first,'File')
  42.         menuBar.Append(second,'Edit')
  43.        
  44.         self.SetMenuBar(menuBar)
  45.        
  46.  
  47.         #Close window when X is pressed
  48.         self.Bind(wx.EVT_CLOSE, self.closewindow)
  49.        
  50.         # create the main sizer
  51.         self.mainSizer = wx.BoxSizer(wx.VERTICAL)
  52.  
  53.         # Add a panel so it looks the correct on all platforms
  54.         self.panel = wx.Panel(self, wx.ID_ANY)
  55.  
  56.         self.lbls = ["Number:", "Provider:", "Number of text to be sent:","Message:"]
  57.         for lbl in self.lbls:
  58.             self.buildLayout(lbl)
  59.         self.panel.SetSizer(self.mainSizer)
  60.            
  61.         self.Center()   #Centers the window
  62.         self.Show()
  63.        
  64.     def closewindow(self, event):
  65.         self.Destroy()
  66.  
  67.     def buildLayout(self, text):
  68.         """"""
  69.         lblSize = (160,-1)
  70.         lbl = wx.StaticText(self.panel, label=text, size=lblSize)
  71.         self.text=text
  72.         txt = wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER)
  73.         txt.Bind(wx.EVT_TEXT_ENTER, self.txtControl, txt)
  74.  
  75.         sizer = wx.BoxSizer(wx.HORIZONTAL)
  76.         sizer.Add(lbl, 0, wx.ALL|wx.ALIGN_LEFT, 5)
  77.         sizer.Add(txt, 0, wx.ALL, 5)
  78.         self.mainSizer.Add(sizer)
  79.    
  80.     def txtControl(self,event):
  81.         item=event.GetString()
  82.         items.append(item)
  83.        
  84.         if len(items)==4:
  85.                        
  86.             try:
  87.                 service[items[1]]
  88.             except KeyError:
  89.                 print "No such service provider in file"
  90.            
  91.             #defining the variables to make them easier to work with
  92.             number=int(items[0])
  93.             provider=str(items[1])
  94.             count=int(items[2])
  95.             message=str(items[3])
  96.            
  97.             email='%d%s' %(number,service[provider])
  98.            
  99.             i=0
  100.             while i<count:
  101.                 #userInfo is the username and password, which is set in readInFiles()
  102.                 mail(userInfo[0],userInfo[1],email,"",message)
  103.                 i=i+1
  104.            
  105.             self.Close()
  106.            
  107.         else:
  108.             event.EventObject.Navigate()
  109.  
  110. class getUserEmail(wx.Frame):
  111.         """ If it's the first time running the program, get the username and password"""
  112.         def __init__(self, parent, title):
  113.             wx.Frame.__init__(self, parent, title=title, size=(300,300))
  114.              
  115.             #Close window when X is pressed
  116.             self.Bind(wx.EVT_CLOSE, self.closewindow)
  117.            
  118.             # create the main sizer
  119.             self.mainSizer = wx.BoxSizer(wx.VERTICAL)
  120.    
  121.             # Add a panel so it looks the correct on all platforms
  122.             self.panel = wx.Panel(self, wx.ID_ANY)
  123.    
  124.             self.lbls = ["Username:", "Password:"]
  125.             for lbl in self.lbls:
  126.                 self.buildLayout(lbl)
  127.             self.panel.SetSizer(self.mainSizer)
  128.                
  129.             self.Center()   #Centers the window
  130.             self.Show()
  131.        
  132.         def closewindow(self, event):
  133.             self.Destroy()
  134.  
  135.         def buildLayout(self, text):
  136.             lblSize = (160,-1)
  137.             lbl = wx.StaticText(self.panel, label=text, size=lblSize)
  138.             self.text=text
  139.             txt = wx.TextCtrl(self.panel,style=wx.TE_PROCESS_ENTER)
  140.             txt.Bind(wx.EVT_TEXT_ENTER, self.txtControl, txt)
  141.    
  142.             sizer = wx.BoxSizer(wx.HORIZONTAL)
  143.             sizer.Add(lbl, 0, wx.ALL|wx.ALIGN_LEFT, 5)
  144.             sizer.Add(txt, 0, wx.ALL, 5)
  145.             self.mainSizer.Add(sizer)
  146.    
  147.         def txtControl(self,event):
  148.             item=event.GetString()
  149.             userItems.append(item)
  150.             event.EventObject.Navigate()
  151.             if len(userItems)==2:
  152.                 userItems[0]='%s@gmail.com' %(userItems[0],)
  153.                 with open("username.txt",'w') as f:
  154.                     for item in userItems:
  155.                         f.write("%s\n" %(str(item),))
  156.                    
  157.                 self.Close()
  158.  
  159. def mail(gmail_user,gmail_pwd,to, subject, text):
  160.     '''I did not write this portion of the program. I found it online some time ago and made modifications to suite my needs.
  161.    The original code can be found here:
  162.    http://kutuma.blogspot.ca/2007/08/sending-emails-via-gmail-with-python.html
  163.    
  164.    '''
  165.    
  166.     msg = MIMEMultipart()
  167.  
  168.     msg['From'] = gmail_user
  169.     msg['To'] = to
  170.     msg['Subject'] = subject
  171.    
  172.     msg.attach(MIMEText(text))
  173.    
  174.        
  175.     mailServer = smtplib.SMTP("smtp.gmail.com", 587)
  176.     mailServer.ehlo()
  177.     mailServer.starttls()
  178.     mailServer.ehlo()
  179.     mailServer.login(gmail_user, gmail_pwd)
  180.     mailServer.sendmail(gmail_user, to, msg.as_string())
  181.     # Should be mailServer.quit(), but that crashes...
  182.     mailServer.close()
  183.  
  184. def readInFiles():
  185.     '''Read in the service providers information and the username and password'''
  186.     #List of some service providers listed as a dictionary
  187.     with open("providers.txt", 'r') as f:
  188.         for line in f:
  189.             k, v = line.strip().split('=')
  190.             service[k.strip()] = v.strip()
  191.    
  192.     with open("username.txt",'r') as f:
  193.         for line in f:
  194.             userInfo.append(line)
  195.    
  196. def makeUserFile():
  197.     '''If there is no file called username.txt, then this will make one, prompting for a username and password '''
  198.     app=wx.App()
  199.     getUserEmail(None,'Username and Password')
  200.     app.MainLoop()
  201.  
  202. def main():
  203.     ''' Main loop for sending the text '''
  204.     app=wx.App()
  205.     getUserData(None,'Annoy those who annoy you')
  206.     app.MainLoop()
  207.  
  208. if __name__=='__main__':
  209.    
  210.     # Tries to read in the providers, username, and password.
  211.     try:
  212.         readInFiles()
  213.     # If there is no username.txt, it will make one then read it in. This should only have to run the first time you run the program.
  214.     except IOError:
  215.         makeUserFile()
  216.         readInFiles()
  217.     # Now that the program has all the information about your email, it will ask about the text message details.
  218.     main()