Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import MySQLdb
- #Database connection to the mysql db hosted at UT
- db = MySQLdb.connect(host="mysql.utk.edu", # your host, usually localhost
- user="cmawhinn", # your username
- passwd="cookies", # your password
- db="cmawhinn_pokemonGo") # name of the data base
- #Global variables for the email
- emailTitle = "OIT Ethernet" #This will need to be pulled from admin DB
- nameVar = "Colin" #Name of the person that will be emailed
- cable1Length = "14ft" #Admin DB
- cable2Length = "25ft" #Admin DB
- cable1Qty = 1000 #From general DB for xyz week
- cable2Qty = 100 #From general DB for xyz week
- cableQtyTotal = 110 #From general DB for xyz week, or just sum cable1 and 2Qty
- def sqldb_info():
- cur = db.cursor()
- # print all the first cell of all the rows
- for row in cur.fetchall():
- rowCom = str(row[0]) + " " + str(row[1] + " " + str(row[2]) + " " + str(row[3]))
- print rowCom
- nameVar = str(row[3])
- db.close()
- #Function py_main decides where to send the mail and connects to the SMTP server
- def py_mail(SUBJECT, BODY, TO, FROM):
- # Create message container - the correct MIME type is multipart/alternative here!
- MESSAGE = MIMEMultipart('alternative')
- MESSAGE['subject'] = SUBJECT
- MESSAGE['To'] = TO
- MESSAGE['From'] = FROM
- MESSAGE.preamble = """
- Your mail reader does not support the report format.
- Please visit <a href="http://www.mysite.com">online</a>!"""
- # Record the MIME type text/html.
- HTML_BODY = MIMEText(BODY, 'html')
- # Attach parts into message container.
- # According to RFC 2046, the last part of a multipart message, in this case
- # the HTML message, is best and preferred.
- MESSAGE.attach(HTML_BODY)
- # The actual sending of the e-mail
- server = smtplib.SMTP('smtp.gmail.com:587')
- # Print debugging output when testing
- if __name__ == "__main__":
- server.set_debuglevel(1)
- # Credentials (if needed) for sending the mail
- password = "6262abcd"
- server.starttls()
- server.login(FROM,password)
- server.sendmail(FROM, [TO], MESSAGE.as_string())
- server.quit()
- #Main Function: Where the HTML Formated email is stored, and where the email list pulls from
- #Email list will be pulled from admin DB
- if __name__ == "__main__":
- email_content = """
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <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;">
- <head>
- <meta name="viewport" content="width=device-width" />
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Billing e.g. invoices and receipts</title>
- <style type="text/css">
- img {
- max-width: 100%;
- }
- body {
- -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em;
- }
- body {
- background-color: #f6f6f6;
- }
- @media only screen and (max-width: 640px) {
- body {
- padding: 0 !important;
- }
- h1 {
- font-weight: 800 !important; margin: 20px 0 5px !important;
- }
- h2 {
- font-weight: 800 !important; margin: 20px 0 5px !important;
- }
- h3 {
- font-weight: 800 !important; margin: 20px 0 5px !important;
- }
- h4 {
- font-weight: 800 !important; margin: 20px 0 5px !important;
- }
- h1 {
- font-size: 22px !important;
- }
- h2 {
- font-size: 18px !important;
- }
- h3 {
- font-size: 16px !important;
- }
- .container {
- padding: 0 !important; width: 100% !important;
- }
- .content {
- padding: 0 !important;
- }
- .content-wrap {
- padding: 10px !important;
- }
- .invoice {
- width: 100% !important;
- }
- }
- </style>
- </head>
- <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">
- <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>
- <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">
- <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;">
- <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">
- <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">
- <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>
- </td>
- </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">
- <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>
- </td>
- </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">
- <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;">
- <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">
- <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>
- <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>
- </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>
- <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>
- </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>
- <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>
- </tr></table></td>
- </tr></table></td>
- </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">
- <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>
- </td>
- </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">
- OIT HelpDesk Cable Distrubtion
- </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">
- Knoxville, TN
- </td>
- </tr></table></td>
- </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;">
- <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>
- </tr></table></div></div>
- </td>
- <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>
- </tr></table></body>
- </html>
- """
- FROM ='fridgepy@gmail.com'
- emails = []
- emails.append("cmawhinn@utk.edu")
- lens = len(emails)
- count = 0
- sqldb_info()
- for i in emails:
- TO = i
- count += 1
- py_mail("Test email subject", email_content, TO, FROM)
- if i == lens:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement