From fbf99177a920069557b0ee8d93ec6691cf6be88d Mon Sep 17 00:00:00 2001 From: Mazyar Date: Sun, 7 Dec 2025 10:52:31 +0330 Subject: [PATCH] send confirmation email after form submission --- cmd/web/main.go | 2 +- internal/contact/email_template.go | 349 +++++++++++++++++++++++++++++ internal/contact/service.go | 51 ++++- 3 files changed, 400 insertions(+), 2 deletions(-) create mode 100644 internal/contact/email_template.go diff --git a/cmd/web/main.go b/cmd/web/main.go index f5b7db2..004cb48 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -178,7 +178,7 @@ func main() { inquiryService := inquiry.NewService(inquiryRepository, notificationSDK, logger) flagService := assets.NewService(logger, conf.Assets.FlagsPath) notificationService := notification.NewService(notificationSDK, userService, customerService, logger) - contactService := contact.NewService(contactRepository, logger) + contactService := contact.NewService(contactRepository, logger, notificationSDK) cmsService := cms.NewService(cmsRepository, logger) scraperService := scraper.NewService(conf.Scraper.BaseURL, conf.Scraper.Timeout, logger, conf.Scraper.Username, conf.Scraper.Password, conf.Scraper.LoginURL) logger.Info("Services initialized successfully", map[string]interface{}{ diff --git a/internal/contact/email_template.go b/internal/contact/email_template.go new file mode 100644 index 0000000..f7952af --- /dev/null +++ b/internal/contact/email_template.go @@ -0,0 +1,349 @@ +package contact + +import ( + "fmt" + "strings" + "time" +) + +// ContactEmailTemplateData represents the data needed for contact confirmation email +type ContactEmailTemplateData struct { + FullName string + Email string + Phone string + Message string + ContactID string + SubmittedAt int64 + CompanyName string + CompanyURL string + SupportEmail string +} + +// GenerateContactConfirmationEmailTemplate generates a beautiful HTML email template for contact confirmation +func GenerateContactConfirmationEmailTemplate(data *ContactEmailTemplateData) string { + // Format the submission date + submittedAt := time.Unix(data.SubmittedAt, 0).Format("January 2, 2006 at 3:04 PM") + + html := ` + + + + + + Contact Confirmation - ` + data.CompanyName + ` + + + +
+ +
+

Thank You for Contacting Us!

+

Your message has been received and we'll get back to you soon.

+
+ + +
+
+
+ ✓ Your contact form has been submitted successfully! +
+ +
+
+ Full Name: + {{FULL_NAME}} +
+ +
+ Email: + {{EMAIL}} +
+ +
+ Phone: + {{PHONE}} +
+ +
+ Reference ID: + {{CONTACT_ID}} +
+ +
+ Your Message: + {{MESSAGE}} +
+
+ +
+ Submitted on: {{SUBMITTED_AT}} +
+

This email confirms your submission to ` + data.CompanyName + `

+
+
+ +
+

Need Immediate Assistance?

+

Email: {{SUPPORT_EMAIL}}

+

Website: {{COMPANY_URL}}

+

Our team typically responds within 24 hours during business days.

+
+
+ + + +
+ +` + + // Replace placeholders with actual data + html = strings.ReplaceAll(html, "{{COMPANY_NAME}}", data.CompanyName) + html = strings.ReplaceAll(html, "{{FULL_NAME}}", data.FullName) + html = strings.ReplaceAll(html, "{{EMAIL}}", data.Email) + html = strings.ReplaceAll(html, "{{PHONE}}", data.Phone) + html = strings.ReplaceAll(html, "{{CONTACT_ID}}", data.ContactID) + html = strings.ReplaceAll(html, "{{MESSAGE}}", data.Message) + html = strings.ReplaceAll(html, "{{SUBMITTED_AT}}", submittedAt) + html = strings.ReplaceAll(html, "{{COMPANY_URL}}", data.CompanyURL) + html = strings.ReplaceAll(html, "{{SUPPORT_EMAIL}}", data.SupportEmail) + + return html +} + +// GenerateContactConfirmationEmailMessage generates a simple text message for the email +func GenerateContactConfirmationEmailMessage(data *ContactEmailTemplateData) string { + submittedAt := time.Unix(data.SubmittedAt, 0).Format("January 2, 2006 at 3:04 PM") + + return fmt.Sprintf(` +Contact Form Submission Confirmation + +Thank you for contacting us! Your message has been received successfully. + +Contact Details: +- Full Name: %s +- Email: %s +- Phone: %s +- Reference ID: %s +- Submitted on: %s + +Your Message: +%s + +Our team will review your inquiry and get back to you within 24 hours during business days. + +For urgent matters, please contact us directly: +Email: %s +Website: %s + +This is an automated confirmation email from %s. +Please do not reply to this email. +`, + data.FullName, + data.Email, + data.Phone, + data.ContactID, + submittedAt, + data.Message, + data.SupportEmail, + data.CompanyURL, + data.CompanyName, + ) +} diff --git a/internal/contact/service.go b/internal/contact/service.go index 9c6dbc7..57361aa 100644 --- a/internal/contact/service.go +++ b/internal/contact/service.go @@ -5,6 +5,7 @@ import ( "fmt" "tm/pkg/logger" orm "tm/pkg/mongo" + "tm/pkg/notification" "tm/pkg/response" ) @@ -33,13 +34,15 @@ type Service interface { type contactService struct { repo ContactRepository logger logger.Logger + sdk notification.SDK } // NewService creates a new contact service -func NewService(repo ContactRepository, logger logger.Logger) Service { +func NewService(repo ContactRepository, logger logger.Logger, sdk notification.SDK) Service { return &contactService{ repo: repo, logger: logger, + sdk: sdk, } } @@ -68,6 +71,52 @@ func (s *contactService) Create(ctx context.Context, form *CreateContactForm) (* return nil, fmt.Errorf("failed to create contact: %w", err) } + // Send confirmation email + s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{ + UserID: contact.GetID(), + Title: "Contact Form Submission Confirmation", + Message: GenerateContactConfirmationEmailTemplate(&ContactEmailTemplateData{ + FullName: contact.FullName, + Email: contact.Email, + Phone: contact.Phone, + Message: contact.Message, + ContactID: contact.GetID(), + SubmittedAt: contact.CreatedAt, + CompanyName: "Opplens", + CompanyURL: "https://opplens.com", + SupportEmail: "info@opplens.com", + }), + Type: "confirmation", + Priority: "normal", + EventType: notification.EventTypeEmail, + Methods: notification.NotificationMethods{ + Email: contact.Email, + }, + }) + + // Notify admin/internal team + s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{ + UserID: contact.GetID(), + Title: fmt.Sprintf("New Contact Form Submission from %s", contact.FullName), + Message: GenerateContactConfirmationEmailTemplate(&ContactEmailTemplateData{ + FullName: contact.FullName, + Email: contact.Email, + Phone: contact.Phone, + Message: contact.Message, + ContactID: contact.GetID(), + SubmittedAt: contact.CreatedAt, + CompanyName: "Opplens", + CompanyURL: "https://opplens.com", + SupportEmail: "info@opplens.com", + }), + Type: "alert", + Priority: "normal", + EventType: notification.EventTypeEmail, + Methods: notification.NotificationMethods{ + Email: "info@opplens.com", + }, + }) + s.logger.Info("Contact message created successfully", map[string]interface{}{ "id": contact.GetID(), "email": form.Email,