Initial work on mail sending
This commit is contained in:
		
							parent
							
								
									bc0dae4f97
								
							
						
					
					
						commit
						2eae36e8fd
					
				@ -1,8 +1,10 @@
 | 
			
		||||
from flask import Flask
 | 
			
		||||
from flask_jwt_extended import JWTManager
 | 
			
		||||
from flask_mail import Mail
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
jwt_manager = JWTManager(app)
 | 
			
		||||
mail = Mail(app)
 | 
			
		||||
 | 
			
		||||
def create_app():
 | 
			
		||||
	from app.api import bp, bp_errors, bp_product, bp_user, bp_cart
 | 
			
		||||
 | 
			
		||||
@ -19,4 +19,10 @@ class FlaskProduction:
 | 
			
		||||
class FlaskTesting:
 | 
			
		||||
	DEBUG = True
 | 
			
		||||
	JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY')
 | 
			
		||||
	SERVER_NAME = os.environ.get('HOST') + ':' + os.environ.get('PORT')
 | 
			
		||||
	SERVER_NAME = os.environ.get('HOST') + ':' + os.environ.get('PORT')
 | 
			
		||||
 | 
			
		||||
	MAIL_SERVER = os.environ.get('MAIL_SERVER')
 | 
			
		||||
	MAIL_PORT = os.environ.get('MAIL_USERNAME')
 | 
			
		||||
	MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
 | 
			
		||||
	MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
 | 
			
		||||
	MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS')
 | 
			
		||||
							
								
								
									
										15
									
								
								app/mail_utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								app/mail_utils.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
from flask_mail import Message
 | 
			
		||||
 | 
			
		||||
from app import mail
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def send_mail(subject: str, recipient: str, body: str):
 | 
			
		||||
	msg = Message(subject, recipients=[recipient])
 | 
			
		||||
	msg.body = body
 | 
			
		||||
 | 
			
		||||
	try:
 | 
			
		||||
		mail.send(msg)
 | 
			
		||||
		return True
 | 
			
		||||
	except Exception as e:
 | 
			
		||||
		print(f"Failed to send email. Error: {e}")
 | 
			
		||||
		return False
 | 
			
		||||
@ -9,6 +9,8 @@ from flask_jwt_extended import create_access_token
 | 
			
		||||
from app.extensions import db_cursor, db_connection
 | 
			
		||||
from app.extensions import jwt_redis_blocklist
 | 
			
		||||
 | 
			
		||||
from app.mail_utils import send_mail
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class UserService:
 | 
			
		||||
	"""
 | 
			
		||||
@ -59,6 +61,11 @@ class UserService:
 | 
			
		||||
			print(f"Error: {e}")
 | 
			
		||||
			return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500
 | 
			
		||||
 | 
			
		||||
		# TODO Implement mail sending
 | 
			
		||||
		# Currently throws error - connection refused
 | 
			
		||||
		
 | 
			
		||||
		# send_mail("Successfully registered!", email, "Congratulations! Your account has been successfully created.\nThis mail also serves as a test that the email address is correct")
 | 
			
		||||
 | 
			
		||||
		return {"Success": "User created successfully"}, 200
 | 
			
		||||
 | 
			
		||||
	@staticmethod
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user