Advertisement
Guest User

Untitled

a guest
Oct 13th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. class SendMail():
  2.  
  3.     #  Create a folder named 'report'
  4.     reportpath = os.path.join(os.getcwd(), 'report')
  5.    
  6.     def getreport(self):
  7.         dirs = os.listdir(self.reportpath)
  8.  
  9.         dirs.sort(key=lambda fn:os.path.getmtime(self.reportpath+'//'+fn))
  10.  
  11.         report = os.path.join(self.reportpath,dirs[-1])
  12.         print (report)
  13.         return (report)
  14.  
  15.     def sendmail(self,report):
  16.        
  17.         smtpserver = 'smtp.gmail.com'
  18.  
  19.         user = 'hjcameron97@gmail.com'
  20.         passwd = 'oyxmnkzsntkukueg'     # here is an Authorization code for your gmail account
  21.         sender = 'hjcameron97@gmail.com'
  22.         receiver = ['hjcameron97@gmail.com']
  23.  
  24.         header = 'Cathodic Protection - Weather Correlation Data'
  25.  
  26.         content = ('<br>The following email contains plots and data surronding\
  27.                    Cathodic Protection data. The email format is still in a prototyping stage,\
  28.                    and will be improved over time.<br>'
  29.                    '<br>\n\r Please refer any feedback to the original sender of the email!<br>')
  30.  
  31.        
  32.         f = open(report,'rb')
  33.         sendfile = f.read()
  34.         f.close()
  35.  
  36.         msgRoot = MIMEMultipart()
  37.         msgRoot.attach(MIMEText(content,'html','utf-8'))
  38.         msgRoot['From'] = sender
  39.         msgRoot['To'] = ','.join(receiver)
  40.         msgRoot['Subject'] = Header(header,'utf-8')
  41.         #msgRoot.attach(att)
  42.        
  43.         msgAlternative = MIMEMultipart('alternative')
  44.         msgRoot.attach(msgAlternative)
  45.        
  46.         msgText = MIMEText('\n\r<b>Cathodic Protection Plots:</b><br><br>'
  47.                    '<br>Weather Data<br>'        
  48.                    '<img src="cid:image1">'
  49.                    '<br>'
  50.                    '<br>'
  51.                    '<br>Cathodic Protection Current Data<br>'
  52.                    '<img src="cid:image2">'
  53.                    '<br>'
  54.                    '<br>', 'html')
  55.  
  56.         msgRoot.attach(msgText)
  57.  
  58.         fp = open('test.png', 'rb')
  59.         msgImage = MIMEImage(fp.read())
  60.         fp.close()
  61.         msgImage.add_header('Content-ID', '<image1>')
  62.         msgRoot.attach(msgImage)
  63.        
  64.         fp = open('cv.png', 'rb')
  65.         msgImage = MIMEImage(fp.read())
  66.         fp.close()
  67.         msgImage.add_header('Content-ID', '<image2>')
  68.         msgRoot.attach(msgImage)
  69.  
  70.         #fp = open('test.png', 'rb')
  71.         #msgImage2 = MIMEImage(fp.read())
  72.         #fp.close()
  73.         #msgImage.add_header('Content-ID', '<image2>')
  74.         #msgRoot.attach(msgImage2)
  75.  
  76.        
  77.         smtp = smtplib.SMTP_SSL(smtpserver, 465)
  78.  
  79.         smtp.helo(smtpserver)
  80.         smtp.ehlo(smtpserver)
  81.  
  82.      
  83.         smtp.login(user, passwd)
  84.         smtp.sendmail(sender, receiver, msgRoot.as_string())
  85.  
  86.         smtp.quit()
  87.  
  88.  
  89. if __name__ == "__main__":
  90.     mail = SendMail()
  91.     report = mail.getreport()
  92.     mail.sendmail(report)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement