| Line 31... |
Line 31... |
| 31 |
@Service
|
31 |
@Service
|
| 32 |
public class MailOutboxService {
|
32 |
public class MailOutboxService {
|
| 33 |
|
33 |
|
| 34 |
private static final Logger LOGGER = LogManager.getLogger(MailOutboxService.class);
|
34 |
private static final Logger LOGGER = LogManager.getLogger(MailOutboxService.class);
|
| 35 |
|
35 |
|
| - |
|
36 |
/**
|
| - |
|
37 |
* The two ways this system can send mail. There is no third.
|
| - |
|
38 |
*
|
| - |
|
39 |
* GOOGLE authenticates as sdtech@smartdukaan.com, and every message it sends
|
| - |
|
40 |
* has its From rewritten to that identity by AuthenticatedIdentityMailSender —
|
| - |
|
41 |
* Workspace binds an authenticated session to one identity and refuses any
|
| - |
|
42 |
* other. RELAY is the Workspace relay: it does not authenticate at all and is
|
| 36 |
public static final String SENDER_SENDGRID = "SENDGRID";
|
43 |
* authorised by source IP, so it may legitimately send as noreply@.
|
| - |
|
44 |
*/
|
| 37 |
public static final String SENDER_GOOGLE = "GOOGLE";
|
45 |
public static final String SENDER_GOOGLE = "GOOGLE";
|
| 38 |
public static final String SENDER_RELAY = "RELAY";
|
46 |
public static final String SENDER_RELAY = "RELAY";
|
| 39 |
|
47 |
|
| 40 |
@Value("${prod}")
|
48 |
@Value("${prod}")
|
| 41 |
private boolean prod;
|
49 |
private boolean prod;
|
| Line 65... |
Line 73... |
| 65 |
|
73 |
|
| 66 |
@Autowired
|
74 |
@Autowired
|
| 67 |
@Qualifier("gmailRelaySender")
|
75 |
@Qualifier("gmailRelaySender")
|
| 68 |
private JavaMailSender gmailRelaySender;
|
76 |
private JavaMailSender gmailRelaySender;
|
| 69 |
|
77 |
|
| 70 |
// ---- Default sender (SendGrid) convenience methods ----
|
78 |
// ---- Default sender (Workspace relay) convenience methods ----
|
| 71 |
|
79 |
|
| 72 |
public void queueMail(String[] emailTo, String[] cc, String subject, String body, String source) {
|
80 |
public void queueMail(String[] emailTo, String[] cc, String subject, String body, String source) {
|
| 73 |
queueMail(emailTo, cc, null, subject, body, false, source, SENDER_SENDGRID, (AttachmentData[]) null);
|
81 |
queueMail(emailTo, cc, null, subject, body, false, source, SENDER_RELAY, (AttachmentData[]) null);
|
| 74 |
}
|
82 |
}
|
| 75 |
|
83 |
|
| 76 |
public void queueMail(String emailTo, String[] cc, String subject, String body, String source) {
|
84 |
public void queueMail(String emailTo, String[] cc, String subject, String body, String source) {
|
| 77 |
queueMail(new String[]{emailTo}, cc, null, subject, body, false, source, SENDER_SENDGRID, (AttachmentData[]) null);
|
85 |
queueMail(new String[]{emailTo}, cc, null, subject, body, false, source, SENDER_RELAY, (AttachmentData[]) null);
|
| 78 |
}
|
86 |
}
|
| 79 |
|
87 |
|
| 80 |
public void queueMail(String[] emailTo, String[] cc, String subject, String body, boolean html, String source) {
|
88 |
public void queueMail(String[] emailTo, String[] cc, String subject, String body, boolean html, String source) {
|
| 81 |
queueMail(emailTo, cc, null, subject, body, html, source, SENDER_SENDGRID, (AttachmentData[]) null);
|
89 |
queueMail(emailTo, cc, null, subject, body, html, source, SENDER_RELAY, (AttachmentData[]) null);
|
| 82 |
}
|
90 |
}
|
| 83 |
|
91 |
|
| 84 |
public void queueMail(String[] emailTo, String[] cc, String[] bcc, String subject, String body, boolean html, String source) {
|
92 |
public void queueMail(String[] emailTo, String[] cc, String[] bcc, String subject, String body, boolean html, String source) {
|
| 85 |
queueMail(emailTo, cc, bcc, subject, body, html, source, SENDER_SENDGRID, (AttachmentData[]) null);
|
93 |
queueMail(emailTo, cc, bcc, subject, body, html, source, SENDER_RELAY, (AttachmentData[]) null);
|
| 86 |
}
|
94 |
}
|
| 87 |
|
95 |
|
| 88 |
public void queueMailWithAttachments(String[] emailTo, String[] cc, String subject, String body, String source, Utils.Attachment... attachments) {
|
96 |
public void queueMailWithAttachments(String[] emailTo, String[] cc, String subject, String body, String source, Utils.Attachment... attachments) {
|
| 89 |
queueMailWithAttachments(emailTo, cc, null, subject, body, false, source, SENDER_SENDGRID, attachments);
|
97 |
queueMailWithAttachments(emailTo, cc, null, subject, body, false, source, SENDER_RELAY, attachments);
|
| 90 |
}
|
98 |
}
|
| 91 |
|
99 |
|
| 92 |
public void queueMailWithAttachments(String[] emailTo, String[] cc, String[] bcc, String subject, String body, boolean html, String source, Utils.Attachment... attachments) {
|
100 |
public void queueMailWithAttachments(String[] emailTo, String[] cc, String[] bcc, String subject, String body, boolean html, String source, Utils.Attachment... attachments) {
|
| 93 |
queueMailWithAttachments(emailTo, cc, bcc, subject, body, html, source, SENDER_SENDGRID, attachments);
|
101 |
queueMailWithAttachments(emailTo, cc, bcc, subject, body, html, source, SENDER_RELAY, attachments);
|
| 94 |
}
|
102 |
}
|
| 95 |
|
103 |
|
| 96 |
// ---- Google sender convenience methods ----
|
104 |
// ---- Google sender convenience methods ----
|
| 97 |
|
105 |
|
| 98 |
public void queueMailViaGoogle(String[] emailTo, String[] cc, String subject, String body, String source) {
|
106 |
public void queueMailViaGoogle(String[] emailTo, String[] cc, String subject, String body, String source) {
|
| Line 177... |
Line 185... |
| 177 |
LOGGER.error("Failed to read file attachment: {}", files[i].getName(), e);
|
185 |
LOGGER.error("Failed to read file attachment: {}", files[i].getName(), e);
|
| 178 |
attachmentDataArray[i] = new AttachmentData(files[i].getName(), new byte[0], null);
|
186 |
attachmentDataArray[i] = new AttachmentData(files[i].getName(), new byte[0], null);
|
| 179 |
}
|
187 |
}
|
| 180 |
}
|
188 |
}
|
| 181 |
}
|
189 |
}
|
| 182 |
queueMail(emailTo, cc, bcc, subject, body, false, source, SENDER_SENDGRID, attachmentDataArray);
|
190 |
queueMail(emailTo, cc, bcc, subject, body, false, source, SENDER_RELAY, attachmentDataArray);
|
| 183 |
}
|
191 |
}
|
| 184 |
|
192 |
|
| 185 |
/**
|
193 |
/**
|
| 186 |
* Core method: persists mail + attachments in current transaction, triggers async send after commit.
|
194 |
* Core method: persists mail + attachments in current transaction, triggers async send after commit.
|
| 187 |
*/
|
195 |
*/
|
| Line 332... |
Line 340... |
| 332 |
|
340 |
|
| 333 |
private JavaMailSender resolveSender(String senderType) {
|
341 |
private JavaMailSender resolveSender(String senderType) {
|
| 334 |
if (SENDER_GOOGLE.equals(senderType)) {
|
342 |
if (SENDER_GOOGLE.equals(senderType)) {
|
| 335 |
return googleMailSender;
|
343 |
return googleMailSender;
|
| 336 |
}
|
344 |
}
|
| - |
|
345 |
// Anything else is the relay. Rows written before the SENDGRID label was
|
| 337 |
// SendGrid and Relay both route through Google Workspace Relay
|
346 |
// retired land here too, which is where they were already being sent.
|
| 338 |
return gmailRelaySender;
|
347 |
return gmailRelaySender;
|
| 339 |
}
|
348 |
}
|
| 340 |
|
349 |
|
| 341 |
private byte[] readInputStreamSource(InputStreamSource source) throws IOException {
|
350 |
private byte[] readInputStreamSource(InputStreamSource source) throws IOException {
|
| 342 |
try (InputStream is = source.getInputStream()) {
|
351 |
try (InputStream is = source.getInputStream()) {
|