114 lines
4.1 KiB
Python
114 lines
4.1 KiB
Python
import os
|
|
from django.template.loader import render_to_string
|
|
from django.conf import settings
|
|
import logging
|
|
from django.core.mail import send_mail
|
|
from django.urls import reverse
|
|
import uuid
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def load_email_template(template_name, context):
|
|
template_path = os.path.join('email', template_name)
|
|
email_content = render_to_string(template_path, context)
|
|
|
|
# 件名と本文を分離
|
|
subject, _, body = email_content.partition('\n\n')
|
|
subject = subject.replace('件名: ', '').strip()
|
|
|
|
# 件名と本文を分離し、件名から改行を削除
|
|
subject, _, body = email_content.partition('\n\n')
|
|
subject = subject.replace('件名: ', '').strip().replace('\n', ' ')
|
|
|
|
return subject, body
|
|
|
|
def share_send_email(subject, body, recipient_email):
|
|
try:
|
|
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [recipient_email], fail_silently=False)
|
|
logger.info(f"メールを送信しました。 受信者: {recipient_email}")
|
|
except Exception as e:
|
|
logger.error(f"メールの送信に失敗しました。 受信者: {recipient_email}, エラー: {str(e)}")
|
|
raise # エラーを再度発生させて、呼び出し元で処理できるようにします
|
|
|
|
|
|
# 自らユーザー登録した際に、メールの確認メールを送る。
|
|
#
|
|
def send_verification_email(user, activation_link):
|
|
context = {
|
|
'name': user.firstname or user.email,
|
|
'activation_link': activation_link,
|
|
}
|
|
logger.info(f"send_verification_email : {context}")
|
|
subject, body = load_email_template('verification_email.txt', context)
|
|
share_send_email(subject,body,user.email)
|
|
|
|
|
|
def send_reset_password_email(email,activation_link):
|
|
context = {
|
|
'name': email,
|
|
'activation_link': activation_link,
|
|
}
|
|
logger.info(f"send_reset_password_email : {context}")
|
|
subject, body = load_email_template('reset_password_email.txt', context)
|
|
share_send_email(subject,body,email)
|
|
|
|
|
|
# 既にユーザーになっている人にチームへの参加要請メールを出す。
|
|
#
|
|
def send_team_join_email(request,sender,user,team):
|
|
activation_link = request.build_absolute_uri(
|
|
reverse('activate-member', args=[user.id, team.id])
|
|
)
|
|
logger.info(f"request: {request}")
|
|
|
|
context = {
|
|
'name': user.lastname or user.email,
|
|
'invitor': sender.lastname,
|
|
'activation_link': activation_link,
|
|
'team_name': team.team_name,
|
|
}
|
|
|
|
subject, body = load_email_template('invitation_existing_email.txt', context)
|
|
share_send_email(subject,body,user.email)
|
|
|
|
|
|
|
|
# まだユーザーでない人にチームメンバー招待メールを送る
|
|
# その人がユーザー登録して、ユーザー登録されるとメンバーになる。
|
|
# アプリからユーザー登録するため、アプリのダウンロードリンクも送る。
|
|
#
|
|
def send_invitation_email(sender,request,user_email,team):
|
|
|
|
verification_code = uuid.uuid4() # UUIDを生成
|
|
activation_link = request.build_absolute_uri(
|
|
reverse('activate-new-member', args=[verification_code, team.id])
|
|
)
|
|
|
|
|
|
context = {
|
|
'name': user_email,
|
|
'invitor': sender.lastname,
|
|
'team_name': team.team_name,
|
|
'activation_link': activation_link,
|
|
'app_download_link': settings.APP_DOWNLOAD_LINK,
|
|
'android_download_link': settings.ANDROID_DOWNLOAD_LINK,
|
|
}
|
|
|
|
subject, body = load_email_template('invitation_new_email.txt', context)
|
|
share_send_email(subject,body,user_email)
|
|
|
|
|
|
|
|
# 招待された後にユーザー登録された場合、ヴェリフィケーションでチーム参加登録される。
|
|
#
|
|
def send_invitaion_and_verification_email(user, team, activation_link):
|
|
context = {
|
|
'name': user.firstname or user.email,
|
|
'activation_link': activation_link,
|
|
'team_name': team.team_name,
|
|
}
|
|
logger.info(f"send_invitation_and_verification_email : {context}")
|
|
subject, body = load_email_template('invitation_and_verification_email.txt', context)
|
|
share_send_email(subject,body,user.email)
|
|
|