Try sending mail

by roundabout, Wednesday, 7 February 2024, 14:44:00 (1707317040), pushed by roundabout, Wednesday, 31 July 2024, 06:54:41 (1722408881)

Author identity: vlad <vlad.muntoiu@gmail.com>

b31a050aa1ddd7f5bc31e4a9d7b0a89f1e5f639f

celery_tasks.py

@@ -1,6 +1,7 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            import time
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            import os
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            import config
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        import email_send
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            from celery import shared_task
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            from app import db
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            from misc_utils import *
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -14,6 +15,16 @@ def send_notification(notification_id, users, level):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                for user in users:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    db.session.add(UserNotification(db.session.get(User, user), notification, level))
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                with (SMTP(config.MAIL_SERVER) as mail):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    if notification.data.get("type") == "welcome":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        message = ("Subject:Welcome"
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                 + email_send.render_email_template("welcome.html", username=user))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    mail.sendmail(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            config.NOTIFICATION_EMAIL,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            db.session.get(User, user).email,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            message,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                db.session.commit()
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                return 0  # notification sent successfully
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

config.py

@@ -6,6 +6,7 @@ DB_PASSWORD: str = os.environ.get("DB_PASSWORD")

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            DB_URI: str = f"postgresql://root:{DB_PASSWORD}@localhost/roundabout"
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            REDIS_URI: str = "redis://localhost"
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            MAIL_SERVER: str = "localhost"
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        NOTIFICATION_EMAIL: str = "notifications@roundabout-host.com"
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            CONTACT_EMAIL: str = "root@roundabout-host.com"
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            REPOS_PATH: str = "./repos"
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

email_send.py

@@ -0,0 +1,11 @@

                                
                                
                                
                            
                                
                                    
                                        
                                        import config
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        from jinja2 import Environment, FileSystemLoader, select_autoescape
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def render_email_template(template, **kwargs):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            env = Environment(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                loader=FileSystemLoader("email_templates"),
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                autoescape=select_autoescape(["html", "xml"])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            template = env.get_template(template)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return template.render(**kwargs, config=config)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

email_templates/welcome.html

@@ -0,0 +1,7 @@

                                
                                
                                
                            
                                
                                    
                                        
                                        <h1>Hello, {{ username }}</h1>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        <p>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            Welcome to the roundabout. Make yourself at home.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        </p>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        <small>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            For any inquiries, write to <a href="mailto:{{ config.CONTACT_EMAIL }}"></a><br>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        </small>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

templates/login.html

@@ -74,7 +74,7 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    </x-vbox>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    <label>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        <input id="signup-terms" type="checkbox" name="tos" required>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    I accept the <a href="/help/policies">policies listed here</a>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    <span>I accept the <a href="/help/policies">policies listed here</a></span>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    </label>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    <button style="width: 100%;" id="signup-submit" type="submit">Sign up</button>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                </x-vbox>