Added sendmail library
This commit is contained in:
parent
59bd97be74
commit
e40b618349
6 changed files with 228 additions and 16 deletions
|
|
@ -4,23 +4,32 @@ from importlib import import_module
|
|||
import os
|
||||
import sys
|
||||
import inspect
|
||||
from paramecio2.libraries.datetime import set_timezone
|
||||
|
||||
def start_app():
|
||||
|
||||
set_timezone()
|
||||
|
||||
app=Flask(__name__, static_url_path=config.static_url_path, static_folder=config.static_folder)
|
||||
|
||||
app.secret_key=config.secret_key
|
||||
|
||||
app.config['JSON_SORT_KEYS']=False
|
||||
|
||||
application_root='/'
|
||||
|
||||
if hasattr(config, 'application_root'):
|
||||
application_root=config.application_root
|
||||
|
||||
|
||||
app.config.update(
|
||||
APPLICATION_ROOT=application_root
|
||||
)
|
||||
|
||||
if hasattr(config, 'json_sort_keys'):
|
||||
app.config.update(
|
||||
JSON_SORT_KEYS=config.json_sort_keys
|
||||
)
|
||||
|
||||
workdir=os.getcwd()
|
||||
arr_module_path={}
|
||||
|
||||
|
|
|
|||
|
|
@ -131,6 +131,8 @@ class PhangoField:
|
|||
|
||||
value=str(value).strip()
|
||||
|
||||
#Minimal escape for prevent basic js injection.
|
||||
|
||||
if self.escape==False:
|
||||
value=value.replace('<', '<')
|
||||
|
||||
|
|
|
|||
|
|
@ -277,13 +277,19 @@ class AjaxList(SimpleList):
|
|||
|
||||
str_query=self.str_query+' '+order_sql
|
||||
|
||||
params=self.str_query_params
|
||||
|
||||
html_pages=''
|
||||
|
||||
if self.limit>0:
|
||||
str_query+=' limit %s, %s'
|
||||
|
||||
params=self.str_query_params
|
||||
params.append(begin_page)
|
||||
params.append(limit)
|
||||
|
||||
pages=Pages()
|
||||
|
||||
html_pages=I18n.lang('cuchulu', 'pages', 'Pages')+': '+pages.show( begin_page, total_elements, limit, '#' ,initial_num_pages=self.initial_num_pages, variable='begin_page', label='', func_jscript='')
|
||||
|
||||
with self.db.query(str_query, params) as cursor:
|
||||
for row in cursor:
|
||||
"""
|
||||
|
|
@ -301,11 +307,7 @@ class AjaxList(SimpleList):
|
|||
rows.append(row)
|
||||
#{k:d[k] for in set(d).intersection(l)}
|
||||
|
||||
pages=Pages()
|
||||
|
||||
html_pages=pages.show( begin_page, total_elements, limit, '#' ,initial_num_pages=self.initial_num_pages, variable='begin_page', label='', func_jscript='')
|
||||
|
||||
return {'fields': self.fields, 'rows': rows, 'html_pages': I18n.lang('cuchulu', 'pages', 'Pages')+': '+html_pages}
|
||||
return {'fields': self.fields, 'rows': rows, 'html_pages': html_pages}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
185
paramecio2/libraries/sendmail.py
Normal file
185
paramecio2/libraries/sendmail.py
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import smtplib
|
||||
import mimetypes
|
||||
from email import encoders
|
||||
from email.message import Message
|
||||
from email.mime.audio import MIMEAudio
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.image import MIMEImage
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
import ssl as ssl_module
|
||||
import sys
|
||||
|
||||
class SendMail:
|
||||
|
||||
port=587
|
||||
|
||||
host='localhost'
|
||||
|
||||
username=''
|
||||
|
||||
password=''
|
||||
|
||||
ssl=True
|
||||
|
||||
if sys.version_info < (3, 6):
|
||||
|
||||
context = ssl_module.SSLContext(ssl_module.PROTOCOL_TLSv1_2)
|
||||
else:
|
||||
context = ssl_module.SSLContext(ssl_module.PROTOCOL_TLS)
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.smtp=smtplib.SMTP(host=self.host, port=self.port)
|
||||
self.txt_error=''
|
||||
|
||||
def send(self, from_address, to_address: list, subject, message, content_type='plain', attachments=[]):
|
||||
|
||||
if self.ssl==True:
|
||||
|
||||
try:
|
||||
|
||||
self.smtp.starttls(context=self.context)
|
||||
|
||||
except smtplib.SMTPHeloError:
|
||||
|
||||
self.txt_error='Error: cannot make HELO to this server'
|
||||
|
||||
return False
|
||||
|
||||
except RuntimeError:
|
||||
|
||||
self.txt_error='Error: SSL/TLS is not supported in your python interpreter'
|
||||
|
||||
return False
|
||||
|
||||
except smtplib.SMTPException as e:
|
||||
|
||||
self.txt_error=e.__str__()
|
||||
|
||||
return False
|
||||
|
||||
"""
|
||||
except smtplib.SMTPNotSupportedError:
|
||||
|
||||
self.txt_error='Error: SSL/TLS is not supported'
|
||||
|
||||
return False
|
||||
"""
|
||||
|
||||
if self.username!='':
|
||||
|
||||
try:
|
||||
|
||||
self.smtp.login(self.username, self.password)
|
||||
|
||||
except smtplib.SMTPHeloError:
|
||||
|
||||
self.txt_error='Error: cannot make HELO to this server'
|
||||
|
||||
return False
|
||||
|
||||
except smtplib.SMTPAuthenticationError:
|
||||
|
||||
self.txt_error='Error: cannot login. Wrong username or password'
|
||||
|
||||
return False
|
||||
|
||||
except smtplib.SMTPException as e:
|
||||
# self.txt_error=e.__str__()
|
||||
self.txt_error='Error: any method for login is avaliable - '+e.__str__()
|
||||
|
||||
return False
|
||||
|
||||
"""
|
||||
except smtplib.SMTPNotSupportedError:
|
||||
|
||||
self.txt_error='Error: AUTH is not supported'
|
||||
|
||||
return False
|
||||
"""
|
||||
|
||||
COMMASPACE=', '
|
||||
|
||||
if len(attachments)==0:
|
||||
|
||||
msg=MIMEText(message, content_type)
|
||||
|
||||
msg['Subject']=subject
|
||||
msg['From']=from_address
|
||||
|
||||
msg['To']=COMMASPACE.join(to_address)
|
||||
|
||||
self.smtp.send_message(msg)
|
||||
|
||||
#self.quit()
|
||||
|
||||
return True
|
||||
|
||||
else:
|
||||
|
||||
outer=MIMEMultipart()
|
||||
|
||||
outer['Subject']=subject
|
||||
outer['From']=from_address
|
||||
|
||||
outer['To']=COMMASPACE.join(to_address)
|
||||
|
||||
# Attach message text
|
||||
|
||||
msg=MIMEText(message, content_type)
|
||||
|
||||
outer.attach(msg)
|
||||
|
||||
for path in attachments:
|
||||
|
||||
ctype, encoding = mimetypes.guess_type(path)
|
||||
|
||||
if ctype is None or encoding is not None:
|
||||
# No guess could be made, or the file is encoded (compressed), so
|
||||
# use a generic bag-of-bits type.
|
||||
ctype = 'application/octet-stream'
|
||||
|
||||
maintype, subtype = ctype.split('/', 1)
|
||||
|
||||
if maintype == 'text':
|
||||
with open(path) as fp:
|
||||
# Note: we should handle calculating the charset
|
||||
msg = MIMEText(fp.read(), _subtype=subtype)
|
||||
|
||||
elif maintype == 'image':
|
||||
with open(path, 'rb') as fp:
|
||||
msg = MIMEImage(fp.read(), _subtype=subtype)
|
||||
|
||||
elif maintype == 'audio':
|
||||
with open(path, 'rb') as fp:
|
||||
msg = MIMEAudio(fp.read(), _subtype=subtype)
|
||||
|
||||
else:
|
||||
with open(path, 'rb') as fp:
|
||||
msg = MIMEBase(maintype, subtype)
|
||||
msg.set_payload(fp.read())
|
||||
# Encode the payload using Base64
|
||||
encoders.encode_base64(msg)
|
||||
|
||||
# Set the filename parameter
|
||||
msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
|
||||
|
||||
outer.attach(msg)
|
||||
|
||||
self.smtp.send_message(outer)
|
||||
|
||||
#self.quit()
|
||||
|
||||
return True
|
||||
|
||||
def quit(self):
|
||||
|
||||
self.smtp.quit()
|
||||
|
||||
def __del__(self):
|
||||
|
||||
self.smtp.quit()
|
||||
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
try:
|
||||
from flask import Blueprint
|
||||
from paramecio2.libraries.mtemplates import PTemplate, env_theme
|
||||
|
||||
admin_app=Blueprint('admin_app', __name__, static_folder='static')
|
||||
|
||||
|
||||
env=env_theme(__file__)
|
||||
|
||||
t=PTemplate(env)
|
||||
|
||||
except:
|
||||
pass
|
||||
|
|
|
|||
9
paramecio2/modules/admin/templates/users.phtml
Normal file
9
paramecio2/modules/admin/templates/users.phtml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<%inherit file="dashboard.phtml"/>
|
||||
<%block name="content">
|
||||
<div class="title">
|
||||
Welcome to Paramecio Admin
|
||||
</div>
|
||||
<div class="cont">
|
||||
From here you can admin your site
|
||||
</div>
|
||||
</%block>
|
||||
Loading…
Add table
Add a link
Reference in a new issue