71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
"""Employee invitation emails sent from the platform domain."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.config import get_settings
|
|
from app.models import Account, MailDomain
|
|
from app.services.domain_provisioning import get_domain_by_name
|
|
from app.services.mail_sender import deliver_email
|
|
|
|
|
|
async def send_employee_invitation(
|
|
session: AsyncSession,
|
|
*,
|
|
member: Account,
|
|
manager: Account,
|
|
initial_password: str,
|
|
force_password_change: bool,
|
|
) -> None:
|
|
if not member.invitation_email:
|
|
raise ValueError("Invitation email is required.")
|
|
|
|
settings = get_settings()
|
|
platform_domain = await get_domain_by_name(session, settings.platform_domain)
|
|
if platform_domain is None:
|
|
raise ValueError("Platform mail domain is not configured.")
|
|
|
|
login_url = f"https://{settings.app_public_host}/login"
|
|
manager_name = f"{manager.first_name} {manager.last_name}".strip()
|
|
company = member.company_domain or settings.platform_domain
|
|
|
|
change_note = (
|
|
"You will be asked to choose a new password immediately after signing in."
|
|
if force_password_change
|
|
else "You can change your password later from account settings."
|
|
)
|
|
|
|
body_text = (
|
|
f"Hi {member.first_name},\n\n"
|
|
f"{manager_name} invited you to AGMail for {company}.\n\n"
|
|
f"Sign in at: {login_url}\n"
|
|
f"Work email: {member.email}\n"
|
|
f"Temporary password: {initial_password}\n\n"
|
|
f"{change_note}\n\n"
|
|
"— AGMail"
|
|
)
|
|
body_html = (
|
|
f"<p>Hi {member.first_name},</p>"
|
|
f"<p><strong>{manager_name}</strong> invited you to AGMail for "
|
|
f"<strong>{company}</strong>.</p>"
|
|
f"<p><a href=\"{login_url}\">Sign in to AGMail</a></p>"
|
|
f"<ul>"
|
|
f"<li><strong>Work email:</strong> {member.email}</li>"
|
|
f"<li><strong>Temporary password:</strong> {initial_password}</li>"
|
|
f"</ul>"
|
|
f"<p>{change_note}</p>"
|
|
f"<p>— AGMail</p>"
|
|
)
|
|
|
|
sender = f"AGMail <noreply@{settings.platform_domain}>"
|
|
await deliver_email(
|
|
sender=sender,
|
|
recipients=[member.invitation_email],
|
|
cc=[],
|
|
bcc=[],
|
|
subject=f"You're invited to {company} on AGMail",
|
|
body_text=body_text,
|
|
body_html=body_html,
|
|
domain=platform_domain,
|
|
)
|