-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,37 @@ | ||
import smtplib | ||
from socket import gaierror | ||
import ssl | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
|
||
|
||
def sendMail(to: str, subject: str, message: str): | ||
def sendMail(to: str, subject: str, message: str, html: bool = False): | ||
import env as e | ||
try: | ||
with smtplib.SMTP(e.mail_host, e.mail_port) as server: | ||
server.login(e.mail_username, e.mail_password) | ||
server.sendmail(from_addr=e.mail_sender, to_addrs=to, msg=message) | ||
except (gaierror, ConnectionRefusedError): | ||
return "failed to connect to the server" | ||
except smtplib.SMTPServerDisconnected: | ||
return "Failed to connect to the server with the given credentials" | ||
except smtplib.SMTPException as e: | ||
return "SMTP error occured: " + str(e) | ||
server.close() | ||
return True | ||
|
||
context = ssl.create_default_context() | ||
|
||
mail = MIMEMultipart("alternative") | ||
mail["Subject"] = subject | ||
mail["From"] = e.mail_sender | ||
mail["To"] = to | ||
|
||
if html: | ||
msg = MIMEText(message, "html") | ||
else: | ||
msg = MIMEText(message, "plain") | ||
|
||
mail.attach(msg) | ||
|
||
with smtplib.SMTP_SSL(e.mail_host, e.mail_port, context=context) as server: | ||
|
||
server.login( | ||
user=e.mail_sender, | ||
password=e.mail_password | ||
) | ||
|
||
server.sendmail( | ||
from_addr=e.mail_sender, | ||
to_addrs=to, | ||
msg=mail.as_string() | ||
) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM python:3.8.1-slim-buster | ||
|
||
ENV WORKDIR=/usr/src/app | ||
ENV USER=app | ||
ENV APP_HOME=/home/app/web | ||
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 | ||
|
||
WORKDIR $WORKDIR | ||
|
||
RUN pip install --upgrade pip | ||
COPY ./requirements.txt $WORKDIR/requirements.txt | ||
RUN pip install -r requirements.txt | ||
|
||
RUN adduser --system --group $USER | ||
RUN mkdir $APP_HOME | ||
WORKDIR $APP_HOME | ||
|
||
WORKDIR $APP_HOME | ||
RUN chown -R $USER:$USER $APP_HOME | ||
USER $USER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: '3.8' | ||
|
||
services: | ||
web: | ||
build: | ||
context: ./services/api | ||
command: gunicorn main:app --bind 0.0.0.0:5000 -k uvicorn.workers.UvicornWorker | ||
expose: | ||
- 5000 | ||
labels: | ||
- "traefik.enable=true" | ||
- "traefik.http.routers.fastapi.rule=Host(`fastapi.localhost`)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pydantic import BaseModel, validator | ||
from typing import Optional | ||
from assets.mail import sendMail | ||
import re | ||
|
||
|
||
class Mail(BaseModel): | ||
to: str | ||
subject: str | ||
message: str | ||
html: Optional[bool] = False | ||
|
||
@validator('to') | ||
def is_valid_email(cls, email): | ||
regex = r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" | ||
if re.search(regex, email): | ||
return email | ||
else: | ||
raise ValueError('EMAIL not Valid') | ||
|
||
def send(self): | ||
return sendMail(to=self.to, subject=self.subject, message=self.message, html=self.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters