Release 1.2: bulk upload, S3/SFTP/FTP, SMTP, password reset, user groups, git deploy
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import io
|
||||
import smtplib
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
from flask import current_app, url_for
|
||||
|
||||
from app.settings_service import get_settings
|
||||
|
||||
|
||||
def send_email(to_email, subject, body_text, body_html=None):
|
||||
settings = get_settings()
|
||||
if not settings.smtp_enabled:
|
||||
current_app.logger.warning("SMTP disabled, email not sent to %s", to_email)
|
||||
return False, "SMTP не включён в настройках админки"
|
||||
|
||||
if not settings.smtp_host or not settings.smtp_from_email:
|
||||
return False, "SMTP host или from email не настроены"
|
||||
|
||||
msg = MIMEMultipart("alternative")
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = f"{settings.smtp_from_name} <{settings.smtp_from_email}>"
|
||||
msg["To"] = to_email
|
||||
msg.attach(MIMEText(body_text, "plain", "utf-8"))
|
||||
if body_html:
|
||||
msg.attach(MIMEText(body_html, "html", "utf-8"))
|
||||
|
||||
try:
|
||||
if settings.smtp_use_tls:
|
||||
server = smtplib.SMTP(settings.smtp_host, settings.smtp_port, timeout=30)
|
||||
server.starttls()
|
||||
else:
|
||||
server = smtplib.SMTP(settings.smtp_host, settings.smtp_port, timeout=30)
|
||||
|
||||
if settings.smtp_username and settings.smtp_password:
|
||||
server.login(settings.smtp_username, settings.smtp_password)
|
||||
server.sendmail(settings.smtp_from_email, [to_email], msg.as_string())
|
||||
server.quit()
|
||||
return True, "Email отправлен"
|
||||
except Exception as exc:
|
||||
current_app.logger.exception("SMTP error")
|
||||
return False, str(exc)
|
||||
|
||||
|
||||
def send_password_reset_email(user, token):
|
||||
reset_url = url_for("auth.reset_password", token=token, _external=True)
|
||||
subject = "PhotoHost — сброс пароля"
|
||||
body = (
|
||||
f"Здравствуйте, {user.username}!\n\n"
|
||||
f"Для сброса пароля перейдите по ссылке:\n{reset_url}\n\n"
|
||||
"Ссылка действует 24 часа. Если вы не запрашивали сброс — проигнорируйте письмо."
|
||||
)
|
||||
html = (
|
||||
f"<p>Здравствуйте, <strong>{user.username}</strong>!</p>"
|
||||
f'<p><a href="{reset_url}">Сбросить пароль</a></p>'
|
||||
"<p>Ссылка действует 24 часа.</p>"
|
||||
)
|
||||
return send_email(user.email, subject, body, html)
|
||||
|
||||
|
||||
def send_welcome_email(user):
|
||||
subject = "PhotoHost — регистрация успешна"
|
||||
body = f"Добро пожаловать, {user.username}! Ваш аккаунт на PhotoHost создан."
|
||||
return send_email(user.email, subject, body)
|
||||
|
||||
|
||||
def send_upload_notification(user, count, folder_name=None):
|
||||
location = f" в папку «{folder_name}»" if folder_name else ""
|
||||
subject = f"PhotoHost — загружено {count} фото"
|
||||
body = f"Загружено {count} фото{location}."
|
||||
return send_email(user.email, subject, body)
|
||||
Reference in New Issue
Block a user