Files
2025-12-07 23:52:17 +03:30

297 lines
7.9 KiB
Go

package inquiry
import (
"fmt"
"html"
"strings"
"time"
"tm/pkg/security"
)
// EmailTemplateData represents the data needed for the email template
type EmailTemplateData struct {
FullName string
CompanyName string
WorkEmail string
PhoneNumber string
InquiryID string
CreatedAt int64
CompanyLogo string
CompanyURL string
SupportEmail string
}
// GenerateNewInquiryEmailTemplate generates a beautiful HTML email template for new inquiry notifications
func GenerateNewInquiryEmailTemplate(data *EmailTemplateData, xssPolicy *security.XSSPolicy) string {
// Sanitize all user inputs before inserting into HTML
safeFullName := html.EscapeString(data.FullName)
safeCompanyName := html.EscapeString(data.CompanyName)
safeWorkEmail := html.EscapeString(data.WorkEmail)
safePhoneNumber := html.EscapeString(data.PhoneNumber)
safeInquiryID := html.EscapeString(data.InquiryID)
// Format the creation date
createdAt := time.Unix(data.CreatedAt, 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>New Inquiry Request - {{COMPANY_NAME}}</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(0deg, #5750F1 0%, #e0e0e0 100%);
padding: 40px 30px;
text-align: center;
color: white;
}
.logo {
max-width: 200px;
height: auto;
margin-bottom: 20px;
border-radius: 8px;
}
.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;
}
.inquiry-card {
background-color: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 30px;
margin: 30px 0;
position: relative;
}
.inquiry-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #5750F1, #e0e0e0);
border-radius: 12px 12px 0 0;
}
.inquiry-id {
background-color: #5750F1;
color: white;
padding: 8px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
display: inline-block;
margin-bottom: 20px;
letter-spacing: 0.5px;
}
.info-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.info-item {
background-color: white;
padding: 20px;
border-radius: 8px;
border: 1px solid #e2e8f0;
}
.info-label {
font-size: 12px;
color: #64748b;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 8px;
}
.info-value {
font-size: 16px;
color: #1e293b;
font-weight: 500;
}
.timestamp {
text-align: center;
color: #64748b;
font-size: 14px;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #e2e8f0;
}
.footer {
background-color: #f8fafc;
padding: 30px;
text-align: center;
border-top: 1px solid #e2e8f0;
}
.footer p {
color: #64748b;
font-size: 14px;
margin-bottom: 10px;
}
.footer a {
color: #5750F1;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
@media (max-width: 600px) {
.email-container {
margin: 16px;
border-radius: 0;
}
.content {
padding: 20px;
}
.info-grid {
grid-template-columns: 1fr;
gap: 15px;
}
.header {
padding: 30px 20px;
}
.header h1 {
font-size: 24px;
}
}
</style>
</head>
<body>
<div class="email-container">
<!-- Header -->
<div class="header">
<h1>New Inquiry Received</h1>
<p>A new inquiry has been submitted to the opplens system</p>
</div>
<!-- Content -->
<div class="content">
<div class="inquiry-card">
<div class="inquiry-id">Identifier: {{INQUIRY_ID}}</div>
<div class="info-grid">
<div class="info-item">
<div class="info-label">Full Name</div>
<div class="info-value">{{FULL_NAME}}</div>
</div>
<div class="info-item">
<div class="info-label">Company Name</div>
<div class="info-value">{{COMPANY_NAME}}</div>
</div>
<div class="info-item">
<div class="info-label">Work Email</div>
<div class="info-value">{{WORK_EMAIL}}</div>
</div>
<div class="info-item">
<div class="info-label">Phone Number</div>
<div class="info-value">{{PHONE_NUMBER}}</div>
</div>
</div>
<div class="timestamp">
<strong>Submitted on:</strong> {{CREATED_AT}}
<br>
<p>This email was automatically sent from the Opplens System</p>
</div>
</div>
</div>
</div>
</body>
</html>`
// Replace placeholders with sanitized data
html = strings.ReplaceAll(html, "{{COMPANY_NAME}}", safeCompanyName)
html = strings.ReplaceAll(html, "{{INQUIRY_ID}}", safeInquiryID)
html = strings.ReplaceAll(html, "{{FULL_NAME}}", safeFullName)
html = strings.ReplaceAll(html, "{{WORK_EMAIL}}", safeWorkEmail)
html = strings.ReplaceAll(html, "{{PHONE_NUMBER}}", safePhoneNumber)
html = strings.ReplaceAll(html, "{{CREATED_AT}}", createdAt)
return html
}
// GenerateNewInquiryEmailMessage generates a simple text message for the email
func GenerateNewInquiryEmailMessage(data *EmailTemplateData) string {
createdAt := time.Unix(data.CreatedAt, 0).Format("January 2, 2006 at 3:04 PM")
return fmt.Sprintf(`
New Inquiry Received
Inquiry ID: %s
Full Name: %s
Company Name: %s
Work Email: %s
Phone Number: %s
Submitted on: %s
Please check the admin panel for more details.
`,
data.InquiryID,
data.FullName,
data.CompanyName,
data.WorkEmail,
data.PhoneNumber,
createdAt,
)
}