| 
									
										
										
										
											2024-05-29 19:06:16 +09:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | from typing import Optional | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import resend | 
					
						
							|  |  |  | from flask import Flask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Mail: | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self._client = None | 
					
						
							|  |  |  |         self._default_send_from = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def is_inited(self) -> bool: | 
					
						
							|  |  |  |         return self._client is not None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def init_app(self, app: Flask): | 
					
						
							|  |  |  |         if app.config.get('MAIL_TYPE'): | 
					
						
							|  |  |  |             if app.config.get('MAIL_DEFAULT_SEND_FROM'): | 
					
						
							|  |  |  |                 self._default_send_from = app.config.get('MAIL_DEFAULT_SEND_FROM') | 
					
						
							| 
									
										
										
										
											2024-05-29 19:06:16 +09:00
										 |  |  |              | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |             if app.config.get('MAIL_TYPE') == 'resend': | 
					
						
							|  |  |  |                 api_key = app.config.get('RESEND_API_KEY') | 
					
						
							|  |  |  |                 if not api_key: | 
					
						
							|  |  |  |                     raise ValueError('RESEND_API_KEY is not set') | 
					
						
							| 
									
										
										
										
											2024-02-07 18:08:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-10 18:15:02 +08:00
										 |  |  |                 api_url = app.config.get('RESEND_API_URL') | 
					
						
							| 
									
										
										
										
											2024-01-10 21:14:10 +08:00
										 |  |  |                 if api_url: | 
					
						
							|  |  |  |                     resend.api_url = api_url | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |                 resend.api_key = api_key | 
					
						
							|  |  |  |                 self._client = resend.Emails | 
					
						
							| 
									
										
										
										
											2024-02-07 18:08:41 +08:00
										 |  |  |             elif app.config.get('MAIL_TYPE') == 'smtp': | 
					
						
							|  |  |  |                 from libs.smtp import SMTPClient | 
					
						
							|  |  |  |                 if not app.config.get('SMTP_SERVER') or not app.config.get('SMTP_PORT'): | 
					
						
							|  |  |  |                     raise ValueError('SMTP_SERVER and SMTP_PORT are required for smtp mail type') | 
					
						
							| 
									
										
										
										
											2024-05-30 05:56:46 -05:00
										 |  |  |                 if not app.config.get('SMTP_USE_TLS') and app.config.get('SMTP_OPPORTUNISTIC_TLS'): | 
					
						
							|  |  |  |                     raise ValueError('SMTP_OPPORTUNISTIC_TLS is not supported without enabling SMTP_USE_TLS') | 
					
						
							| 
									
										
										
										
											2024-02-07 18:08:41 +08:00
										 |  |  |                 self._client = SMTPClient( | 
					
						
							|  |  |  |                     server=app.config.get('SMTP_SERVER'), | 
					
						
							|  |  |  |                     port=app.config.get('SMTP_PORT'), | 
					
						
							|  |  |  |                     username=app.config.get('SMTP_USERNAME'), | 
					
						
							|  |  |  |                     password=app.config.get('SMTP_PASSWORD'), | 
					
						
							|  |  |  |                     _from=app.config.get('MAIL_DEFAULT_SEND_FROM'), | 
					
						
							| 
									
										
										
										
											2024-05-30 05:56:46 -05:00
										 |  |  |                     use_tls=app.config.get('SMTP_USE_TLS'), | 
					
						
							|  |  |  |                     opportunistic_tls=app.config.get('SMTP_OPPORTUNISTIC_TLS') | 
					
						
							| 
									
										
										
										
											2024-02-07 18:08:41 +08:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 raise ValueError('Unsupported mail type {}'.format(app.config.get('MAIL_TYPE'))) | 
					
						
							| 
									
										
										
										
											2024-05-29 19:06:16 +09:00
										 |  |  |         else: | 
					
						
							|  |  |  |             logging.warning('MAIL_TYPE is not set') | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2023-07-14 11:19:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def send(self, to: str, subject: str, html: str, from_: Optional[str] = None): | 
					
						
							|  |  |  |         if not self._client: | 
					
						
							|  |  |  |             raise ValueError('Mail client is not initialized') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not from_ and self._default_send_from: | 
					
						
							|  |  |  |             from_ = self._default_send_from | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not from_: | 
					
						
							|  |  |  |             raise ValueError('mail from is not set') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not to: | 
					
						
							|  |  |  |             raise ValueError('mail to is not set') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not subject: | 
					
						
							|  |  |  |             raise ValueError('mail subject is not set') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not html: | 
					
						
							|  |  |  |             raise ValueError('mail html is not set') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._client.send({ | 
					
						
							|  |  |  |             "from": from_, | 
					
						
							|  |  |  |             "to": to, | 
					
						
							|  |  |  |             "subject": subject, | 
					
						
							|  |  |  |             "html": html | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def init_app(app: Flask): | 
					
						
							|  |  |  |     mail.init_app(app) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | mail = Mail() |