def tms_cron_messages(): ret = mongo.db.messages_cron.find_one({"cron_status":False,"message_detail.message_origin":"TMS"}) if ret is not None: vet = mongo.db.messages_cron.update({"_id":ObjectId(ret['_id'])}, { "$set": { "cron_status": True } }) if ret['type'] == "email": send_email(message=ret['message'],recipients=ret['recipients'],subject=ret['subject']) elif ret['type'] == "slack": slack_message(message=ret['message'],channel=ret['channel'],req_json=ret['req_json'],message_detail=ret['message_detail']) else: pass else: pass
def reject_mail(): ret = mongo.db.rejection_handling.find_one({"send_status":False}) if ret is not None: mail = ret['email'] message = ret['message'] time = ret['rejection_time'] time_update = dateutil.parser.parse(time).strftime("%Y-%m-%dT%H:%M:%SZ") rejected_time = datetime.datetime.strptime(time_update,'%Y-%m-%dT%H:%M:%SZ') diffrence = datetime.datetime.utcnow() - rejected_time if diffrence.days >= 1: to = [] to.append(mail) send_email(message=message,recipients=to,subject='REJECTED') user_status = mongo.db.rejection_handling.remove({"_id":ObjectId(ret['_id'])}) else: pass else: pass
def mail_test(): email = None if app.config['ENV']=='development': email = app.config['to'] else: email = request.json.get('email') try: send_email(message="SMTP WORKING!",recipients=[email],subject="SMTP TESTING MAIL!") return jsonify({"status":True,"message": "Smtp working"}), 200 except smtplib.SMTPServerDisconnected: return jsonify({"status":False,"message": "Smtp server is disconnected"}), 400 except smtplib.SMTPConnectError: return jsonify({"status":False,"message": "Smtp is unable to established"}), 400 except smtplib.SMTPAuthenticationError: return jsonify({"status":False,"message": "Smtp login and password is wrong"}), 400 except smtplib.SMTPDataError: return jsonify({"status":False,"message": "Smtp account is not activated"}), 400 except Exception: return jsonify({"status":False,"message": "Something went wrong with smtp"}), 400
def mails(): if not request.json: abort(500) MAIL_SEND_TO = None if app.config['ENV'] == 'development': MAIL_SEND_TO = [app.config['to']] else: if app.config['ENV'] == 'production': MAIL_SEND_TO = request.json.get("to",None) message = request.json.get("message",None) subject = request.json.get("subject",None) filename = request.json.get("filename",None) filelink = request.json.get("filelink",None) #is_reminder = request.json.get("is_reminder",True) if not MAIL_SEND_TO and message: return jsonify({"MSG": "Invalid Request"}), 400 bcc = None if 'bcc' in request.json: bcc = request.json['bcc'] cc = None if 'cc' in request.json: cc = request.json['cc'] if 'fcm_registration_id' in request.json: Push_notification(message=message,subject=subject,fcm_registration_id=request.json['fcm_registration_id']) if MAIL_SEND_TO is not None: for mail_store in MAIL_SEND_TO: id = mongo.db.recruit_mail.update({"message":message,"subject":subject,"to":mail_store},{ "$set":{ "message": message, "subject": subject, "to":mail_store, "is_reminder":True }},upsert=True) send_email(message=message,recipients=MAIL_SEND_TO,subject=subject,bcc=bcc,cc=cc,filelink=filelink,filename=filename) return jsonify({"status":True,"Message":"Sended"}),200 else: return jsonify({"status":False,"Message":"Please select a mail"}),400
def send_mails(): if not request.json: abort(500) for elem in dates_converter: if elem in request.json['data']: if request.json['data'][elem] is not None: if request.json['data'][elem] != "": if request.json['data'][elem] != "No Access": date_formatted = dateutil.parser.parse(request.json['data'][elem]).strftime("%d %b %Y") request.json['data'][elem] = date_formatted MSG_KEY = request.json.get("message_key", None) Data = request.json.get("data",None) Message = request.json.get("message",None) Subject = request.json.get("subject",None) message_detail = mongo.db.mail_template.find_one({"message_key": MSG_KEY}) if message_detail is not None: if Message is not None: message_detail['message'] = Message else: pass if Subject is not None: if Subject != "": message_detail['message_subject'] = Subject else: pass attachment_file = None attachment_file_name = None if 'attachment' in request.json: if 'attachment_file' in message_detail: attachment_file = message_detail['attachment_file'] if 'attachment_file_name' in message_detail: attachment_file_name = message_detail['attachment_file_name'] else: pass files = None if 'attachment_files' in message_detail: if message_detail['attachment_files']: files = message_detail['attachment_files'] header = None footer = None if 'template_head' in message_detail: var = mongo.db.letter_heads.find_one({"_id":ObjectId(message_detail['template_head'])}) if var is not None: header = var['header_value'] footer = var['footer_value'] system_variable = mongo.db.mail_variables.find({}) system_variable = [serialize_doc(doc) for doc in system_variable] missing_payload = [] message_variables = [] message = message_detail['message'].split('#') del message[0] rex = re.compile('!|@|\$|\%|\^|\&|\*|\:') for elem in message: varb = re.split(rex, elem) message_variables.append(varb[0]) message_str = message_detail['message'] for detail in message_variables: if detail in request.json['data']: if request.json['data'][detail] is not None: rexWithString = '#' + re.escape(detail) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:])' message_str = re.sub(rexWithString, str(request.json['data'][detail]), message_str) else: for element in system_variable: if "#" + detail == element['name'] and element['value'] is not None: rexWithSystem = re.escape(element['name']) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:])' message_str = re.sub(rexWithSystem, str(element['value']), message_str) missing = message_str.split('#') del missing[0] missing_rex = re.compile('!|@|\$|\%|\^|\&|\*|\:') for elem in missing: missing_data = re.split(missing_rex, elem) missing_payload.append({"key": missing_data[0] , "type": "date" if missing_data[0] in dates_converter else "text"}) subject_variables = [] message_sub = message_detail['message_subject'].split('#') del message_sub[0] regex = re.compile('!|@|\$|\%|\^|\&|\*|\:') for elem in message_sub: sub_varb = re.split(regex, elem) subject_variables.append(sub_varb[0]) message_subject = message_detail['message_subject'] for detail in subject_variables: if detail in request.json['data']: if request.json['data'][detail] is not None: rexWithString = '#' + re.escape(detail) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:])' message_subject = re.sub(rexWithString, str(request.json['data'][detail]), message_subject) else: for element in system_variable: if "#" + detail == element['name'] and element['value'] is not None: rexWithSystem = re.escape(element['name']) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:])' message_subject = re.sub(rexWithSystem, str(element['value']), message_subject) missing_subject = message_subject.split("#") del missing_subject[0] missing_sub_rex = re.compile('!|@|\$|\%|\^|\&|\*|\:') for elem in missing_subject: sub_varb_missing = re.split(missing_sub_rex, elem) missing_payload.append({"key": sub_varb_missing[0] , "type": "date" if sub_varb_missing[0] in dates_converter else "text"}) if 'fromDate' in request.json['data'] and request.json['data']['fromDate'] is not None: if 'toDate' in request.json['data'] and request.json['data']['toDate'] is not None: if request.json['data']['fromDate'] == request.json['data']['toDate']: message_str = message_str.replace(request.json['data']['fromDate'] + " to " + request.json['data']['toDate'],request.json['data']['fromDate']) if 'fromDate' in request.json['data'] and request.json['data']['fromDate'] is not None: if 'toDate' in request.json['data'] and request.json['data']['toDate'] is not None: if request.json['data']['fromDate'] == request.json['data']['toDate']: message_subject = message_subject.replace(request.json['data']['fromDate'] + " to " + request.json['data']['toDate'],request.json['data']['fromDate']) download_pdf = "#letter_head #content #letter_foot" if header is not None: download_pdf = download_pdf.replace("#letter_head",header) else: download_pdf = download_pdf.replace("#letter_head",'') download_pdf = download_pdf.replace("#content",message_str) if footer is not None: download_pdf = download_pdf.replace("#letter_foot",footer) else: download_pdf = download_pdf.replace("#letter_foot",'') if message_detail['message_key'] == "Payslip": system_settings = mongo.db.system_settings.find_one({},{"_id":0}) if system_settings is not None: if system_settings['pdf'] is True: filename = "{}.pdf".format(str(uuid.uuid4())) pdfkit = HTML(string=message_str).write_pdf(os.getcwd() + '/attached_documents/' + filename,stylesheets=[CSS(string='@page {size:Letter; margin: 0in 0in 0in 0in;}')]) attachment_file_name = filename attachment_file = os.getcwd() + '/attached_documents/' + filename else: pass to = None bcc = None cc = None if app.config['ENV'] == 'development': if 'to' in request.json: to = [app.config['to']] bcc = [app.config['bcc']] cc = [app.config['cc']] else: pass else: if app.config['ENV'] == 'production': if 'to' in request.json: if not request.json['to']: to = None else: to = request.json['to'] else: to = None if 'bcc' in request.json: if not request.json['bcc']: bcc = None else: bcc = request.json['bcc'] else: bcc = None if 'cc' in request.json: if not request.json['cc']: cc = None else: cc = request.json['cc'] else: cc = None if message_detail['message_key'] == "interviewee_reject": reject_mail = None if app.config['ENV'] == 'production': if 'email' in request.json['data']: reject_mail = request.json['data']['email'] else: return jsonify({"status": False,"Message": "No rejection mail is sended"}), 400 else: if app.config['ENV'] == 'development': reject_mail = [app.config['to']] reject_handling = mongo.db.rejection_handling.insert_one({ "email": request.json['data']['email'], 'rejection_time': request.json['data']['rejection_time'], 'send_status': False, 'message': message_str, 'subject': message_subject }).inserted_id return jsonify({"status":True,"*Note":"Added for Rejection"}),200 else: if to is not None: send_email(message=message_str,recipients=to,subject=message_subject,bcc=bcc,cc=cc,filelink=attachment_file,filename=attachment_file_name,files=files) return jsonify({"status":True,"Subject":message_subject,"Message":download_pdf,"attachment_file_name":attachment_file_name,"attachment_file":attachment_file,"missing_payload":missing_payload}),200 else: return jsonify({"status":True,"*Note":"No mail will be sended!","Subject":message_subject,"Message":download_pdf,"attachment_file_name":attachment_file_name,"attachment_file":attachment_file,"missing_payload":missing_payload}),200
def campaign_mail(): ret = mongo.db.campaign_users.find_one({"send_status":False}) if ret is not None: if app.config['ENVIRONMENT'] == "development": mail = "*****@*****.**" else: mail = ret['email'] cam = mongo.db.campaigns.find_one({"_id":ObjectId(ret['campaign'])}) if cam is not None: if 'Template' in cam: for data in cam['Template']: system_variable = mongo.db.mail_variables.find({}) system_variable = [serialize_doc(doc) for doc in system_variable] temp = mongo.db.mail_template.find_one({"_id":ObjectId(data)}) subject = temp['message_subject'] message_variables = [] message = temp['message'].split('#') del message[0] rex = re.compile('!|@|\$|\%|\^|\&|\*|\:|\;') for elem in message: varb = re.split(rex, elem) message_variables.append(varb[0]) message_str = temp['message'] for detail in message_variables: if detail in ret: rexWithString = '#' + re.escape(detail) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:]|[\;])' message_str = re.sub(rexWithString, ret[detail], message_str) else: for element in system_variable: if "#" + detail == element['name'] and element['value'] is not None: rexWithSystem = re.escape(element['name']) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:]|[\;])' message_str = re.sub(rexWithSystem, element['value'], message_str) subject_variables = [] message_sub = subject.split('#') del message_sub[0] regex = re.compile('!|@|\$|\%|\^|\&|\*|\:|\;') for elem in message_sub: sub_varb = re.split(regex, elem) subject_variables.append(sub_varb[0]) message_subject = subject for detail in subject_variables: if detail in ret: rexWithString = '#' + re.escape(detail) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:]|[\;])' message_subject = re.sub(rexWithString, ret[detail], message_subject) else: for element in system_variable: if "#" + detail == element['name'] and element['value'] is not None: rexWithSystem = re.escape(element['name']) + r'([!]|[@]|[\$]|[\%]|[\^]|[\&]|[\*]|[\:]|[\;])' message_subject = re.sub(rexWithSystem, element['value'], message_subject) to = [] to.append(mail) working_status = True try: send_email(message=message_str,recipients=to,subject=subject) except Exception: working_status = False mail_data = mongo.db.mail_status.insert_one({ "user_mail": ret['email'], "user_id": str(ret['_id']), "sending_time": datetime.datetime.now(), "message": message_str, "mail_sended_status": working_status, "subject":subject, "recipients": to }) campaign = mongo.db.campaigns.update({"_id":ObjectId(ret['campaign'])}, { "$set": { "cron_status": True } }) user_status = mongo.db.campaign_users.update({"_id":ObjectId(ret['_id'])}, { "$set": { "send_status": True } }) else: pass else: pass else: pass
def mail_setings(origin, id=None): if request.method == "GET": mail = mongo.db.mail_settings.find({"origin": origin}, {"mail_password": 0}) mail = [serialize_doc(doc) for doc in mail] return jsonify(mail) if request.method == "DELETE": mail = mongo.db.mail_settings.remove({ "origin": origin, "_id": ObjectId(str(id)) }) return jsonify({"message": "Smtp conf deleted"}), 200 if request.method == "PUT": ret = mongo.db.mail_settings.update({ "origin": origin, "active": True }, {"$set": { "active": False }}, multi=True) mail = mongo.db.mail_settings.update( { "origin": origin, "_id": ObjectId(str(id)) }, {"$set": { "active": True }}) return jsonify({"message": "Smtp conf set as active"}), 200 if request.method == "POST": if not request.json: abort(500) mail_server = request.json.get("mail_server", "smtp.gmail.com") mail_port = request.json.get("mail_port", 465) mail_use_tls = request.json.get("mail_use_tls", True) mail_username = request.json.get("mail_username", None) mail_password = request.json.get("mail_password", None) mail_from = request.json.get("mail_from", None) active = request.json.get("active", True) type_s = request.json.get("type", "tls") if not mail_server and mail_password and mail_port and mail_use_tls and mail_username: return jsonify({"message": "Invalid Request"}), 400 email = None if mail_from is None: email = mail_username else: email = mail_from try: send_email(message="SMTP WORKING!", recipients=[email], subject="SMTP TESTING MAIL!", sending_mail=mail_username, sending_password=mail_password, sending_port=mail_port, sending_server=mail_server) except smtplib.SMTPServerDisconnected: return jsonify({"message": "Smtp server is disconnected"}), 400 except smtplib.SMTPConnectError: return jsonify({"message": "Smtp is unable to established"}), 400 except smtplib.SMTPAuthenticationError: return jsonify({"message": "Smtp login and password is wrong"}), 400 except smtplib.SMTPDataError: return jsonify({"message": "Smtp account is not activated"}), 400 except Exception as e: print(repr(e), "EXCEPTION") return jsonify({"message": repr(e)}), 400 else: if origin == "HR": ret = mongo.db.mail_settings.update({}, { "$set": { "mail_server": mail_server, "mail_port": mail_port, "origin": origin, "mail_use_tls": mail_use_tls, "mail_username": mail_username, "mail_password": mail_password, "mail_from": mail_from } }, upsert=True) return jsonify({"message": "upsert"}), 200 elif origin == "RECRUIT": vet = mongo.db.mail_settings.find_one({ "mail_username": mail_username, "mail_password": mail_password, "origin": origin }) if vet is None: exist = mongo.db.mail_settings.find_one({"origin": origin}) if exist is None: active = True else: active = False ret = mongo.db.mail_settings.insert_one({ "mail_server": mail_server, "mail_port": mail_port, "origin": origin, "mail_use_tls": mail_use_tls, "mail_username": mail_username, "mail_password": mail_password, "active": active, "type": type_s, "mail_from": mail_from }) return jsonify({"message": "upsert"}), 200 else: return jsonify({"message": "Smtp already exists"}), 400