Migrated SMPT server to office365

+ image email fix
This commit is contained in:
Luca Sorace "Stranck 2024-02-11 23:57:52 +01:00
parent 0d663c5b58
commit 165c9d6bb5
7 changed files with 99 additions and 16 deletions

1
.gitignore vendored
View File

@ -167,3 +167,4 @@ furizon_webinit_riverside2023.tar.gz
diomerdas diomerdas
furizon.net/site/* furizon.net/site/*
furizon.net.zip furizon.net.zip
stuff/secrets.py

3
app.py
View File

@ -12,7 +12,7 @@ from io import BytesIO
from asyncio import Queue from asyncio import Queue
from messages import LOCALES from messages import LOCALES
import sqlite3 import sqlite3
from sanic.log import logger from sanic.log import logger, logging
app = Sanic(__name__) app = Sanic(__name__)
app.static("/res", "res/") app.static("/res", "res/")
@ -45,6 +45,7 @@ async def clear_session(request, exception):
@app.before_server_start @app.before_server_start
async def main_start(*_): async def main_start(*_):
logger.info(f"[{app.name}] >>>>>> main_start <<<<<<") logger.info(f"[{app.name}] >>>>>> main_start <<<<<<")
logger.setLevel(LOG_LEVEL)
app.config.REQUEST_MAX_SIZE = PROPIC_MAX_FILE_SIZE * 3 app.config.REQUEST_MAX_SIZE = PROPIC_MAX_FILE_SIZE * 3

View File

@ -1,3 +1,6 @@
from sanic.log import logging
LOG_LEVEL = logging.DEBUG
API_TOKEN = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' API_TOKEN = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
ORGANIZER = 'furizon' ORGANIZER = 'furizon'
EVENT_NAME = 'overlord' EVENT_NAME = 'overlord'
@ -25,6 +28,9 @@ SMTP_HOST = 'host'
SMTP_PORT = 0 SMTP_PORT = 0
SMTP_USER = 'user' SMTP_USER = 'user'
SMTP_PASSWORD = 'pw' SMTP_PASSWORD = 'pw'
EMAIL_SENDER_NAME = "Fantastic Furcon Wow"
EMAIL_SENDER_MAIL = "no-reply@thisIsAFantasticFurconWowItIsWonderful.cuteFurries.ovh"
SMPT_CLIENT_CLOSE_TIMEOUT = 60 * 15 # 15 minutes
FILL_CACHE = True FILL_CACHE = True
CACHE_EXPIRE_TIME = 60 * 60 * 4 CACHE_EXPIRE_TIME = 60 * 60 * 4
@ -111,11 +117,6 @@ CATEGORIES_LIST_MAP = {
'dailys': [] 'dailys': []
} }
SPONSORSHIP_COLOR_MAP = {
'super': (251, 140, 0),
'normal': (142, 36, 170)
}
# Create a bunch of "room" items which will get added to the order once somebody gets a room. # Create a bunch of "room" items which will get added to the order once somebody gets a room.
# Map item_name -> room capacity # Map item_name -> room capacity
ROOM_CAPACITY_MAP = { ROOM_CAPACITY_MAP = {

View File

@ -1,13 +1,60 @@
from sanic import Sanic from sanic import Sanic
from sanic.log import logger
import ssl
from ssl import SSLContext
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from messages import ROOM_ERROR_TYPES from messages import ROOM_ERROR_TYPES
import smtplib import smtplib
from messages import * from messages import *
from config import SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD from config import *
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from threading import Timer, Lock
async def send_unconfirm_message (room_order, orders): def killSmptClient():
global sslLock
global sslTimer
global smptSender
sslTimer.cancel()
sslLock.acquire()
if(smptSender is not None):
logger.debug('[SMPT] Closing smpt client')
smptSender.quit() # it calls close() inside
smptSender = None
sslLock.release()
async def openSmptClient():
global sslLock
global sslTimer
global sslContext
global smptSender
sslTimer.cancel()
sslLock.acquire()
if(smptSender is None):
logger.debug('[SMPT] Opening smpt client')
client : smtplib.SMTP = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
client.starttls(context=sslContext)
client.login(SMTP_USER, SMTP_PASSWORD)
smptSender = client
sslLock.release()
sslTimer = createTimer()
sslTimer.start()
def createTimer():
return Timer(SMPT_CLIENT_CLOSE_TIMEOUT, killSmptClient)
sslLock : Lock = Lock()
sslTimer : Timer = createTimer()
sslContext : SSLContext = ssl.create_default_context()
smptSender : smtplib.SMTP = None
async def sendEmail(message : MIMEMultipart):
await openSmptClient()
logger.debug(f"[SMPT] Sending mail {message['From']} -> {message['to']} '{message['Subject']}'")
sslLock.acquire()
smptSender.sendmail(message['From'], message['to'], message.as_string())
sslLock.release()
async def send_unconfirm_message(room_order, orders):
memberMessages = [] memberMessages = []
issues_plain = "" issues_plain = ""
@ -32,17 +79,15 @@ async def send_unconfirm_message (room_order, orders):
message = MIMEMultipart("alternative") message = MIMEMultipart("alternative")
message.attach(plain_text) message.attach(plain_text)
message.attach(html_text) message.attach(html_text)
message['Subject'] = '[Furizon] Your room cannot be confirmed' message['Subject'] = f'[{EMAIL_SENDER_NAME}] Your room cannot be confirmed'
message['From'] = 'Furizon <no-reply@furizon.net>' message['From'] = f'{EMAIL_SENDER_NAME} <{EMAIL_SENDER_MAIL}>'
message['To'] = f"{member.name} <{member.email}>" message['To'] = f"{member.name} <{member.email}>"
memberMessages.append(message) memberMessages.append(message)
if len(memberMessages) == 0: return if len(memberMessages) == 0: return
with smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT) as sender:
sender.login(SMTP_USER, SMTP_PASSWORD)
for message in memberMessages: for message in memberMessages:
sender.sendmail(message['From'], message['to'], message.as_string()) await sendEmail(message)
def render_email_template(title = "", body = ""): def render_email_template(title = "", body = ""):
tpl = Environment(loader=FileSystemLoader("tpl"), autoescape=False).get_template('email/comunication.html') tpl = Environment(loader=FileSystemLoader("tpl"), autoescape=False).get_template('email/comunication.html')

3
startup.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
python3 app.py 2>&1 | tee -a log.txt

32
stuff/testEmail.py Normal file
View File

@ -0,0 +1,32 @@
import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from secrets import *
SMTP_HOST = 'smtp.office365.com'
SMTP_USER = 'webservice@furizon.net'
SMTP_PORT = 587
plain_body = "Test aaaa"
plain_text = MIMEText(plain_body, "plain")
message = MIMEMultipart("alternative")
message.attach(plain_text)
message['Subject'] = '[Furizon] This is a test!'
message['From'] = 'Furizon <webservice@furizon.net>'
message['To'] = f"Luca Sorace <strdjn@gmail.com>"
print("Start")
context = ssl.create_default_context()
print("Context created")
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as sender:
print("Created sender obj")
sender.starttls(context=context)
print("Tls started")
sender.login(SMTP_USER, SMTP_PASSWORD)
print("Logged in")
print(sender.sendmail(message['From'], message['to'], message.as_string()))
print("Mail sent")

View File

@ -10,13 +10,13 @@
.container { max-width: 40em; padding: 1em; margin: 0 auto; } .container { max-width: 40em; padding: 1em; margin: 0 auto; }
.title { font-size: 1.75em; margin-bottom: 1.2em; color: #e1e6eb; margin-top: 0; font-family: sans-serif; } .title { font-size: 1.75em; margin-bottom: 1.2em; color: #e1e6eb; margin-top: 0; font-family: sans-serif; }
.main-content { margin-top: 0; font-style: normal; font-weight: 400; font-family: sans-serif;} .main-content { margin-top: 0; font-style: normal; font-weight: 400; font-family: sans-serif;}
.con-logo { height:2em;} .con-logo { height:3em;}
.link { text-decoration: none; background-color: #1095c1; color: #fff; padding: 1em; border-radius: 5px; font-weight: 600; } .link { text-decoration: none; background-color: #1095c1; color: #fff; padding: 1em; border-radius: 5px; font-weight: 600; }
</style> </style>
</head> </head>
<div class="body"> <div class="body">
<div class="container"> <div class="container">
<img src="https://reg.furizon.net/res/furizon.png" class="con-logo"> <img src="https://reg.furizon.net/res/furizon.png" alt="con_logo" title="con_logo" class="con-logo">
<div> <div>
<h2 class="title">{{title}}</h2> <h2 class="title">{{title}}</h2>
<p class="main-content">{{body}}</p> <p class="main-content">{{body}}</p>