2022-10-02 15:09:16 -04:00
|
|
|
import traceback
|
2022-09-26 08:24:59 -04:00
|
|
|
import os
|
2022-09-26 17:00:21 -04:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from mailjet_rest import Client
|
2022-09-26 22:47:38 -04:00
|
|
|
from undercover.fallback import mock_sender
|
2022-09-26 08:24:59 -04:00
|
|
|
|
2022-09-26 17:00:21 -04:00
|
|
|
api_key = os.environ.get('MAILJET_API_KEY')
|
|
|
|
api_secret = os.environ.get('MAILJET_SECRET_KEY')
|
2022-09-26 08:24:59 -04:00
|
|
|
|
2022-09-26 17:00:21 -04:00
|
|
|
has_mailjet_keys = api_key and api_secret
|
|
|
|
mailjet = Client(auth=(api_key, api_secret), version='v3.1') if has_mailjet_keys else None
|
|
|
|
|
|
|
|
if not mailjet:
|
|
|
|
sys.stderr.write('Mailjet keys not configured: email access is disabled.\n')
|
|
|
|
sys.stderr.write(' Emails will be printed to the console.\n')
|
|
|
|
sys.stderr.write(' To enable, ensure MAILJET_API_KEY and MAILJET_SECRET_KEY are set\n')
|
2022-09-26 22:47:38 -04:00
|
|
|
mailjet = mock_sender
|
2022-09-26 08:24:59 -04:00
|
|
|
|
2022-09-26 08:40:10 -04:00
|
|
|
|
2022-09-26 08:24:59 -04:00
|
|
|
def send_password_reset(to_email: str, reset_link: str):
|
|
|
|
data = {
|
2022-09-26 17:00:21 -04:00
|
|
|
'Messages': [
|
2022-09-26 08:24:59 -04:00
|
|
|
{
|
2022-09-26 17:00:21 -04:00
|
|
|
"From": {
|
|
|
|
"Email": "donotreply@undercover.cafe",
|
|
|
|
"Name": "UnderCover"
|
|
|
|
},
|
|
|
|
"To": [{"Email": to_email}],
|
|
|
|
"Subject": "UnderCover - Password Reset",
|
|
|
|
"TextPart": f"Complete your UnderCover password reset by visiting {reset_link}",
|
|
|
|
"HTMLPart": f"""
|
|
|
|
<h3>
|
|
|
|
<a href='{reset_link}'>Click here to complete your UnderCover password reset</a>
|
|
|
|
</h3>
|
|
|
|
<br />
|
|
|
|
Or copy and paste this link into your browser: {reset_link}
|
|
|
|
""",
|
|
|
|
"CustomID": "PasswordResetLink"
|
2022-09-26 08:24:59 -04:00
|
|
|
}
|
2022-09-26 17:00:21 -04:00
|
|
|
]
|
2022-09-26 08:24:59 -04:00
|
|
|
}
|
2022-09-26 08:52:42 -04:00
|
|
|
|
2022-09-26 22:47:38 -04:00
|
|
|
result = mailjet.send.create(data=data)
|
|
|
|
return 200 <= result.status_code <= 299
|
2022-09-26 08:24:59 -04:00
|
|
|
|
2022-09-26 08:40:10 -04:00
|
|
|
|
2022-10-02 15:09:16 -04:00
|
|
|
def exception_alert(e: Exception):
|
|
|
|
data = {
|
|
|
|
'Messages': [
|
|
|
|
{
|
|
|
|
"From": {
|
|
|
|
"Email": "ErrorReporting@undercover.cafe",
|
|
|
|
"Name": "UnderCover"
|
|
|
|
},
|
|
|
|
"To": [{"Email": "sage@sagev.space"}],
|
|
|
|
"Subject": "UnderCover Internal Error",
|
|
|
|
"TextPart": str(e) + '\n' + ''.join(traceback.format_exception(None, e, e.__traceback__)),
|
|
|
|
"CustomID": "InternalServerError"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
mailjet.send.create(data=data)
|
|
|
|
|
|
|
|
|
2022-09-26 08:40:10 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
send_password_reset('sage@sagev.space', 'https://sagev.space/')
|