Move email send features to the User model

master
kaiyou 7 years ago
parent 92f2025d7c
commit 570e90acbc

@ -3,11 +3,14 @@ from mailu import app, db, dkim, login_manager
from sqlalchemy.ext import declarative from sqlalchemy.ext import declarative
from passlib import context, hash from passlib import context, hash
from datetime import datetime from datetime import datetime
from email.mime import text
import re import re
import time import time
import os import os
import glob import glob
import smtplib
# Many-to-many association table for domain managers # Many-to-many association table for domain managers
@ -153,6 +156,18 @@ class Email(object):
primary_key=True, nullable=False, primary_key=True, nullable=False,
default=updater) default=updater)
def sendmail(self, subject, body):
""" Send an email to the address.
"""
from_address = '{}@{}'.format(
app.config['POSTMASTER'], app.config['DOMAIN'])
with smtplib.SMTP('smtp', port=10025) as smtp:
msg = text.MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_address
msg['To'] = self.email
smtp.sendmail(from_address, [self.email], msg.as_string())
def __str__(self): def __str__(self):
return self.email return self.email
@ -230,6 +245,11 @@ class User(Base, Email):
emails.extend(domain.aliases) emails.extend(domain.aliases)
return emails return emails
def send_welcome(self):
if app.config["WELCOME"].lower() == "true":
self.sendmail(app.config["WELCOME_SUBJECT"],
app.config["WELCOME_BODY"])
@classmethod @classmethod
def login(cls, email, password): def login(cls, email, password):
user = cls.query.get(email) user = cls.query.get(email)

@ -4,10 +4,6 @@
{% trans %}Public announcement{% endtrans %} {% trans %}Public announcement{% endtrans %}
{% endblock %} {% endblock %}
{% block subtitle %}
{% trans %}from{% endtrans %} {{ from_address }}
{% endblock %}
{% block content %} {% block content %}
{% call macros.box() %} {% call macros.box() %}
<form class="form" method="post" role="form"> <form class="form" method="post" role="form">

Loading…
Cancel
Save