Advertisement
Guest User

Untitled

a guest
Aug 9th, 2016
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.50 KB | None | 0 0
  1. import smtplib
  2. from email.mime.multipart import MIMEMultipart
  3. from email.mime.text import MIMEText
  4. import MySQLdb
  5.  
  6. #Database connection to the mysql db hosted at UT
  7. db = MySQLdb.connect(host="mysql.utk.edu", # your host, usually localhost
  8. user="cmawhinn", # your username
  9. passwd="cookies", # your password
  10. db="cmawhinn_pokemonGo") # name of the data base
  11.  
  12. #Global variables for the email
  13. emailTitle = "OIT Ethernet" #This will need to be pulled from admin DB
  14. nameVar = "Colin" #Name of the person that will be emailed
  15. cable1Length = "14ft" #Admin DB
  16. cable2Length = "25ft" #Admin DB
  17. cable1Qty = 1000 #From general DB for xyz week
  18. cable2Qty = 100 #From general DB for xyz week
  19. cableQtyTotal = 110 #From general DB for xyz week, or just sum cable1 and 2Qty
  20.  
  21. def sqldb_info():
  22. cur = db.cursor()
  23. # print all the first cell of all the rows
  24. for row in cur.fetchall():
  25.  
  26. rowCom = str(row[0]) + " " + str(row[1] + " " + str(row[2]) + " " + str(row[3]))
  27. print rowCom
  28.  
  29. nameVar = str(row[3])
  30. db.close()
  31.  
  32.  
  33. #Function py_main decides where to send the mail and connects to the SMTP server
  34. def py_mail(SUBJECT, BODY, TO, FROM):
  35. # Create message container - the correct MIME type is multipart/alternative here!
  36. MESSAGE = MIMEMultipart('alternative')
  37. MESSAGE['subject'] = SUBJECT
  38. MESSAGE['To'] = TO
  39. MESSAGE['From'] = FROM
  40. MESSAGE.preamble = """
  41. Your mail reader does not support the report format.
  42. Please visit <a href="http://www.mysite.com">online</a>!"""
  43.  
  44. # Record the MIME type text/html.
  45. HTML_BODY = MIMEText(BODY, 'html')
  46.  
  47. # Attach parts into message container.
  48. # According to RFC 2046, the last part of a multipart message, in this case
  49. # the HTML message, is best and preferred.
  50. MESSAGE.attach(HTML_BODY)
  51.  
  52. # The actual sending of the e-mail
  53. server = smtplib.SMTP('smtp.gmail.com:587')
  54.  
  55. # Print debugging output when testing
  56. if __name__ == "__main__":
  57. server.set_debuglevel(1)
  58.  
  59. # Credentials (if needed) for sending the mail
  60. password = "6262abcd"
  61.  
  62. server.starttls()
  63. server.login(FROM,password)
  64. server.sendmail(FROM, [TO], MESSAGE.as_string())
  65. server.quit()
  66.  
  67.  
  68. #Main Function: Where the HTML Formated email is stored, and where the email list pulls from
  69. #Email list will be pulled from admin DB
  70. if __name__ == "__main__":
  71.  
  72.  
  73. email_content = """
  74. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  75. <html xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
  76. <head>
  77. <meta name="viewport" content="width=device-width" />
  78. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  79. <title>Billing e.g. invoices and receipts</title>
  80.  
  81.  
  82. <style type="text/css">
  83. img {
  84. max-width: 100%;
  85. }
  86. body {
  87. -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em;
  88. }
  89. body {
  90. background-color: #f6f6f6;
  91. }
  92. @media only screen and (max-width: 640px) {
  93. body {
  94. padding: 0 !important;
  95. }
  96. h1 {
  97. font-weight: 800 !important; margin: 20px 0 5px !important;
  98. }
  99. h2 {
  100. font-weight: 800 !important; margin: 20px 0 5px !important;
  101. }
  102. h3 {
  103. font-weight: 800 !important; margin: 20px 0 5px !important;
  104. }
  105. h4 {
  106. font-weight: 800 !important; margin: 20px 0 5px !important;
  107. }
  108. h1 {
  109. font-size: 22px !important;
  110. }
  111. h2 {
  112. font-size: 18px !important;
  113. }
  114. h3 {
  115. font-size: 16px !important;
  116. }
  117. .container {
  118. padding: 0 !important; width: 100% !important;
  119. }
  120. .content {
  121. padding: 0 !important;
  122. }
  123. .content-wrap {
  124. padding: 10px !important;
  125. }
  126. .invoice {
  127. width: 100% !important;
  128. }
  129. }
  130. </style>
  131. </head>
  132.  
  133. <body itemscope itemtype="http://schema.org/EmailMessage" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">
  134.  
  135. <table class="body-wrap" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
  136. <td class="container" width="600" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;" valign="top">
  137. <div class="content" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
  138. <table class="main" width="100%" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;" bgcolor="#fff"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 20px;" align="center" valign="top">
  139. <table width="100%" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; " valign="top">
  140. <h1 class="aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; box-sizing: border-box; font-size: 32px; color: #000; line-height: 1.2em; font-weight: 500; text-align: center; margin: 40px 0 0;" align="center">"""+ emailTitle +"""</h1>
  141. </td>
  142. </tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
  143. <h2 class="aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif; box-sizing: border-box; font-size: 24px; color: #000; line-height: 1.2em; font-weight: 400; text-align: center; " align="center">"""+ nameVar +""", your weekly report</h2>
  144. </td>
  145. </tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
  146. <table class="invoice" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; text-align: left; width: 80%; margin: 40px auto;">
  147. <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 5px 0;" valign="top">
  148. <table class="invoice-items" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" valign="top">"""+ cable1Length +""" Cables</td>
  149. <td class="alignright" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" align="right" valign="top">"""+ str(cable1Qty) +"""</td>
  150. </tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" valign="top">"""+ cable2Length +""" Cables</td>
  151. <td class="alignright" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 1px; border-top-color: #eee; border-top-style: solid; margin: 0; padding: 5px 0;" align="right" valign="top">"""+ str(cable2Qty) +"""</td>
  152.  
  153. </tr><tr class="total" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="alignright" width="80%" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 2px; border-top-color: #333; border-top-style: solid; border-bottom-color: #333; border-bottom-width: 2px; border-bottom-style: solid; font-weight: 700; margin: 0; padding: 5px 0;" align="right" valign="top">Total</td>
  154. <td class="alignright" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: right; border-top-width: 2px; border-top-color: #333; border-top-style: solid; border-bottom-color: #333; border-bottom-width: 2px; border-bottom-style: solid; font-weight: 700; margin: 0; padding: 5px 0;" align="right" valign="top">"""+ str(cableQtyTotal) +"""</td>
  155. </tr></table></td>
  156. </tr></table></td>
  157. </tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">
  158. <a href="http://cmawhinney.com" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #348eda; text-decoration: underline; margin: 0;">View in browser</a>
  159. </td>
  160. </tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0;" align="center" valign="top">
  161. OIT HelpDesk Cable Distrubtion
  162. </td></tr><tr><td class="content-block aligncenter" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; text-align: center; margin: 0;" align="center" valign="top">
  163. Knoxville, TN
  164. </td>
  165.  
  166. </tr></table></td>
  167. </tr></table><div class="footer" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;">
  168. <table width="100%" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">Questions? Email <a href="mailto:cmawhinn@utk.edu" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; color: #999; text-decoration: underline; margin: 0;">cmawhinn@utk.edu</a></td>
  169. </tr></table></div></div>
  170. </td>
  171. <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
  172. </tr></table></body>
  173. </html>
  174. """
  175.  
  176. FROM ='fridgepy@gmail.com'
  177. emails = []
  178. emails.append("cmawhinn@utk.edu")
  179. lens = len(emails)
  180. count = 0
  181. sqldb_info()
  182. for i in emails:
  183. TO = i
  184. count += 1
  185. py_mail("Test email subject", email_content, TO, FROM)
  186. if i == lens:
  187. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement