adding Sent email API
This commit is contained in:
@@ -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}")
|
||||
Reference in New Issue
Block a user