EmailTemplateEngine.java
18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.ecommerce.notification.template;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Map;
@Component
public class EmailTemplateEngine {
public String processTemplate(String template, Map<String, Object> variables) {
if (!StringUtils.hasText(template) || variables == null) {
return template;
}
String processed = template;
for (Map.Entry<String, Object> entry : variables.entrySet()) {
String placeholder = "{{" + entry.getKey() + "}}";
String value = entry.getValue() != null ? entry.getValue().toString() : "";
processed = processed.replace(placeholder, value);
}
return processed;
}
public String generateOrderConfirmationEmail(Map<String, Object> variables) {
StringBuilder template = new StringBuilder();
template.append("<!DOCTYPE html>")
.append("<html>")
.append("<head>")
.append(" <meta charset=\"UTF-8\">")
.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
.append(" <title>Order Confirmation</title>")
.append(" <style>")
.append(" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; }")
.append(" .container { max-width: 600px; margin: 0 auto; background: #ffffff; padding: 0; }")
.append(" .header { background: #4CAF50; color: white; padding: 20px; text-align: center; }")
.append(" .content { padding: 30px; background: white; }")
.append(" .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f9f9f9; }")
.append(" .order-details { background: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0; }")
.append(" .button { display: inline-block; padding: 12px 24px; background: #4CAF50; color: white; text-decoration: none; border-radius: 4px; margin: 10px 0; }")
.append(" </style>")
.append("</head>")
.append("<body>")
.append(" <div class=\"container\">")
.append(" <div class=\"header\">")
.append(" <h1>Order Confirmation</h1>")
.append(" </div>")
.append(" <div class=\"content\">")
.append(" <h2>Thank you for your order!</h2>")
.append(" <p>Dear {{customerName}},</p>")
.append(" <p>Your order <strong>{{orderNumber}}</strong> has been received and is being processed.</p>")
.append(" <div class=\"order-details\">")
.append(" <h3>Order Summary</h3>")
.append(" <p><strong>Order Number:</strong> {{orderNumber}}</p>")
.append(" <p><strong>Order Date:</strong> {{orderDate}}</p>")
.append(" <p><strong>Total Amount:</strong> ${{totalAmount}}</p>")
.append(" <p><strong>Shipping Address:</strong><br>{{shippingAddress}}</p>")
.append(" </div>")
.append(" <p>We'll send you another email when your order ships. You can track your order status anytime by clicking the button below:</p>")
.append(" <a href=\"{{trackingUrl}}\" class=\"button\">Track Your Order</a>")
.append(" <p>If you have any questions about your order, please contact our customer service team.</p>")
.append(" <p>Thank you for shopping with us!</p>")
.append(" <p><strong>The E-Commerce Team</strong></p>")
.append(" </div>")
.append(" <div class=\"footer\">")
.append(" <p>© 2024 E-Commerce Store. All rights reserved.</p>")
.append(" <p>1234 Commerce Street, Business City, BC 12345</p>")
.append(" </div>")
.append(" </div>")
.append("</body>")
.append("</html>");
return processTemplate(template.toString(), variables);
}
public String generatePaymentSuccessEmail(Map<String, Object> variables) {
StringBuilder template = new StringBuilder();
template.append("<!DOCTYPE html>")
.append("<html>")
.append("<head>")
.append(" <meta charset=\"UTF-8\">")
.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
.append(" <title>Payment Successful</title>")
.append(" <style>")
.append(" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; }")
.append(" .container { max-width: 600px; margin: 0 auto; background: #ffffff; padding: 0; }")
.append(" .header { background: #2196F3; color: white; padding: 20px; text-align: center; }")
.append(" .content { padding: 30px; background: white; }")
.append(" .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f9f9f9; }")
.append(" .payment-details { background: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0; }")
.append(" </style>")
.append("</head>")
.append("<body>")
.append(" <div class=\"container\">")
.append(" <div class=\"header\">")
.append(" <h1>Payment Successful</h1>")
.append(" </div>")
.append(" <div class=\"content\">")
.append(" <h2>Thank you for your payment!</h2>")
.append(" <p>Dear {{customerName}},</p>")
.append(" <p>Your payment for order <strong>{{orderNumber}}</strong> has been processed successfully.</p>")
.append(" <div class=\"payment-details\">")
.append(" <h3>Payment Details</h3>")
.append(" <p><strong>Payment ID:</strong> {{paymentId}}</p>")
.append(" <p><strong>Amount:</strong> ${{amount}}</p>")
.append(" <p><strong>Date:</strong> {{paymentDate}}</p>")
.append(" <p><strong>Payment Method:</strong> {{paymentMethod}}</p>")
.append(" </div>")
.append(" <p>Your order is now being prepared for shipment. We'll notify you when it ships.</p>")
.append(" <p>Thank you for your business!</p>")
.append(" <p><strong>The E-Commerce Team</strong></p>")
.append(" </div>")
.append(" <div class=\"footer\">")
.append(" <p>© 2024 E-Commerce Store. All rights reserved.</p>")
.append(" <p>1234 Commerce Street, Business City, BC 12345</p>")
.append(" </div>")
.append(" </div>")
.append("</body>")
.append("</html>");
return processTemplate(template.toString(), variables);
}
public String generateShippingUpdateEmail(Map<String, Object> variables) {
StringBuilder template = new StringBuilder();
template.append("<!DOCTYPE html>")
.append("<html>")
.append("<head>")
.append(" <meta charset=\"UTF-8\">")
.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
.append(" <title>Shipping Update</title>")
.append(" <style>")
.append(" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; }")
.append(" .container { max-width: 600px; margin: 0 auto; background: #ffffff; padding: 0; }")
.append(" .header { background: #FF9800; color: white; padding: 20px; text-align: center; }")
.append(" .content { padding: 30px; background: white; }")
.append(" .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f9f9f9; }")
.append(" .shipping-info { background: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0; }")
.append(" .button { display: inline-block; padding: 12px 24px; background: #FF9800; color: white; text-decoration: none; border-radius: 4px; margin: 10px 0; }")
.append(" </style>")
.append("</head>")
.append("<body>")
.append(" <div class=\"container\">")
.append(" <div class=\"header\">")
.append(" <h1>Shipping Update</h1>")
.append(" </div>")
.append(" <div class=\"content\">")
.append(" <h2>Your order is on the way!</h2>")
.append(" <p>Dear {{customerName}},</p>")
.append(" <p>Great news! Your order <strong>{{orderNumber}}</strong> has been shipped.</p>")
.append(" <div class=\"shipping-info\">")
.append(" <h3>Shipping Information</h3>")
.append(" <p><strong>Tracking Number:</strong> {{trackingNumber}}</p>")
.append(" <p><strong>Carrier:</strong> {{carrier}}</p>")
.append(" <p><strong>Estimated Delivery:</strong> {{estimatedDelivery}}</p>")
.append(" <p><strong>Shipping Address:</strong><br>{{shippingAddress}}</p>")
.append(" </div>")
.append(" <p>You can track your package using the button below:</p>")
.append(" <a href=\"{{trackingUrl}}\" class=\"button\">Track Package</a>")
.append(" <p>If you have any questions about your shipment, please contact our customer service team.</p>")
.append(" <p>Thank you for shopping with us!</p>")
.append(" <p><strong>The E-Commerce Team</strong></p>")
.append(" </div>")
.append(" <div class=\"footer\">")
.append(" <p>© 2024 E-Commerce Store. All rights reserved.</p>")
.append(" <p>1234 Commerce Street, Business City, BC 12345</p>")
.append(" </div>")
.append(" </div>")
.append("</body>")
.append("</html>");
return processTemplate(template.toString(), variables);
}
public String generatePasswordResetEmail(Map<String, Object> variables) {
StringBuilder template = new StringBuilder();
template.append("<!DOCTYPE html>")
.append("<html>")
.append("<head>")
.append(" <meta charset=\"UTF-8\">")
.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
.append(" <title>Password Reset</title>")
.append(" <style>")
.append(" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; }")
.append(" .container { max-width: 600px; margin: 0 auto; background: #ffffff; padding: 0; }")
.append(" .header { background: #F44336; color: white; padding: 20px; text-align: center; }")
.append(" .content { padding: 30px; background: white; }")
.append(" .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f9f9f9; }")
.append(" .reset-button { display: inline-block; padding: 12px 24px; background: #F44336; color: white; text-decoration: none; border-radius: 4px; margin: 10px 0; }")
.append(" .note { background: #fff3cd; padding: 10px; border-radius: 4px; border-left: 4px solid #ffc107; margin: 15px 0; }")
.append(" </style>")
.append("</head>")
.append("<body>")
.append(" <div class=\"container\">")
.append(" <div class=\"header\">")
.append(" <h1>Password Reset Request</h1>")
.append(" </div>")
.append(" <div class=\"content\">")
.append(" <h2>Reset Your Password</h2>")
.append(" <p>Dear {{customerName}},</p>")
.append(" <p>We received a request to reset your password for your E-Commerce account.</p>")
.append(" <p>Click the button below to reset your password:</p>")
.append(" <a href=\"{{resetUrl}}\" class=\"reset-button\">Reset Password</a>")
.append(" <div class=\"note\">")
.append(" <p><strong>Note:</strong> This password reset link will expire in {{expiryTime}}.</p>")
.append(" <p>If you didn't request a password reset, please ignore this email.</p>")
.append(" </div>")
.append(" <p>For security reasons, do not share this link with anyone.</p>")
.append(" <p>If you have any questions, please contact our support team.</p>")
.append(" <p><strong>The E-Commerce Team</strong></p>")
.append(" </div>")
.append(" <div class=\"footer\">")
.append(" <p>© 2024 E-Commerce Store. All rights reserved.</p>")
.append(" <p>1234 Commerce Street, Business City, BC 12345</p>")
.append(" </div>")
.append(" </div>")
.append("</body>")
.append("</html>");
return processTemplate(template.toString(), variables);
}
public String generateWelcomeEmail(Map<String, Object> variables) {
StringBuilder template = new StringBuilder();
template.append("<!DOCTYPE html>")
.append("<html>")
.append("<head>")
.append(" <meta charset=\"UTF-8\">")
.append(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
.append(" <title>Welcome to E-Commerce</title>")
.append(" <style>")
.append(" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; }")
.append(" .container { max-width: 600px; margin: 0 auto; background: #ffffff; padding: 0; }")
.append(" .header { background: #9C27B0; color: white; padding: 20px; text-align: center; }")
.append(" .content { padding: 30px; background: white; }")
.append(" .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; background: #f9f9f9; }")
.append(" .welcome-bonus { background: #e8f5e8; padding: 15px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #4CAF50; }")
.append(" .button { display: inline-block; padding: 12px 24px; background: #9C27B0; color: white; text-decoration: none; border-radius: 4px; margin: 10px 0; }")
.append(" </style>")
.append("</head>")
.append("<body>")
.append(" <div class=\"container\">")
.append(" <div class=\"header\">")
.append(" <h1>Welcome to E-Commerce!</h1>")
.append(" </div>")
.append(" <div class=\"content\">")
.append(" <h2>Hello {{customerName}},</h2>")
.append(" <p>Welcome to our E-Commerce family! We're thrilled to have you on board.</p>")
.append(" <div class=\"welcome-bonus\">")
.append(" <h3>๐ Special Welcome Offer!</h3>")
.append(" <p>As a new member, enjoy <strong>{{discountPercentage}}% OFF</strong> your first order.</p>")
.append(" <p><strong>Use code: {{promoCode}}</strong></p>")
.append(" <p>Offer valid until {{offerExpiry}}</p>")
.append(" </div>")
.append(" <p>Start shopping now and discover our amazing products:</p>")
.append(" <a href=\"{{shopUrl}}\" class=\"button\">Start Shopping</a>")
.append(" <p>With your account, you can:</p>")
.append(" <ul>")
.append(" <li>Track your orders</li>")
.append(" <li>Save your favorite items</li>")
.append(" <li>Get exclusive member discounts</li>")
.append(" <li>Receive early access to sales</li>")
.append(" </ul>")
.append(" <p>If you have any questions, our customer service team is here to help.</p>")
.append(" <p>Happy shopping!</p>")
.append(" <p><strong>The E-Commerce Team</strong></p>")
.append(" </div>")
.append(" <div class=\"footer\">")
.append(" <p>© 2024 E-Commerce Store. All rights reserved.</p>")
.append(" <p>1234 Commerce Street, Business City, BC 12345</p>")
.append(" </div>")
.append(" </div>")
.append("</body>")
.append("</html>");
return processTemplate(template.toString(), variables);
}
}