Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import smtplib
  2. import mimetypes
  3. from email.mime.multipart import MIMEMultipart
  4. from email import encoders
  5. from email.message import Message
  6. from email.mime.audio import MIMEAudio
  7. from email.mime.base import MIMEBase
  8. from email.mime.image import MIMEImage
  9. from email.mime.text import MIMEText
  10. import datetime
  11.  
  12.  
  13. import xlwt
  14.  
  15. from sqlalchemy import create_engine
  16. from sqlalchemy.orm import scoped_session, sessionmaker
  17.  
  18. # Create connection with MySQL database
  19. cur = create_engine('mysql+pymysql://root:user1234@127.0.0.1:3306/atlantip?charset=utf8&local_infile=1')
  20. #cur = create_engine('mysql+pymysql://straqr:straqr2018@straqr.cicypd6ywu7v.ap-south-1.rds.amazonaws.com:3306/dj?charset=utf8&local_infile=1')
  21. db = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=cur ))
  22.  
  23.  
  24.  
  25.  
  26.  
  27. # create excel sheet for notification
  28.  
  29. def notification_rule():
  30.  
  31. wb = xlwt.Workbook(encoding='utf-8')
  32. sel_q = "SELECT patent_title , territory , p_type , publication_no, registration_date, registration_no, next_annuity, annuity_no , owner_name , group_name , agent_name FROM atlip_patentportfolio where datediff( next_annuity , curdate())"
  33.  
  34. sel = cur.execute(sel_q).fetchall()
  35.  
  36.  
  37. if len(sel)>0 :
  38. ws = wb.add_sheet('Patent', cell_overwrite_ok=True)
  39. row_num = 0
  40. font_style = xlwt.XFStyle()
  41. font_style.font.bold = True
  42.  
  43. columns = ["Patent Title" , "Territory" , "Type" , "Publication Number" , "Registration Date" , "Registration Number" , "Next Annuity" , "Annuity Number", "Owner" , "Group" , "Agent"]
  44.  
  45.  
  46. for col_num in range(len(columns)):
  47. #print(columns[col_num])
  48. ws.write(row_num, col_num, columns[col_num], font_style)
  49.  
  50. # Writing data in excel
  51. row_num = 0
  52. font_style = xlwt.XFStyle()
  53.  
  54. for s in sel :
  55. row_num += 1
  56.  
  57. for col_num in range(len(s)):
  58. #print(str(s[col_num]))
  59. ws.write(row_num, col_num, str(s[col_num]), font_style)
  60.  
  61. wb.save('Email_notifications.xls')
  62.  
  63. else :
  64. pass
  65.  
  66.  
  67. # return response
  68. notification_rule()
  69.  
  70.  
  71.  
  72.  
  73. """emailfrom = "info@socialtraqr.com"
  74. emailto = "rajat@socialtraqr.com"
  75. fileToSend = "hi.xlsx"
  76. username = "info@socialtraqr.com"
  77. password = "xsw2#EDC"
  78.  
  79. msg = MIMEMultipart()
  80. msg["From"] = "info@socialtraqr.com"
  81. msg["To"] = "rajat@socialtraqr.com"
  82. msg["Subject"] = "here we are sending you an attachment"
  83. msg.preamble = "here we are sending you an attachment"
  84.  
  85. ctype, encoding = mimetypes.guess_type(fileToSend)
  86. if ctype is None or encoding is not None:
  87. ctype = "application/octet-stream"
  88.  
  89. maintype, subtype = ctype.split("/", 1)
  90.  
  91. if maintype == "text":
  92. fp = open(fileToSend)
  93. # Note: we should handle calculating the charset
  94. attachment = MIMEText(fp.read(), _subtype=subtype)
  95. fp.close()
  96. elif maintype == "image":
  97. fp = open(fileToSend, "rb")
  98. attachment = MIMEImage(fp.read(), _subtype=subtype)
  99. fp.close()
  100. elif maintype == "audio":
  101. fp = open(fileToSend, "rb")
  102. attachment = MIMEAudio(fp.read(), _subtype=subtype)
  103. fp.close()
  104. else:
  105. fp = open(fileToSend, "rb")
  106. attachment = MIMEBase(maintype, subtype)
  107. attachment.set_payload(fp.read())
  108. fp.close()
  109. encoders.encode_base64(attachment)
  110. attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
  111. msg.attach(attachment)
  112.  
  113. server = smtplib.SMTP("smtp.gmail.com:587")
  114. server.starttls()
  115. server.login(username,password)
  116. server.sendmail(emailfrom, emailto, msg.as_string())
  117. server.quit()
  118. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement