diff --git a/mail_config.py b/mail_config.py new file mode 100644 index 0000000000000000000000000000000000000000..1146e3848f5e3a0bdcc3fc24a6fdcb711d475b0a --- /dev/null +++ b/mail_config.py @@ -0,0 +1,30 @@ +# Some variable for email +Server = 'localhost' +My_email = 'root@localhost' +Sender = 'ROOT@LOCALHOST' +Sender_alias = 'Services' +Subject = 'New User Account' +Info_url = 'https://www.google.com' + +Head = f"""From: {Sender_alias} <{Sender}> +To: <{{{{ to }}}}> +Subject: {Subject} +""" + +Body = f""" +Hi {{{{ username }}}} +Your account has been set up with: + +============================ +User ID: {{{{ username }}}} +============================ + +If you have any questions, please visit: +{Info_url} + +or email at {My_email} + +Cheers, +""" + +Whole_mail = Head + Body diff --git a/notify_user.py b/notify_user.py new file mode 100644 index 0000000000000000000000000000000000000000..753d14d790c4c1d5fea98ed3709dfaa1739f5cff --- /dev/null +++ b/notify_user.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +import sys +import json +import rc_util +import smtplib +import dataset +from rc_rmq import RCRMQ +from jinja2 import Template +from datetime import datetime +import mail_config as mail_cfg + +task = 'notify_user' + +args = rc_util.get_args() +logger = rc_util.get_logger(args) + +db = dataset.connect(f'sqlite:///.agent_db/{task}.db') +table = db['notified_user'] + +# Instantiate rabbitmq object +rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'}) + +# Email instruction to user +def notify_user(ch, method, properties, body): + msg = json.loads(body) + username = msg['username'] + user_email = msg['email'] + msg['task'] = task + msg['success'] = False + + try: + + # Search username in database + record = table.find_one(username=username) + + if record: + # Update counter + count = record['count'] + table.update({'username': username, 'count': count + 1}, ['username']) + + logger.debug(f'User {username} counter updated to {count + 1}') + + else: + # Send email to user + receivers = [user_email, mail_cfg.My_email] + message = Template(mail_cfg.Whole_mail).render(username=username, to=user_email) + + if args.dry_run: + logger.info(f'smtp = smtplib.SMTP({mail_cfg.Server})') + logger.info(f'smtp.sendmail({mail_cfg.Sender}, {receivers}, message)') + logger.info(f"table.insert({{'username': {username}, 'count': 1, 'sent_at': datetime.now()}})") + + else: + smtp = smtplib.SMTP(mail_cfg.Server) + smtp.sendmail(mail_cfg.Sender, receivers, message) + + logger.debug(f'Email sent to: {user_email}') + + table.insert({ + 'username': username, + 'count': 1, + 'sent_at': datetime.now() + }) + + logger.debug(f'User {username} inserted into database') + + msg['success'] = True + except Exception as exception: + logger.error('', exc_info=True) + + # Send confirm message + rc_rmq.publish_msg({ + 'routing_key': 'confirm.' + username, + 'msg': msg + }) + + logger.debug(f'User {username} confirmation sent') + + # Acknowledge the message + ch.basic_ack(delivery_tag=method.delivery_tag) + + +if __name__ == "__main__": + logger.info(f'Start listening to queue: {task}') + rc_rmq.start_consume({ + 'queue': task, + 'routing_key': "notify.*", + 'cb': notify_user + }) + + logger.info('Disconnected') + rc_rmq.disconnect() diff --git a/requirements.txt b/requirements.txt index 6d35ddfcd9d728e552c87e4efc45f130639a7070..5a9a79de947b027e538fe018037e8b44d6567eba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ pika==1.1.0 -sh==1.12.14 +dataset==1.3.1 +Jinja2==2.11.2 +sh==1.12.14 \ No newline at end of file