in api/views/emails.py [0:0]
def first_forwarded_email(request):
"""
Requires `free_user_onboarding` flag to be active for the user.
Send the `first_forwarded_email.html` email to the user via a mask.
See [/emails/first_forwarded_email](/emails/first_forwarded_email).
Note: `mask` value must be a `RelayAddress` that belongs to the authenticated user.
A `DomainAddress` will not work.
"""
if not flag_is_active(request, "free_user_onboarding"):
# Return Permission Denied error
return Response(
{"detail": "Requires free_user_onboarding waffle flag."}, status=403
)
serializer = FirstForwardedEmailSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
mask = str(serializer.data.get("mask"))
user = request.user
try:
address = _get_address(mask)
RelayAddress.objects.get(user=user, address=address)
except ObjectDoesNotExist:
return Response(f"{mask} does not exist for user.", status=HTTP_404_NOT_FOUND)
profile = user.profile
app_config = apps.get_app_config("emails")
if not isinstance(app_config, EmailsConfig):
raise TypeError("app_config must be type EmailsConfig")
ses_client = app_config.ses_client
if not ses_client:
raise ValueError("ses_client must be truthy value.")
if not settings.RELAY_FROM_ADDRESS:
raise ValueError("settings.RELAY_FROM_ADDRESS must have a value.")
with django_ftl.override(profile.language):
translated_subject = ftl_bundle.format("forwarded-email-hero-header")
first_forwarded_email_html = render_to_string(
"emails/first_forwarded_email.html",
{
"SITE_ORIGIN": settings.SITE_ORIGIN,
},
)
from_address = generate_from_header(settings.RELAY_FROM_ADDRESS, mask)
wrapped_email = wrap_html_email(
first_forwarded_email_html,
profile.language,
profile.has_premium,
from_address,
0,
)
ses_client.send_email(
Destination={
"ToAddresses": [user.email],
},
Source=from_address,
Message={
"Subject": ses_message_props(translated_subject),
"Body": {
"Html": ses_message_props(wrapped_email),
},
},
)
logger.info(f"Sent first_forwarded_email to user ID: {user.id}")
return Response(status=HTTP_201_CREATED)