91 lines
3.3 KiB
Python
91 lines
3.3 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
|
|
|
|
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_team_join_email(sender,user,team,activation_link):
|
|
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,user,team,activation_link):
|
|
context = {
|
|
'name': user.firstname or user.email,
|
|
'invitor': sender.lastname,
|
|
'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(sender, 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)
|
|
|