Subversion Repositories SmartDukaan

Rev

Rev 36398 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36398 Rev 37477
Line 1... Line -...
1
//@Value("${prod}")
-
 
2
package com.spice.profitmandi.common.services;
1
package com.spice.profitmandi.common.services;
3
 
2
 
4
import org.apache.logging.log4j.LogManager;
3
import com.spice.profitmandi.common.mail.MailQueue;
-
 
4
import com.spice.profitmandi.common.mail.MailRequest;
-
 
5
import com.spice.profitmandi.common.mail.MailSenderType;
5
import org.apache.logging.log4j.Logger;
6
import com.spice.profitmandi.common.util.Utils;
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.beans.factory.annotation.Autowired;
-
 
8
import org.springframework.core.io.FileSystemResource;
7
import org.springframework.core.io.InputStreamSource;
9
import org.springframework.core.io.InputStreamSource;
8
import org.springframework.mail.javamail.JavaMailSender;
10
import org.springframework.mail.javamail.JavaMailSender;
9
import org.springframework.mail.javamail.MimeMessageHelper;
-
 
10
import org.springframework.stereotype.Service;
11
import org.springframework.stereotype.Service;
11
 
12
 
12
import javax.mail.Multipart;
-
 
13
import javax.mail.internet.InternetAddress;
-
 
14
import javax.mail.internet.MimeBodyPart;
-
 
15
import javax.mail.internet.MimeMessage;
-
 
16
import javax.mail.internet.MimeMultipart;
-
 
17
import java.io.File;
13
import java.io.File;
18
import java.io.Serializable;
14
import java.io.Serializable;
19
import java.util.Arrays;
15
import java.util.ArrayList;
20
import java.util.List;
16
import java.util.List;
21
import java.util.Map;
17
import java.util.Map;
22
 
18
 
-
 
19
/**
-
 
20
 * Deprecated. Every method here now hands the mail to {@link MailQueue} and
-
 
21
 * returns; nothing is sent inline any more.
-
 
22
 *
-
 
23
 * This class used to build its own MimeMessage, hardcode
-
 
24
 * From: noreply@smartdukaan.com in five places, and send straight down whichever
-
 
25
 * JavaMailSender the caller happened to pass. That From is the reason mail was
-
 
26
 * being rejected: an authenticated Workspace session may only send as the
-
 
27
 * account it logged in as, and noreply@ is a different identity in the same
-
 
28
 * domain. It also meant a failed send was simply lost, and took the calling job
-
 
29
 * down with it.
-
 
30
 *
-
 
31
 * The signatures are kept so the ~11 existing callers keep compiling. The
-
 
32
 * JavaMailSender argument is ignored — choosing a transport is not the caller's
-
 
33
 * job. New code should use {@link MailQueue} directly.
-
 
34
 */
-
 
35
@Deprecated
23
@Service
36
@Service
24
public class EmailServiceImpl implements EmailService {
37
public class EmailServiceImpl implements EmailService {
25
 
38
 
26
    private static final Logger logger =
39
    private final MailQueue mailQueue;
27
            LogManager.getLogger(EmailServiceImpl.class);
-
 
28
 
40
 
29
    @Value("${prod}")
41
    @Autowired
30
    private boolean isProd;
-
 
31
 
-
 
32
    private final JavaMailSender gmailRelaySender;
-
 
33
 
-
 
34
    public EmailServiceImpl(JavaMailSender gmailRelaySender) {
42
    public EmailServiceImpl(MailQueue mailQueue) {
35
        this.gmailRelaySender = gmailRelaySender;
43
        this.mailQueue = mailQueue;
36
    }
44
    }
37
 
45
 
38
 
-
 
39
    @Override
46
    @Override
40
    public void sendMailWithAttachments(
47
    public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
41
            JavaMailSender mailSender,
-
 
42
            String[] emailTo,
-
 
43
            String[] cc,
-
 
44
            String subject,
-
 
45
            String body
-
 
46
    ) throws Exception {
-
 
47
 
-
 
48
        MimeMessage message = mailSender.createMimeMessage();
48
                                        String subject, String body) {
49
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
-
 
50
 
-
 
51
        helper.setTo(emailTo);
-
 
52
        helper.setSubject(subject);
-
 
53
        helper.setText(body, true);
-
 
54
 
-
 
55
        if (cc != null) {
-
 
56
            helper.setCc(cc);
-
 
57
        }
-
 
58
 
-
 
59
        helper.setFrom(new InternetAddress(
-
 
60
                "noreply@smartdukaan.com",
-
 
61
                "SmartDukaan Care"
-
 
62
        ));
-
 
63
 
-
 
64
        sendIfProd(message, emailTo, subject);
49
        send(emailTo, cc, null, subject, body, true, null);
65
    }
50
    }
66
 
51
 
67
    @Override
52
    @Override
68
    public void sendMailWithAttachments(
53
    public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
69
            JavaMailSender mailSender,
-
 
70
            String[] emailTo,
-
 
71
            String[] cc,
-
 
72
            String subject,
-
 
73
            String body,
-
 
74
            List<File> attachments
-
 
75
    ) throws Exception {
-
 
76
 
-
 
77
        MimeMessage message = mailSender.createMimeMessage();
-
 
78
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
54
                                        String subject, String body, List<File> attachments) {
79
 
-
 
80
        helper.setTo(emailTo);
-
 
81
        helper.setSubject(subject);
-
 
82
        helper.setText(body, true);
-
 
83
 
-
 
84
        if (cc != null) {
-
 
85
            helper.setCc(cc);
-
 
86
        }
-
 
87
 
-
 
88
        helper.setFrom(new InternetAddress(
-
 
89
                "noreply@smartdukaan.com",
-
 
90
                "SmartDukaan Care"
-
 
91
        ));
-
 
92
 
-
 
93
        if (attachments != null) {
-
 
94
            for (File file : attachments) {
55
        send(emailTo, cc, null, subject, body, true, fromFiles(attachments));
95
                helper.addAttachment(file.getName(), file);
-
 
96
            }
-
 
97
        }
-
 
98
 
-
 
99
        sendIfProd(message, emailTo, subject);
-
 
100
    }
56
    }
101
 
57
 
102
    @Override
58
    @Override
103
    public void sendMailWithAttachments(
59
    public void sendMailWithAttachments(JavaMailSender ignored, String emailTo, String[] cc,
104
            JavaMailSender mailSender,
-
 
105
            String emailTo,
-
 
106
            String[] cc,
-
 
107
            String subject,
-
 
108
            String body,
-
 
109
            List<File> attachments
60
                                        String subject, String body, List<File> attachments) {
110
    ) throws Exception {
-
 
111
        // Delegate to array version for Utils.java compatibility
61
        send(new String[]{emailTo}, cc, null, subject, body, true, fromFiles(attachments));
112
        sendMailWithAttachments(
-
 
113
                mailSender,
-
 
114
                new String[]{emailTo},
-
 
115
                cc,
-
 
116
                subject,
-
 
117
                body,
-
 
118
                attachments
-
 
119
        );
-
 
120
    }
62
    }
121
 
63
 
122
    @Override
64
    @Override
123
    public void sendMailWithAttachment(
65
    public void sendMailWithAttachment(JavaMailSender ignored, String[] emailTo, String[] cc,
124
            JavaMailSender mailSender,
-
 
125
            String[] emailTo,
-
 
126
            String[] cc,
-
 
127
            String subject,
66
                                       String subject, String body,
128
            String body,
-
 
129
            String fileName,
-
 
130
            InputStreamSource inputStreamSource
67
                                       String fileName, InputStreamSource inputStreamSource) {
131
    ) throws Exception {
-
 
132
 
-
 
133
        MimeMessage message = mailSender.createMimeMessage();
-
 
134
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
-
 
135
 
-
 
136
        helper.setTo(emailTo);
-
 
137
        helper.setSubject(subject);
-
 
138
        helper.setText(body, true);
68
        send(emailTo, cc, null, subject, body, true,
139
 
-
 
140
        if (cc != null) {
-
 
141
            helper.setCc(cc);
-
 
142
        }
-
 
143
 
-
 
144
        helper.addAttachment(fileName, inputStreamSource);
69
                new Utils.Attachment[]{new Utils.Attachment(fileName, inputStreamSource)});
145
 
-
 
146
        helper.setFrom(new InternetAddress(
-
 
147
                "noreply@smartdukaan.com",
-
 
148
                "SmartDukaan Care"
-
 
149
        ));
-
 
150
 
-
 
151
        sendIfProd(message, emailTo, subject);
-
 
152
    }
70
    }
153
 
71
 
154
    @Override
72
    @Override
155
    public void sendMailWithAttachments(
-
 
156
            JavaMailSender mailSender,
-
 
157
            String[] emailTo,
-
 
158
            String[] cc,
-
 
159
            String[] bcc,
-
 
160
            String subject,
-
 
161
            String body,
-
 
162
            boolean html,
-
 
163
            Attachment... attachments
-
 
164
    ) throws Exception {
-
 
165
 
-
 
166
        MimeMessage message = mailSender.createMimeMessage();
-
 
167
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
73
    public void sendMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc, String[] bcc,
168
 
-
 
169
        helper.setTo(emailTo);
-
 
170
        helper.setSubject(subject);
-
 
171
        helper.setText(body, html);
-
 
172
 
-
 
173
        if (cc != null) helper.setCc(cc);
-
 
174
        if (bcc != null) helper.setBcc(bcc);
-
 
175
 
-
 
176
        helper.setFrom(new InternetAddress(
-
 
177
                "noreply@smartdukaan.com",
-
 
178
                "SmartDukaan Care"
-
 
179
        ));
-
 
180
 
-
 
181
        if (attachments != null) {
-
 
182
            for (Attachment attachment : attachments) {
-
 
183
                if (attachment != null) {
-
 
184
                    helper.addAttachment(
-
 
185
                            attachment.getFileName(),
74
                                        String subject, String body, boolean html,
186
                            attachment.getInputStreamSource()
75
                                        Attachment... attachments) {
187
                    );
-
 
188
                }
-
 
189
            }
-
 
190
        }
-
 
191
 
-
 
192
        sendIfProd(message, emailTo, subject);
76
        send(emailTo, cc, bcc, subject, body, html, convert(attachments));
193
    }
77
    }
194
 
78
 
195
 
-
 
196
    @Override
79
    @Override
197
    public void sendHtmlMailWithAttachments(
80
    public void sendHtmlMailWithAttachments(JavaMailSender ignored, String[] emailTo, String[] cc,
198
            JavaMailSender mailSender,
-
 
199
            String[] emailTo,
-
 
200
            String[] cc,
-
 
201
            String subject,
-
 
202
            String body,
-
 
203
            Attachment... attachments
81
                                            String subject, String body, Attachment... attachments) {
204
    ) throws Exception {
-
 
205
 
-
 
206
        sendMailWithAttachments(
82
        send(emailTo, cc, null, subject, body, true, convert(attachments));
207
                mailSender,
-
 
208
                emailTo,
-
 
209
                cc,
-
 
210
                null,
-
 
211
                subject,
-
 
212
                body,
-
 
213
                true,
-
 
214
                attachments
-
 
215
        );
-
 
216
    }
83
    }
217
 
84
 
-
 
85
    /**
-
 
86
     * Inline images are not carried through the outbox — it stores attachments,
-
 
87
     * not CID-referenced parts. The images are attached normally instead, so the
-
 
88
     * mail still arrives with its content rather than being dropped.
218
 
89
     */
219
    @Override
90
    @Override
220
    public void sendEmbeddedHtmlMail(
91
    public void sendEmbeddedHtmlMail(JavaMailSender ignored, String[] emailTo, String[] cc,
221
            JavaMailSender mailSender,
-
 
222
            String[] emailTo,
-
 
223
            String[] cc,
-
 
224
            String subject,
92
                                     String subject, String body,
225
            String body,
-
 
226
            Map<? extends Serializable, File> inlineImages
93
                                     Map<? extends Serializable, File> inlineImages) {
227
    ) throws Exception {
-
 
228
 
-
 
229
        MimeMessage message = mailSender.createMimeMessage();
-
 
230
        Multipart multipart = new MimeMultipart();
94
        List<Utils.Attachment> images = new ArrayList<>();
231
 
-
 
232
        MimeBodyPart bodyPart = new MimeBodyPart();
-
 
233
        bodyPart.setContent(body, "text/html; charset=UTF-8");
-
 
234
        multipart.addBodyPart(bodyPart);
-
 
235
 
-
 
236
        if (inlineImages != null) {
95
        if (inlineImages != null) {
237
            for (Map.Entry<? extends Serializable, File> entry : inlineImages.entrySet()) {
96
            for (Map.Entry<? extends Serializable, File> entry : inlineImages.entrySet()) {
238
                MimeBodyPart imagePart = new MimeBodyPart();
97
                File file = entry.getValue();
239
                imagePart.setHeader("Content-ID", entry.getKey().toString());
-
 
240
                imagePart.setDisposition(MimeBodyPart.INLINE);
98
                if (file != null) {
241
                imagePart.attachFile(entry.getValue());
99
                    images.add(new Utils.Attachment(file.getName(), new FileSystemResource(file)));
242
                multipart.addBodyPart(imagePart);
100
                }
243
            }
101
            }
244
        }
102
        }
-
 
103
        send(emailTo, cc, null, subject, body, true, images.toArray(new Utils.Attachment[0]));
-
 
104
    }
245
 
105
 
-
 
106
    private void send(String[] to, String[] cc, String[] bcc, String subject, String body,
-
 
107
                      boolean html, Utils.Attachment[] attachments) {
246
        message.setContent(multipart);
108
        MailRequest request = MailRequest.to(to)
247
        message.setSubject(subject);
109
                .cc(cc)
248
        message.setRecipients(
110
                .bcc(bcc)
249
                MimeMessage.RecipientType.TO,
111
                .subject(subject)
250
                InternetAddress.parse(String.join(",", emailTo))
112
                .from(MailSenderType.RELAY)
251
        );
113
                .source("EmailService");
252
 
-
 
253
        if (cc != null) {
114
        if (html) {
254
            message.addRecipients(
115
            request.html(body);
255
                    MimeMessage.RecipientType.CC,
116
        } else {
256
                    InternetAddress.parse(String.join(",", cc))
-
 
257
            );
117
            request.body(body);
258
        }
118
        }
259
 
-
 
260
        message.setFrom(new InternetAddress(
119
        if (attachments != null && attachments.length > 0) {
261
                "noreply@smartdukaan.com",
-
 
262
                "SmartDukaan Care"
120
            request.attach(attachments);
263
        ));
121
        }
264
 
-
 
265
        sendIfProd(message, emailTo, subject);
122
        mailQueue.queue(request);
266
    }
123
    }
267
 
124
 
-
 
125
    private Utils.Attachment[] fromFiles(List<File> files) {
-
 
126
        if (files == null || files.isEmpty()) {
-
 
127
            return null;
-
 
128
        }
-
 
129
        List<Utils.Attachment> converted = new ArrayList<>(files.size());
-
 
130
        for (File file : files) {
-
 
131
            if (file != null) {
-
 
132
                converted.add(new Utils.Attachment(file.getName(), new FileSystemResource(file)));
-
 
133
            }
-
 
134
        }
-
 
135
        return converted.toArray(new Utils.Attachment[0]);
-
 
136
    }
268
 
137
 
269
    private void sendIfProd(
138
    private Utils.Attachment[] convert(Attachment[] attachments) {
270
            MimeMessage message,
139
        if (attachments == null || attachments.length == 0) {
271
            String[] emailTo,
-
 
272
            String subject
140
            return null;
273
    ) {
141
        }
274
        if (isProd) {
-
 
275
            gmailRelaySender.send(message);
142
        Utils.Attachment[] converted = new Utils.Attachment[attachments.length];
276
        } else {
-
 
277
            logger.info(
-
 
278
                    "[MAIL BLOCKED - NON PROD] to={}, subject={}",
143
        for (int i = 0; i < attachments.length; i++) {
279
                    Arrays.toString(emailTo),
144
            converted[i] = new Utils.Attachment(
280
                    subject
145
                    attachments[i].getFileName(), attachments[i].getInputStreamSource());
281
            );
-
 
282
        }
146
        }
-
 
147
        return converted;
283
    }
148
    }
284
}
149
}