adding Sent email API

This commit is contained in:
2024-09-22 20:02:30 +02:00
parent 4612d43a5c
commit c9a0e0a7b6
8 changed files with 40 additions and 1 deletions

2
.env
View File

@@ -1 +1,3 @@
SECRET_KEY=n5i4&l#vk1c*34_(lonzy46ahy%^lq@clc@!o7s3*i(0g*=6+i
EMAIL_API=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

View File

@@ -17,6 +17,8 @@ COPY requirements.txt /app/
RUN pip install --upgrade pip \
&& pip install -r /app/requirements.txt
RUN pip install sib-api-v3-sdk
COPY . /app/
RUN python manage.py collectstatic --noinput --clear

View File

@@ -1,6 +1,7 @@
from __future__ import unicode_literals
from django.db import models
import os
from modelcluster.fields import ParentalKey
@@ -9,8 +10,16 @@ from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
from wagtail.fields import RichTextField
from wagtail.admin.panels import FieldPanel, FieldRowPanel,InlinePanel, MultiFieldPanel
from saburly.custom_editor import FULL_EDITOR
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from dotenv import load_dotenv
load_dotenv()
class FormField(AbstractFormField):
page = ParentalKey(
@@ -52,3 +61,27 @@ class ContactPage(AbstractEmailForm):
], heading = 'Email Settings'),
]
def send_mail(self, form):
form_data = '\n'.join([f"{field.label}: {form.cleaned_data.get(field.name)}" for field in form])
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = os.getenv('EMAIL_API')
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
subject = self.subject or "New Contact Form Submission"
sender = {"email": self.from_address}
to = [{"email": self.to_address}]
html_content = f"<html><body><pre>{form_data}</pre></body></html>"
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
to=to,
sender=sender,
subject=subject,
html_content=html_content
)
try:
api_instance.send_transac_email(send_smtp_email)
print("Email sent successfully")
except ApiException as e:
print(f"Failed to send email: {e}")

Binary file not shown.

View File

@@ -34,3 +34,5 @@ wagtail==6.2.1
Willow==1.8.0
whitenoise==6.0.0
django-widget-tweaks==1.4.5
sib-api-v3-sdk==7.6.0

View File

@@ -179,7 +179,7 @@ WAGTAILSEARCH_BACKENDS = {
# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
WAGTAILADMIN_BASE_URL = "http://example.com"
WAGTAILADMIN_BASE_URL = "https://saburly.com"
# Allowed file extensions for documents in the document library.
# This can be omitted to allow all files, but note that this may present a security risk