class EmailLogger(object): def __init__(self, config): user = config.get_option(CFG_KEY, CFG_USER) password = config.get_option(CFG_KEY, CFG_PASSWORD) host = config.get_option(CFG_KEY, CFG_HOST) port = config.get_option(CFG_KEY, CFG_PORT, optional=True)\ or DEFAULT_PORT tls = config.get_option(CFG_KEY, CFG_TLS, optional=True) or DEFAULT_TLS ssl = config.get_option(CFG_KEY, CFG_SSL, optional=True) or DEFAULT_SSL self.to = config.get_option(CFG_KEY, CFG_TO, optional=False) self.sender = config.get_option(CFG_KEY, CFG_SENDER, optional=False) self.messages = list() self.mail = Mail( host, port=port, username=user, password=password, use_tls=tls, use_ssl=ssl, debug_level=None ) def log(self, message): self.messages.append(message) def close(self): body = "\n".join(self.messages) self.mail.send_message( SUBJECT, fromaddr=self.sender, to=self.to, body=body )
def send_email(to_email, subject, body): if 'SMTP_LOGIN' in os.environ and 'SMTP_PASSWORD' in os.environ: mail = Mail("fiq.de", port=25, username=os.environ['SMTP_LOGIN'], password=os.environ['SMTP_PASSWORD'], use_tls=False, use_ssl=False, debug_level=None) mail.send_message(subject, fromaddr="*****@*****.**", to=to_email, body=body) else: print('Cannot send email. SMTP_LOGIN and SMTP_PASSWORD not set.')
# Grab the list of link of pictures for a in doc.find_class('filename-link'): last_pictures.add(a.attrib['href']) # Build a set of mot seen pictures. The last pictures list - the saved pictures list pictures_not_seen = last_pictures - pictures_seen if len(pictures_not_seen) == 0: print("No new pictures. Exiting") else: # Send an email containing the URL of the new pictures mail = Mail(config['smtp']['host'], port = int(config['smtp']['port']), username = config['smtp']['username'], password = config['smtp']['password'], use_tls = config['smtp']['use_tls'], use_ssl = config['smtp']['use_ssl']) body = "<p>A new picture has been posted !</p>" for picture in pictures_not_seen: body += "- <a href='%s'>%s</a><br>" % (picture, picture) mail.send_message("New pictures of %s" % dp['name'], fromaddr=dp['from'], bcc=dp['to'], html=body) # Save the last pictures seen to the backup file with open(backup_file, 'w') as f: f.write(json.dumps(list(last_pictures), indent=4))
def send_mail(smtp_user, smtp_pass, subject='test', body='test', sendto=sendto, smtp_server='smtp.qq.com', smtp_port=465): mail = Mail(smtp_server, port=smtp_port, username=smtp_user, password=smtp_pass, use_tls=False, use_ssl=True, debug_level=None) mail.send_message(subject, fromaddr=smtp_user, to=sendto, body=body)