fix: Add escape char for curl rendering and format python files
This commit is contained in:
69
mail.py
69
mail.py
@@ -21,95 +21,76 @@ import os
|
||||
# "body":"G'\''day\nThis is a test email from my website api\n\nRegards,\nNathan.Woodburn/"
|
||||
# }'
|
||||
|
||||
|
||||
def validateSender(email):
|
||||
domains = os.getenv("EMAIL_DOMAINS")
|
||||
if not domains:
|
||||
return False
|
||||
|
||||
|
||||
domains = domains.split(",")
|
||||
for domain in domains:
|
||||
if re.match(r".+@" + domain, email):
|
||||
return True
|
||||
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def sendEmail(data):
|
||||
fromEmail = "noreply@woodburn.au"
|
||||
if "from" in data:
|
||||
fromEmail = data["from"]
|
||||
|
||||
if not validateSender(fromEmail):
|
||||
return jsonify({
|
||||
"status": 400,
|
||||
"message": "Bad request 'from' email invalid"
|
||||
})
|
||||
|
||||
return jsonify({"status": 400, "message": "Bad request 'from' email invalid"})
|
||||
|
||||
if "to" not in data:
|
||||
return jsonify({
|
||||
"status": 400,
|
||||
"message": "Bad request 'to' json data missing"
|
||||
})
|
||||
return jsonify({"status": 400, "message": "Bad request 'to' json data missing"})
|
||||
to = data["to"]
|
||||
|
||||
if "subject" not in data:
|
||||
return jsonify({
|
||||
"status": 400,
|
||||
"message": "Bad request 'subject' json data missing"
|
||||
})
|
||||
return jsonify(
|
||||
{"status": 400, "message": "Bad request 'subject' json data missing"}
|
||||
)
|
||||
subject = data["subject"]
|
||||
|
||||
if "body" not in data:
|
||||
return jsonify({
|
||||
"status": 400,
|
||||
"message": "Bad request 'body' json data missing"
|
||||
})
|
||||
return jsonify(
|
||||
{"status": 400, "message": "Bad request 'body' json data missing"}
|
||||
)
|
||||
body = data["body"]
|
||||
|
||||
if not re.match(r"[^@]+@[^@]+\.[^@]+", to):
|
||||
raise ValueError("Invalid recipient email address.")
|
||||
|
||||
|
||||
if not subject:
|
||||
raise ValueError("Subject cannot be empty.")
|
||||
|
||||
|
||||
if not body:
|
||||
raise ValueError("Body cannot be empty.")
|
||||
|
||||
|
||||
fromName = "Nathan Woodburn"
|
||||
if 'sender' in data:
|
||||
fromName = data['sender']
|
||||
if "sender" in data:
|
||||
fromName = data["sender"]
|
||||
|
||||
# Create the email message
|
||||
msg = MIMEMultipart()
|
||||
msg['From'] = formataddr((fromName, fromEmail))
|
||||
msg['To'] = to
|
||||
msg['Subject'] = subject
|
||||
msg.attach(MIMEText(body, 'plain'))
|
||||
|
||||
msg["From"] = formataddr((fromName, fromEmail))
|
||||
msg["To"] = to
|
||||
msg["Subject"] = subject
|
||||
msg.attach(MIMEText(body, "plain"))
|
||||
|
||||
# Sending the email
|
||||
try:
|
||||
host = os.getenv("EMAIL_SMTP")
|
||||
user = os.getenv("EMAIL_USER")
|
||||
password = os.getenv("EMAIL_PASS")
|
||||
if host is None or user is None or password is None:
|
||||
return jsonify({
|
||||
"status": 500,
|
||||
"error": "Email server not configured"
|
||||
})
|
||||
return jsonify({"status": 500, "error": "Email server not configured"})
|
||||
|
||||
with smtplib.SMTP_SSL(host, 465) as server:
|
||||
server.login(user, password)
|
||||
server.sendmail(fromEmail, to, msg.as_string())
|
||||
print("Email sent successfully.")
|
||||
return jsonify({
|
||||
"status": 200,
|
||||
"message": "Send email successfully"
|
||||
})
|
||||
return jsonify({"status": 200, "message": "Send email successfully"})
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
"status": 500,
|
||||
"error": "Sending email failed",
|
||||
"exception":e
|
||||
})
|
||||
|
||||
|
||||
return jsonify({"status": 500, "error": "Sending email failed", "exception": e})
|
||||
|
||||
Reference in New Issue
Block a user