def create_mail(self): if self.mail_data: qs = ", ".join(self.mail_data.keys()) subject = MAIL_SUBJECT.format(qs) msg = Template(MAIL_TEMPLATE).render(queries=self.mail_data, urls=self.mail_urls) return subject, msg else: print("Error in creating mail") sys.exit()
def main(): for url in TARGET_URLS: message = '' status_code = None try: response = urllib2.urlopen(url, timeout=TIME_OUT_SECOND) status_code = response.getcode() except: message = URL_OPEN_ERROR_MESSAGE.format( url=url, info=sys.exc_info(), datetime=datetime.now().strftime(DATETIME_FORMAT)) else: if status_code == 200: print('{url:32s} - OK'.format(url=url)) else: message += STATUS_CODE_ERROR_MESSAGE.format( url=url, datetime=datetime.now().strftime(DATETIME_FORMAT), status_code=response.getcode(), response_info=response.info() ) if message: print(message) for email in EMAILS: subject = MAIL_SUBJECT.format(url=url) text = message msg = MIMEText(text.encode(MAIL_CHARSET), "plain", MAIL_CHARSET) msg["Subject"] = Header(subject, MAIL_CHARSET) msg["From"] = FROM_ADDRESS msg["To"] = email msg["Date"] = formatdate(localtime=True) smtp = smtplib.SMTP(SMTP_HOST, SMTP_PORT) smtp.sendmail(FROM_ADDRESS, email, msg.as_string()) smtp.close()