Guest User

Untitled

a guest
Jul 19th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. import smtplib
  4. import os
  5. import re
  6. import MySQLdb
  7. import time
  8. # Random gift recipients for Christmas 2008
  9.  
  10. # SQL : connection to the database :
  11.  
  12. try:
  13. conn = MySQLdb.connect (host = "localhost", user = "######", passwd = "######", db = "noelfam")
  14. except MySQLdb.Error, e:
  15. print "Error %d: %s" % (e.args[0], e.args[1])
  16. sys.exit(1)
  17.  
  18. gifted_ids = ['z'] # Initialization of Recipient List for unicity.
  19.  
  20. # SQL : Retrieval of all members + Execution de la boucle principale
  21. cursor = conn.cursor()
  22.  
  23. once = "false"
  24.  
  25. for i in range(1,6):
  26. if i == 1:
  27. current_fam_id = 3
  28. if i == 2:
  29. current_fam_id = 1
  30. if i == 3:
  31. current_fam_id = 2
  32. if i == 4:
  33. current_fam_id = 4
  34. if i == 5:
  35. current_fam_id = 5
  36.  
  37. if once == "true":
  38. cursor.execute ("""SELECT * FROM membres WHERE id_membre <> '18' ORDER BY RAND() LIMIT 1""")
  39. print "once == true"
  40. print "ID : 18 : ODETTE:"
  41.  
  42. else:
  43. cursor.execute ("""SELECT * FROM membres WHERE family = %s ORDER BY RAND()""",(current_fam_id))
  44. print "once == false"
  45.  
  46. print "I :::::::::: ============= %d" % i
  47.  
  48. while(1):
  49. row = cursor.fetchone()
  50. if row != None:
  51. print "_____________________________"
  52. id_membre = int(row[0])
  53. name = row[1]
  54. fam_id = int(row[2])
  55. mail = row[3]
  56. print "DONNEUR :: %s :: id %s :: fam: %s" % (name, id_membre, fam_id)
  57. cursor1 = conn.cursor() # A second cursor for imbricated SQL queries
  58.  
  59. print "I :::::::::::::: %d" % i
  60.  
  61. if once == "true":
  62. print "once == true again"
  63. once = "false"
  64. print "%s" % once
  65. try:
  66. cursor1.execute ("""SELECT name, id_membre FROM membres WHERE id_membre = '18' """)
  67. except MySQLdb.Error, e:
  68. print "Error %d: %s" % (e.args[0], e.args[1])
  69. else:
  70. print "once == false now..."
  71. try:
  72. cursor1.execute("""SELECT name, id_membre FROM membres WHERE family <> %s AND id_membre <> %s ORDER BY RAND() LIMIT 1""",(fam_id, id_membre))
  73. except MySQLdb.Error, e:
  74. print "Error %d: %s" % (e.args[0], e.args[1])
  75.  
  76.  
  77. gifted_row = cursor1.fetchone()
  78.  
  79. if gifted_row[1] in gifted_ids:
  80. while( gifted_row[1] in gifted_ids ):
  81. # print "CHECKING FOR OTHER DESTINAID..."
  82. try:
  83. cursor1.execute ("""SELECT name, id_membre FROM membres WHERE family <> %s AND id_membre <> %s ORDER BY RAND() LIMIT 1""", (fam_id, id_membre))
  84. except MySQLdb.Error, e:
  85. print "Error %d: %s" % (e.args[0], e.args[1])
  86. gifted_row = cursor1.fetchone()
  87.  
  88. gifted_ids.append(gifted_row[1])
  89. print "Identifiant du RECEVEUR : %s" % gifted_row[1]
  90. print "%s offre un cadeau a %s" % (row[1], gifted_row[0])
  91.  
  92.  
  93.  
  94. #MAIL SENDING (TESTING)
  95.  
  96. # EMAIL Handling Part
  97. SMTPserver = 'smtp.scarlet.be'
  98. sender = '#####@scarlet.be'
  99. USERNAME = "########"
  100. PASSWORD = "########"
  101.  
  102.  
  103. text_subtype = 'plain' # typical values for text_subtype are plain, html, xml
  104.  
  105. #destination = ['###########@hotmail.com']
  106. destination = mail
  107. content="""\
  108. Bonjour/Bonsoir %s, Voici le courriel de Nowel 2009
  109.  
  110. Alors, tu devras trouver un chouette petit cadeau pour %s
  111. Hohoho...Joyeux Noel. J'espere qu'il y aura de la neige :D. Y en avais pas l'annee derniere. Et il n'y a toujours pas d'accents dans mes mails. ho ho ho.
  112.  
  113. Encore une fois, joyeux Noel.
  114.  
  115. L'ordinateur de Francois.
  116.  
  117. PS: Francois m'embete ! Il n'arrete pas de me surcharger d'informations et en plus il m'a overclocke. Et si je chauffe de trop, il s'en fout on dirait...
  118. Enfin, voila, ce message est de la part de l'ordinateur de Francois..
  119. """ % (name, gifted_row[0])
  120.  
  121. subject = "Nowel 2009 pour %s. Pour les cadeaux..." % (name)
  122.  
  123. #from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
  124. from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
  125. from email.MIMEText import MIMEText
  126.  
  127. msg = MIMEText(content, text_subtype)
  128. msg['Subject']= subject
  129.  
  130.  
  131. connsmtp = SMTP(SMTPserver)
  132. connsmtp.set_debuglevel(False)
  133. connsmtp.login(USERNAME, PASSWORD)
  134.  
  135. connsmtp.sendmail(sender, destination, msg.as_string())
  136. time.sleep(5)
  137. else:
  138. break
  139.  
  140. # SQL : Closing MySQL connection.
  141. conn.close()
Add Comment
Please, Sign In to add comment