-
Notifications
You must be signed in to change notification settings - Fork 1
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
151 additions
and
60 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
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,76 @@ | ||
import os | ||
import boto3 | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from email.mime.application import MIMEApplication | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
|
||
def send_ses_email( | ||
subject, body_text, body_html, sender_email, recipient_emails, attachment=None | ||
): | ||
# Create a multipart/mixed parent container | ||
msg = MIMEMultipart("mixed") | ||
msg["Subject"] = subject | ||
msg["From"] = sender_email | ||
msg["To"] = ", ".join(recipient_emails) | ||
|
||
# Add body to email | ||
msg_body = MIMEMultipart("alternative") | ||
textpart = MIMEText(body_text.encode("utf-8"), "plain", "utf-8") | ||
htmlpart = MIMEText(body_html.encode("utf-8"), "html", "utf-8") | ||
|
||
msg_body.attach(textpart) | ||
msg_body.attach(htmlpart) | ||
msg.attach(msg_body) | ||
|
||
# Attachment | ||
if attachment: | ||
with open(attachment, "rb") as f: | ||
part = MIMEApplication(f.read()) | ||
part.add_header( | ||
"Content-Disposition", | ||
"attachment", | ||
filename=os.path.basename(attachment), | ||
) | ||
msg.attach(part) | ||
|
||
# Connect to AWS SES | ||
client = boto3.client( | ||
"ses", | ||
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), | ||
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), | ||
region_name=os.getenv("AWS_REGION"), | ||
) | ||
|
||
# Try to send the email. | ||
try: | ||
response = client.send_raw_email( | ||
Source=sender_email, | ||
Destinations=recipient_emails, | ||
RawMessage={"Data": msg.as_string()}, | ||
) | ||
except Exception as e: | ||
print(e) | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
subject = "Your magic link to log in to Atlas" | ||
body_text = "Content of your email." | ||
body_html = """<html> | ||
<head></head> | ||
<body> | ||
<h1>Welcome to Atlas!</h1> | ||
<p>Click <a href='https://atlas.scaledhumanity.org'>here</a> to log in</p> | ||
</body> | ||
</html>""" | ||
sender_email = "[email protected]" | ||
recipient_emails = ["[email protected]"] | ||
|
||
# Send the email | ||
send_ses_email(subject, body_text, body_html, sender_email, recipient_emails) |
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,35 @@ | ||
import asyncio | ||
import os | ||
from dotenv import load_dotenv | ||
from beanie import init_beanie | ||
from motor.motor_asyncio import AsyncIOMotorClient | ||
|
||
from models.users import User | ||
|
||
|
||
load_dotenv() | ||
|
||
|
||
# Call this from within your event loop to get beanie setup. | ||
async def init_db(): | ||
# Create Motor client | ||
client = AsyncIOMotorClient(os.getenv("DB_URI")) | ||
|
||
# Init beanie with the Product document class | ||
await init_beanie(database=client.atlas_main, document_models=[User]) | ||
|
||
|
||
async def main(): | ||
await init_db() | ||
us = User( | ||
email="[email protected]", | ||
magic_link="1234", | ||
magic_link_expiration="2021-01-01", | ||
number_of_tokens=5, | ||
) | ||
|
||
await us.create() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
from datetime import datetime | ||
from typing import Optional | ||
from beanie import Document | ||
from beanie.operators import * | ||
|
||
|
||
class User(Document): | ||
email: str | ||
magic_link: str | ||
magic_link_expiration: datetime | ||
number_of_tokens: Optional[int] | ||
created_at: datetime = datetime.now() | ||
updated_at: datetime = datetime.now() | ||
|
||
class Settings: | ||
name = "users" |
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