|
1 | | -from app.config import email_conf |
| 1 | +import os |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | + |
| 5 | +from app.config import (email_conf, templates, |
| 6 | + CALENDAR_SITE_NAME, CALENDAR_HOME_PAGE, |
| 7 | + CALENDAR_REGISTRATION_PAGE) |
2 | 8 | from app.database.models import Event, User |
3 | | -from fastapi import BackgroundTasks |
| 9 | +from fastapi import BackgroundTasks, UploadFile |
4 | 10 | from fastapi_mail import FastMail, MessageSchema |
| 11 | +from pydantic import EmailStr |
| 12 | +from pydantic.errors import EmailError |
5 | 13 | from sqlalchemy.orm.session import Session |
6 | 14 |
|
7 | 15 | mail = FastMail(email_conf) |
@@ -31,10 +39,142 @@ def send( |
31 | 39 | User.id == user_to_send).first() |
32 | 40 | if not user_to_send or not event_used: |
33 | 41 | return False |
34 | | - message = MessageSchema( |
35 | | - subject=f"{title} {event_used.title}", |
36 | | - recipients={"email": [user_to_send.email]}.get("email"), |
37 | | - body=f"begins at:{event_used.start} : {event_used.content}", |
38 | | - ) |
39 | | - background_tasks.add_task(mail.send_message, message) |
| 42 | + if not verify_email_pattern(user_to_send.email): |
| 43 | + return False |
| 44 | + |
| 45 | + subject = f"{title} {event_used.title}" |
| 46 | + recipients = {"email": [user_to_send.email]}.get("email") |
| 47 | + body = f"begins at:{event_used.start} : {event_used.content}" |
| 48 | + |
| 49 | + background_tasks.add_task(send_internal, |
| 50 | + subject=subject, |
| 51 | + recipients=recipients, |
| 52 | + body=body) |
40 | 53 | return True |
| 54 | + |
| 55 | + |
| 56 | +def send_email_invitation(sender_name: str, |
| 57 | + recipient_name: str, |
| 58 | + recipient_mail: str, |
| 59 | + background_tasks: BackgroundTasks = BackgroundTasks |
| 60 | + ) -> bool: |
| 61 | + """ |
| 62 | + This function takes as parameters the sender's name, |
| 63 | + the recipient's name and his email address, configuration, and |
| 64 | + sends the recipient an invitation to his email address in |
| 65 | + the format HTML. |
| 66 | + :param sender_name: str, the sender's name |
| 67 | + :param recipient_name: str, the recipient's name |
| 68 | + :param recipient_mail: str, the recipient's email address |
| 69 | + :param background_tasks: (BackgroundTasks): Function from fastapi that lets |
| 70 | + you apply tasks in the background. |
| 71 | + :return: bool, True if the invitation was successfully |
| 72 | + sent to the recipient, and False if the entered |
| 73 | + email address is incorrect. |
| 74 | + """ |
| 75 | + if not verify_email_pattern(recipient_mail): |
| 76 | + return False |
| 77 | + |
| 78 | + if not recipient_name: |
| 79 | + return False |
| 80 | + |
| 81 | + if not sender_name: |
| 82 | + return False |
| 83 | + |
| 84 | + template = templates.get_template("invite_mail.html") |
| 85 | + html = template.render(recipient=recipient_name, sender=sender_name, |
| 86 | + site_name=CALENDAR_SITE_NAME, |
| 87 | + registration_link=CALENDAR_REGISTRATION_PAGE, |
| 88 | + home_link=CALENDAR_HOME_PAGE, |
| 89 | + addr_to=recipient_mail) |
| 90 | + |
| 91 | + subject = "Invitation" |
| 92 | + recipients = [recipient_mail] |
| 93 | + body = html |
| 94 | + subtype = "html" |
| 95 | + |
| 96 | + background_tasks.add_task(send_internal, |
| 97 | + subject=subject, |
| 98 | + recipients=recipients, |
| 99 | + body=body, |
| 100 | + subtype=subtype) |
| 101 | + return True |
| 102 | + |
| 103 | + |
| 104 | +def send_email_file(file_path: str, |
| 105 | + recipient_mail: str, |
| 106 | + background_tasks: BackgroundTasks = BackgroundTasks): |
| 107 | + """ |
| 108 | + his function takes as parameters the file's path, |
| 109 | + the recipient's email address, configuration, and |
| 110 | + sends the recipient an file to his email address. |
| 111 | + :param file_path: str, the file's path |
| 112 | + :param recipient_mail: str, the recipient's email address |
| 113 | + :param background_tasks: (BackgroundTasks): Function from fastapi that lets |
| 114 | + you apply tasks in the background. |
| 115 | + :return: bool, True if the file was successfully |
| 116 | + sent to the recipient, and False if the entered |
| 117 | + email address is incorrect or file does not exist. |
| 118 | + """ |
| 119 | + if not verify_email_pattern(recipient_mail): |
| 120 | + return False |
| 121 | + |
| 122 | + if not os.path.exists(file_path): |
| 123 | + return False |
| 124 | + |
| 125 | + subject = "File" |
| 126 | + recipients = [recipient_mail] |
| 127 | + body = "file" |
| 128 | + file_attachments = [file_path] |
| 129 | + |
| 130 | + background_tasks.add_task(send_internal, |
| 131 | + subject=subject, |
| 132 | + recipients=recipients, |
| 133 | + body=body, |
| 134 | + file_attachments=file_attachments) |
| 135 | + return True |
| 136 | + |
| 137 | + |
| 138 | +async def send_internal(subject: str, |
| 139 | + recipients: List[str], |
| 140 | + body: str, |
| 141 | + subtype: Optional[str] = None, |
| 142 | + file_attachments: Optional[List[str]] = None): |
| 143 | + if file_attachments is None: |
| 144 | + file_attachments = [] |
| 145 | + |
| 146 | + message = MessageSchema( |
| 147 | + subject=subject, |
| 148 | + recipients=[EmailStr(recipient) for recipient in recipients], |
| 149 | + body=body, |
| 150 | + subtype=subtype, |
| 151 | + attachments=[UploadFile(file_attachment) |
| 152 | + for file_attachment in file_attachments]) |
| 153 | + |
| 154 | + return await send_internal_internal(message) |
| 155 | + |
| 156 | + |
| 157 | +async def send_internal_internal(msg: MessageSchema): |
| 158 | + """ |
| 159 | + This function receives message and |
| 160 | + configuration as parameters and sends the message. |
| 161 | + :param msg: MessageSchema, message |
| 162 | + :return: None |
| 163 | + """ |
| 164 | + await mail.send_message(msg) |
| 165 | + |
| 166 | + |
| 167 | +def verify_email_pattern(email: str) -> bool: |
| 168 | + """ |
| 169 | + This function checks the correctness |
| 170 | + of the entered email address |
| 171 | + :param email: str, the entered email address |
| 172 | + :return: bool, |
| 173 | + True if the entered email address is correct, |
| 174 | + False if the entered email address is incorrect. |
| 175 | + """ |
| 176 | + try: |
| 177 | + EmailStr.validate(email) |
| 178 | + return True |
| 179 | + except EmailError: |
| 180 | + return False |
0 commit comments