Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SendMail():
- # Create a folder named 'report'
- reportpath = os.path.join(os.getcwd(), 'report')
- def getreport(self):
- dirs = os.listdir(self.reportpath)
- dirs.sort(key=lambda fn:os.path.getmtime(self.reportpath+'//'+fn))
- report = os.path.join(self.reportpath,dirs[-1])
- print (report)
- return (report)
- def sendmail(self,report):
- smtpserver = 'smtp.gmail.com'
- user = 'hjcameron97@gmail.com'
- passwd = 'oyxmnkzsntkukueg' # here is an Authorization code for your gmail account
- sender = 'hjcameron97@gmail.com'
- receiver = ['hjcameron97@gmail.com']
- header = 'Cathodic Protection - Weather Correlation Data'
- content = ('<br>The following email contains plots and data surronding\
- Cathodic Protection data. The email format is still in a prototyping stage,\
- and will be improved over time.<br>'
- '<br>\n\r Please refer any feedback to the original sender of the email!<br>')
- f = open(report,'rb')
- sendfile = f.read()
- f.close()
- msgRoot = MIMEMultipart()
- msgRoot.attach(MIMEText(content,'html','utf-8'))
- msgRoot['From'] = sender
- msgRoot['To'] = ','.join(receiver)
- msgRoot['Subject'] = Header(header,'utf-8')
- #msgRoot.attach(att)
- msgAlternative = MIMEMultipart('alternative')
- msgRoot.attach(msgAlternative)
- msgText = MIMEText('\n\r<b>Cathodic Protection Plots:</b><br><br>'
- '<br>Weather Data<br>'
- '<img src="cid:image1">'
- '<br>'
- '<br>'
- '<br>Cathodic Protection Current Data<br>'
- '<img src="cid:image2">'
- '<br>'
- '<br>', 'html')
- msgRoot.attach(msgText)
- fp = open('test.png', 'rb')
- msgImage = MIMEImage(fp.read())
- fp.close()
- msgImage.add_header('Content-ID', '<image1>')
- msgRoot.attach(msgImage)
- fp = open('cv.png', 'rb')
- msgImage = MIMEImage(fp.read())
- fp.close()
- msgImage.add_header('Content-ID', '<image2>')
- msgRoot.attach(msgImage)
- #fp = open('test.png', 'rb')
- #msgImage2 = MIMEImage(fp.read())
- #fp.close()
- #msgImage.add_header('Content-ID', '<image2>')
- #msgRoot.attach(msgImage2)
- smtp = smtplib.SMTP_SSL(smtpserver, 465)
- smtp.helo(smtpserver)
- smtp.ehlo(smtpserver)
- smtp.login(user, passwd)
- smtp.sendmail(sender, receiver, msgRoot.as_string())
- smtp.quit()
- if __name__ == "__main__":
- mail = SendMail()
- report = mail.getreport()
- mail.sendmail(report)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement