initial commit
This commit is contained in:
commit
e15a754b5f
|
@ -0,0 +1,12 @@
|
|||
FROM python:3.9.16-alpine
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
COPY requirements.txt ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 5000
|
||||
|
||||
CMD ["python", "-m", "flask", "run", "--host=0.0.0.0"]
|
|
@ -0,0 +1,28 @@
|
|||
# restful mailer
|
||||
|
||||
a simple and hacky rest api for sending emails
|
||||
|
||||
# running (bare metal)
|
||||
|
||||
```sh
|
||||
pip install -r requirements.txt
|
||||
python -m flask run --host=0.0.0.0
|
||||
```
|
||||
|
||||
# running (docker)
|
||||
|
||||
```sh
|
||||
docker build . -t resting-mailer
|
||||
docker run -p 5000:5000 resting-mailer
|
||||
```
|
||||
|
||||
# sending emails
|
||||
|
||||
assuming run on localhost
|
||||
|
||||
``` sh
|
||||
curl --header "Content-Type: application/json" \
|
||||
http://localhost:5000/send \
|
||||
-d \
|
||||
'{"host": "<host>", "username": "<username>", "password": "<password", "to": "<someone>", "from": "<me>", "subject": "testicle", "content": "balls to the wall"}'
|
||||
```
|
|
@ -0,0 +1,71 @@
|
|||
from flask import Flask, jsonify, request
|
||||
from flask_restful import Resource, Api
|
||||
from flask_restful import reqparse
|
||||
from smtplib import SMTP, SMTPException
|
||||
from email.message import EmailMessage
|
||||
|
||||
app = Flask(__name__)
|
||||
api = Api(app)
|
||||
|
||||
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument("host", type=str)
|
||||
parser.add_argument("port", type=int)
|
||||
parser.add_argument("tls", type=bool)
|
||||
parser.add_argument("username", type=str)
|
||||
parser.add_argument("password", type=str)
|
||||
parser.add_argument("from", type=str)
|
||||
parser.add_argument("to", type=str)
|
||||
parser.add_argument("subject", type=str)
|
||||
parser.add_argument("content", type=str)
|
||||
|
||||
|
||||
def mk_msg(toaddr, fromaddr, subj, content):
|
||||
msg = EmailMessage()
|
||||
msg["To"] = toaddr
|
||||
msg["From"] = fromaddr
|
||||
msg["Subject"] = subj
|
||||
if content is not None:
|
||||
msg.set_content(content)
|
||||
return msg
|
||||
|
||||
|
||||
class SendEmail(Resource):
|
||||
def post(self):
|
||||
args = parser.parse_args(strict=True)
|
||||
|
||||
for key, msg in [
|
||||
("host", "no host provided"),
|
||||
("username", "no username provided"),
|
||||
("to", "no 'to' address"),
|
||||
("from", "no 'from' address"),
|
||||
]:
|
||||
if args[key] is None:
|
||||
return msg, 400
|
||||
|
||||
# just assume we will want TLS (for obvious reasons)
|
||||
port = 587 if args["port"] is None else args["port"]
|
||||
use_tls = args["tls"] is not False
|
||||
|
||||
try:
|
||||
with SMTP(host=args["host"], port=port) as s:
|
||||
if use_tls:
|
||||
s.starttls()
|
||||
s.login(args["username"], args["password"])
|
||||
msg = mk_msg(
|
||||
args["to"],
|
||||
args["from"],
|
||||
args["subject"],
|
||||
args["content"],
|
||||
)
|
||||
s.send_message(msg)
|
||||
return "send successful", 201
|
||||
except SMTPException as e:
|
||||
return str(e), 400
|
||||
|
||||
|
||||
api.add_resource(SendEmail, "/send")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
|
@ -0,0 +1,2 @@
|
|||
flask-restful==0.3.9
|
||||
flask==2.2.2
|
Loading…
Reference in New Issue