198 lines
7.8 KiB
Python
Executable File
198 lines
7.8 KiB
Python
Executable File
from django.core.mail import EmailMultiAlternatives
|
|
from django.utils.html import strip_tags
|
|
from django.template.loader import render_to_string
|
|
from django.conf import settings
|
|
from MNF.settings import BasePath
|
|
from django.core.mail import send_mail , get_connection
|
|
from payment.models import privilegedUser1
|
|
from django.contrib.auth.models import User
|
|
from viewerLounge.models import VideoIntroduction, LifeMemberForm
|
|
from lpp.models import MNFLPPDDatabase
|
|
from harkat.models import HarkatClubDatabase
|
|
from relationshipmanager.models import RMDatabase
|
|
from mnfapp.models import centralDatabase
|
|
import os
|
|
from threading import Thread
|
|
import pandas as pd
|
|
|
|
|
|
def poster(user):
|
|
try:
|
|
if not centralDatabase.objects.get(user_id=user).isWhatsappJoined:
|
|
return '1'
|
|
elif not centralDatabase.objects.get(user_id=user).isNewsLetterSubscribed:
|
|
return '2'
|
|
elif not VideoIntroduction.objects.filter(user=user).exists():
|
|
return '3'
|
|
elif not MNFLPPDDatabase.objects.filter(user_id=user).exists():
|
|
return '4'
|
|
elif not HarkatClubDatabase.objects.filter(user_id=user).exists():
|
|
return '5'
|
|
elif not privilegedUser1.objects.filter(user=user).exists():
|
|
return '6'
|
|
elif not RMDatabase.objects.filter(user_id=user).exists():
|
|
return '7'
|
|
elif not centralDatabase.objects.get(user_id=user).isTelegramJoined:
|
|
return '8'
|
|
elif not LifeMemberForm.objects.get(user=user).exists():
|
|
return '9'
|
|
elif LifeMemberForm.objects.filter(user=user, showreel__isnull=True).exists():
|
|
return '10'
|
|
else:
|
|
return '11'
|
|
except Exception as e:
|
|
print("manoj34541")
|
|
print(e)
|
|
return '11'
|
|
|
|
|
|
def mnfnsendemail(whomtosend, titleofmail, email_template, context, from_email=settings.EMAIL_HOST_USER):
|
|
# old style of sending multiple emails by manoj.
|
|
|
|
# subject = str(titleofmail)
|
|
# # from_email = settings.EMAIL_HOST_USER
|
|
# to = whomtosend
|
|
# basepath = BasePath()
|
|
|
|
# html_content = render_to_string(
|
|
# rf"{basepath}/{email_template}",
|
|
|
|
# context,
|
|
# ) # render with dynamic value
|
|
# # Strip the html tag. So people can see the pure text at least.
|
|
# text_content = strip_tags(html_content)
|
|
# # create the email, and attach the HTML version as well.
|
|
# msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
|
|
# msg.attach_alternative(html_content, "text/html")
|
|
# msg.send()
|
|
|
|
# new mail logic by manoj
|
|
print("manoj28")
|
|
basepath = BasePath()
|
|
msg_html = render_to_string(rf"{basepath}/{email_template}", context)
|
|
if type(whomtosend) == list or type(whomtosend) == tuple:
|
|
pass
|
|
else:
|
|
whomtosend = [whomtosend]
|
|
send_mail(
|
|
str(titleofmail),
|
|
'MNF',
|
|
from_email,
|
|
whomtosend,
|
|
html_message=msg_html,
|
|
fail_silently=False,
|
|
)
|
|
return True
|
|
|
|
|
|
# Himanshu's auto email code
|
|
# def send mail(params):
|
|
# mailing -----
|
|
|
|
# run_mail(params):
|
|
# thread(send mail, args (params,))
|
|
|
|
def sendemail(to_email,email_code): #now to email is taken as a list of emails
|
|
# t = Thread(target=mailer,args=(to_email,email_code))
|
|
# t.start()
|
|
pass
|
|
|
|
'''
|
|
class EmailThread(Thread):
|
|
def __init__(self, sender_email, password, host, port, subject, title, message, email_to):
|
|
self.sender_email = sender_email
|
|
self.password = password
|
|
self.port = port
|
|
self.host = host
|
|
self.subject = subject
|
|
self.title = title
|
|
self.message = message
|
|
self.email_to = email_to
|
|
Thread.__init__(self)
|
|
|
|
def run(self):
|
|
|
|
msg_html = render_to_string('email_templete.html', self.message)
|
|
connection = get_connection(host=self.host, port=self.port,
|
|
username=self.sender_email, password=self.password, use_tls=True)
|
|
send_mail(self.subject, self.title, from_email=self.sender_email, recipient_list=[
|
|
self.email_to], fail_silently=False, html_message=msg_html, connection=connection)
|
|
|
|
|
|
def mailer(all_email=None, email_code=None):
|
|
# to_email = ['emails','emails']
|
|
|
|
# implement all mails code in one thread
|
|
# start Thread
|
|
# dont join
|
|
|
|
print("Mail Sending")
|
|
try:
|
|
for to_email in all_email:
|
|
# Using excel file to check which sheets need to fetch with email code.
|
|
sheet_code_excel = '/home/user/mnf/project/MNF/MNF/email/sheetCodeExcel.xlsx'
|
|
fileexcel = pd.read_excel(sheet_code_excel)
|
|
s1 = slice(0, 2)
|
|
string = email_code[s1]
|
|
value = fileexcel[fileexcel['sheet code'] == string]
|
|
if len(value) <= 0:
|
|
return ('Email code does not match with any sheet ')
|
|
sheet_name = value.iloc[0, 1]
|
|
# targeting the excel file with name for details to fetch
|
|
file_path = "/home/user/mnf/project/MNF/MNF/email/EMAIL Sheet.xlsx"
|
|
file = pd.read_excel(file_path, sheet_name=sheet_name, header=1)
|
|
row_data = file[file['Email Code'] == str(email_code)]
|
|
if len(row_data) <= 0:
|
|
return 'Email code is not present in any sheet'
|
|
# removing all nan or None fields with blank string
|
|
row_data = row_data.fillna('')
|
|
from_email = row_data.iloc[0, 3]
|
|
additional_option_code = row_data.iloc[0, 7]
|
|
# targeting option value with options sheet with code
|
|
option_file = pd.read_excel(
|
|
file_path, sheet_name='Common Options', header=1)
|
|
option_details = option_file[option_file['CODE'] == str(
|
|
additional_option_code)]
|
|
if len(option_details) <= 0:
|
|
return 'options code does not match with sheet options code'
|
|
# targeting poster sheet with priority code for poster url
|
|
poster_file = pd.read_excel(
|
|
file_path, sheet_name='poster', header=1)
|
|
postercode = poster(user=to_email)
|
|
if postercode == 0:
|
|
return 'user has not any priority to send poster'
|
|
poster_url = poster_file[poster_file['Poster code'] == str(
|
|
postercode)]
|
|
posterurl = poster_url.iloc[0, 2]
|
|
# poster_details = poster_file[poster_file['Poster code']==str(poster_code)]
|
|
# split title with /
|
|
title = row_data.iloc[0, 5].split('/')
|
|
# Using dict to store all value which html template needs to send details in email
|
|
message = {'subject': row_data.iloc[0, 4],
|
|
'title1': title[0],
|
|
'title2': title[1],
|
|
'invitation': row_data.iloc[0, 4],
|
|
'additional': option_details.iloc[0, 2],
|
|
'poster': posterurl,
|
|
}
|
|
print('done')
|
|
# passing details in html template
|
|
msg_html = render_to_string('email_templete.html', message)
|
|
# getting details of email which should use as sender email id
|
|
try:
|
|
excel_file = "/home/user/mnf/project/MNF/MNF/email/Email Credentials.xlsx"
|
|
details = pd.read_excel(excel_file)
|
|
email_data = details[details['email'] == str(from_email)]
|
|
host = email_data.iloc[0, 1]
|
|
port = int(email_data.iloc[0, 2])
|
|
# password = emails_creadentials.objects.get(email=from_email) #to get password of sender email is getting from emails_creadentials
|
|
except Exception as e:
|
|
return str(e)
|
|
EmailThread(sender_email=from_email, password=password.password, port=port, host=host,
|
|
subject=message['subject'], title=title[1], message=message, email_to=to_email).start()
|
|
print('done')
|
|
except Exception as e:
|
|
return str(e)
|
|
return 'Email sent successfull'
|
|
'''
|