send confirmation email after form submission

This commit is contained in:
Mazyar
2025-12-07 10:52:31 +03:30
parent b8a10f355e
commit fbf99177a9
3 changed files with 400 additions and 2 deletions
+349
View File
@@ -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 := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Confirmation - ` + data.CompanyName + `</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f8fafc;
}
.email-container {
max-width: 600px;
margin: 16px auto;
background-color: #ffffff;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 40px 30px;
text-align: center;
color: white;
}
.header h1 {
font-size: 28px;
font-weight: 700;
margin-bottom: 10px;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.header p {
font-size: 16px;
opacity: 0.9;
font-weight: 300;
}
.content {
padding: 40px 30px;
}
.confirmation-card {
background-color: #f0f9ff;
border: 2px solid #0ea5e9;
border-radius: 12px;
padding: 30px;
margin: 30px 0;
position: relative;
}
.confirmation-card::before {
content: '✓';
position: absolute;
top: -15px;
left: 20px;
background-color: #0ea5e9;
color: white;
width: 30px;
height: 30px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
font-weight: bold;
}
.success-message {
color: #0c4a6e;
font-size: 18px;
font-weight: 600;
margin-bottom: 20px;
text-align: center;
}
.contact-details {
background-color: white;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
border-left: 4px solid #0ea5e9;
}
.detail-row {
display: flex;
margin-bottom: 12px;
align-items: flex-start;
}
.detail-label {
font-weight: 600;
color: #374151;
min-width: 120px;
font-size: 14px;
}
.detail-value {
color: #6b7280;
flex: 1;
font-size: 14px;
}
.message-preview {
background-color: #f9fafb;
border-radius: 8px;
padding: 15px;
margin-top: 15px;
border-left: 3px solid #6b7280;
}
.message-preview strong {
color: #374151;
display: block;
margin-bottom: 8px;
}
.timestamp {
text-align: center;
color: #6b7280;
font-size: 14px;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #e5e7eb;
}
.footer {
background-color: #f8fafc;
padding: 30px;
text-align: center;
border-top: 1px solid #e5e7eb;
}
.footer p {
color: #6b7280;
font-size: 14px;
margin-bottom: 10px;
}
.footer a {
color: #667eea;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
.support-info {
background-color: #fffbeb;
border: 1px solid #fbbf24;
border-radius: 8px;
padding: 15px;
margin-top: 20px;
}
.support-info h4 {
color: #92400e;
margin-bottom: 8px;
font-size: 16px;
}
.support-info p {
color: #78350f;
font-size: 14px;
margin-bottom: 5px;
}
@media (max-width: 600px) {
.email-container {
margin: 16px;
border-radius: 0;
}
.content {
padding: 20px;
}
.detail-row {
flex-direction: column;
gap: 4px;
}
.header {
padding: 30px 20px;
}
.header h1 {
font-size: 24px;
}
}
</style>
</head>
<body>
<div class="email-container">
<!-- Header -->
<div class="header">
<h1>Thank You for Contacting Us!</h1>
<p>Your message has been received and we'll get back to you soon.</p>
</div>
<!-- Content -->
<div class="content">
<div class="confirmation-card">
<div class="success-message">
✓ Your contact form has been submitted successfully!
</div>
<div class="contact-details">
<div class="detail-row">
<span class="detail-label">Full Name:</span>
<span class="detail-value">{{FULL_NAME}}</span>
</div>
<div class="detail-row">
<span class="detail-label">Email:</span>
<span class="detail-value">{{EMAIL}}</span>
</div>
<div class="detail-row">
<span class="detail-label">Phone:</span>
<span class="detail-value">{{PHONE}}</span>
</div>
<div class="detail-row">
<span class="detail-label">Reference ID:</span>
<span class="detail-value">{{CONTACT_ID}}</span>
</div>
<div class="message-preview">
<strong>Your Message:</strong>
{{MESSAGE}}
</div>
</div>
<div class="timestamp">
<strong>Submitted on:</strong> {{SUBMITTED_AT}}
<br>
<p>This email confirms your submission to ` + data.CompanyName + `</p>
</div>
</div>
<div class="support-info">
<h4>Need Immediate Assistance?</h4>
<p><strong>Email:</strong> <a href="mailto:{{SUPPORT_EMAIL}}">{{SUPPORT_EMAIL}}</a></p>
<p><strong>Website:</strong> <a href="{{COMPANY_URL}}" target="_blank">{{COMPANY_URL}}</a></p>
<p>Our team typically responds within 24 hours during business days.</p>
</div>
</div>
<!-- Footer -->
<div class="footer">
<p>This is an automated confirmation email from ` + data.CompanyName + `</p>
<p>Please do not reply to this email. For questions, contact us at <a href="mailto:{{SUPPORT_EMAIL}}">{{SUPPORT_EMAIL}}</a></p>
</div>
</div>
</body>
</html>`
// 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,
)
}
+50 -1
View File
@@ -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, &notification.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, &notification.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,