| Line 6... |
Line 6... |
| 6 |
import com.spice.profitmandi.dao.repository.mail.MailOutboxRepository;
|
6 |
import com.spice.profitmandi.dao.repository.mail.MailOutboxRepository;
|
| 7 |
import org.apache.logging.log4j.LogManager;
|
7 |
import org.apache.logging.log4j.LogManager;
|
| 8 |
import org.apache.logging.log4j.Logger;
|
8 |
import org.apache.logging.log4j.Logger;
|
| 9 |
import org.springframework.beans.factory.annotation.Autowired;
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 10 |
import org.springframework.beans.factory.annotation.Qualifier;
|
10 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| - |
|
11 |
import org.springframework.beans.factory.annotation.Value;
|
| - |
|
12 |
import org.springframework.context.annotation.Lazy;
|
| 11 |
import org.springframework.core.io.ByteArrayResource;
|
13 |
import org.springframework.core.io.ByteArrayResource;
|
| 12 |
import org.springframework.core.io.InputStreamSource;
|
14 |
import org.springframework.core.io.InputStreamSource;
|
| 13 |
import org.springframework.mail.javamail.JavaMailSender;
|
15 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 14 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
16 |
import org.springframework.mail.javamail.MimeMessageHelper;
|
| 15 |
import org.springframework.scheduling.annotation.Async;
|
17 |
import org.springframework.scheduling.annotation.Async;
|
| Line 34... |
Line 36... |
| 34 |
private static final Logger LOGGER = LogManager.getLogger(MailOutboxService.class);
|
36 |
private static final Logger LOGGER = LogManager.getLogger(MailOutboxService.class);
|
| 35 |
|
37 |
|
| 36 |
public static final String SENDER_SENDGRID = "SENDGRID";
|
38 |
public static final String SENDER_SENDGRID = "SENDGRID";
|
| 37 |
public static final String SENDER_GOOGLE = "GOOGLE";
|
39 |
public static final String SENDER_GOOGLE = "GOOGLE";
|
| 38 |
|
40 |
|
| - |
|
41 |
@Value("${prod}")
|
| - |
|
42 |
private boolean prod;
|
| - |
|
43 |
|
| - |
|
44 |
/**
|
| - |
|
45 |
* In dev/staging, mails are sent to this address instead of actual recipients.
|
| - |
|
46 |
* Set to empty to throw an exception (forces developer to set their email).
|
| - |
|
47 |
*/
|
| - |
|
48 |
@Value("${mail.outbox.dev.recipient:}")
|
| - |
|
49 |
private String devRecipient;
|
| - |
|
50 |
|
| 39 |
@Autowired
|
51 |
@Autowired
|
| 40 |
private MailOutboxRepository mailOutboxRepository;
|
52 |
private MailOutboxRepository mailOutboxRepository;
|
| 41 |
|
53 |
|
| 42 |
@Autowired
|
54 |
@Autowired
|
| - |
|
55 |
@Lazy
|
| - |
|
56 |
private MailOutboxService self;
|
| - |
|
57 |
|
| - |
|
58 |
@Autowired
|
| 43 |
@Qualifier("mailSender")
|
59 |
@Qualifier("mailSender")
|
| 44 |
private JavaMailSender sendgridMailSender;
|
60 |
private JavaMailSender sendgridMailSender;
|
| 45 |
|
61 |
|
| 46 |
@Autowired
|
62 |
@Autowired
|
| 47 |
@Qualifier("googleMailSender")
|
63 |
@Qualifier("googleMailSender")
|
| Line 174... |
Line 190... |
| 174 |
|
190 |
|
| 175 |
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
191 |
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
| 176 |
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
192 |
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
| 177 |
@Override
|
193 |
@Override
|
| 178 |
public void afterCommit() {
|
194 |
public void afterCommit() {
|
| 179 |
sendMailAsync(mail.getId());
|
195 |
self.sendMailAsync(mail.getId());
|
| 180 |
}
|
196 |
}
|
| 181 |
});
|
197 |
});
|
| 182 |
} else {
|
198 |
} else {
|
| 183 |
// No active transaction — send immediately async
|
199 |
// No active transaction — send immediately async
|
| 184 |
sendMailAsync(mail.getId());
|
200 |
self.sendMailAsync(mail.getId());
|
| 185 |
}
|
201 |
}
|
| 186 |
}
|
202 |
}
|
| 187 |
|
203 |
|
| 188 |
@Async("mailOutboxExecutor")
|
204 |
@Async("mailOutboxExecutor")
|
| 189 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
205 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
| Line 234... |
Line 250... |
| 234 |
mail.setErrorMessage(errorMsg);
|
250 |
mail.setErrorMessage(errorMsg);
|
| 235 |
}
|
251 |
}
|
| 236 |
}
|
252 |
}
|
| 237 |
|
253 |
|
| 238 |
private void sendSmtp(MailOutbox mail, List<MailOutboxAttachment> attachments) throws Exception {
|
254 |
private void sendSmtp(MailOutbox mail, List<MailOutboxAttachment> attachments) throws Exception {
|
| - |
|
255 |
if (!prod) {
|
| - |
|
256 |
if (devRecipient == null || devRecipient.trim().isEmpty()) {
|
| - |
|
257 |
throw new IllegalStateException("Non-prod environment: set mail.outbox.dev.recipient in properties to your email before sending mails");
|
| - |
|
258 |
}
|
| - |
|
259 |
LOGGER.info("Non-prod: redirecting mail [subject={}] from [{}] to dev recipient [{}]", mail.getSubject(), mail.getEmailTo(), devRecipient);
|
| - |
|
260 |
}
|
| - |
|
261 |
|
| 239 |
JavaMailSender sender = resolveSender(mail.getSenderType());
|
262 |
JavaMailSender sender = resolveSender(mail.getSenderType());
|
| 240 |
MimeMessage message = sender.createMimeMessage();
|
263 |
MimeMessage message = sender.createMimeMessage();
|
| 241 |
boolean hasAttachments = attachments != null && !attachments.isEmpty();
|
264 |
boolean hasAttachments = attachments != null && !attachments.isEmpty();
|
| 242 |
MimeMessageHelper helper = new MimeMessageHelper(message, hasAttachments);
|
265 |
MimeMessageHelper helper = new MimeMessageHelper(message, hasAttachments);
|
| 243 |
|
266 |
|
| - |
|
267 |
if (prod) {
|
| 244 |
helper.setTo(mail.getEmailTo().split(","));
|
268 |
helper.setTo(mail.getEmailTo().split(","));
|
| 245 |
if (mail.getEmailCc() != null && !mail.getEmailCc().isEmpty()) {
|
269 |
if (mail.getEmailCc() != null && !mail.getEmailCc().isEmpty()) {
|
| 246 |
helper.setCc(mail.getEmailCc().split(","));
|
270 |
helper.setCc(mail.getEmailCc().split(","));
|
| 247 |
}
|
271 |
}
|
| 248 |
if (mail.getEmailBcc() != null && !mail.getEmailBcc().isEmpty()) {
|
272 |
if (mail.getEmailBcc() != null && !mail.getEmailBcc().isEmpty()) {
|
| 249 |
helper.setBcc(mail.getEmailBcc().split(","));
|
273 |
helper.setBcc(mail.getEmailBcc().split(","));
|
| - |
|
274 |
}
|
| - |
|
275 |
} else {
|
| - |
|
276 |
helper.setTo(devRecipient.trim());
|
| 250 |
}
|
277 |
}
|
| 251 |
helper.setSubject(mail.getSubject());
|
278 |
helper.setSubject(mail.getSubject());
|
| 252 |
helper.setText(mail.getBody(), mail.isHtml());
|
279 |
helper.setText(mail.getBody(), mail.isHtml());
|
| 253 |
|
280 |
|
| 254 |
String fromEmail = SENDER_GOOGLE.equals(mail.getSenderType()) ? "sdtech@smartdukaan.com" : "noreply@smartdukaan.com";
|
281 |
String fromEmail = SENDER_GOOGLE.equals(mail.getSenderType()) ? "sdtech@smartdukaan.com" : "noreply@smartdukaan.com";
|