embulk-output-send_email 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,34 @@
1
1
  package org.embulk.output.send_email;
2
2
 
3
3
 
4
+ import java.io.File;
4
5
  import java.util.*;
5
6
  import java.util.stream.Collectors;
6
7
 
7
8
  import com.fasterxml.jackson.databind.ObjectMapper;
9
+ import org.apache.commons.lang3.StringUtils;
8
10
  import org.embulk.config.*;
9
11
  import org.embulk.spi.*;
12
+ import org.jsoup.Jsoup;
13
+
10
14
  import javax.mail.*;
11
15
  import javax.mail.internet.InternetAddress;
12
16
  import javax.mail.internet.MimeBodyPart;
13
17
  import javax.mail.internet.MimeMessage;
14
18
  import javax.mail.internet.MimeMultipart;
15
- import javax.validation.constraints.Null;
16
-
17
- import static ch.qos.logback.core.joran.action.ActionConst.NULL;
18
19
 
19
20
  public class SendEmailOutputPlugin
20
21
  implements OutputPlugin {
21
22
  public interface PluginTask
22
23
  extends Task {
23
24
 
24
- @Config("file_type")
25
+ @Config("format_type")
25
26
  @ConfigDefault("\"json\"")
26
- public String getFileType();
27
+ public String getFormatType();
27
28
 
28
29
  @Config("to")
29
30
  public List<String> getTO();
30
31
 
31
-
32
32
  @Config("cc")
33
33
  @ConfigDefault("[]")
34
34
  public List<String> getCC();
@@ -41,6 +41,7 @@ public class SendEmailOutputPlugin
41
41
  public String getPassword();
42
42
 
43
43
  @Config("port")
44
+ @ConfigDefault("\"25\"")
44
45
  public String getPort();
45
46
 
46
47
  @Config("subject")
@@ -51,6 +52,7 @@ public class SendEmailOutputPlugin
51
52
  public boolean getAuth();
52
53
 
53
54
  @Config("host")
55
+ @ConfigDefault("\"smtp.gmail.com\"")
54
56
  public String getHost();
55
57
 
56
58
  @Config("protocol")
@@ -62,12 +64,20 @@ public class SendEmailOutputPlugin
62
64
  public int getRow();
63
65
 
64
66
  @Config("username")
65
- @ConfigDefault("")
67
+ @ConfigDefault("\"\"")
66
68
  public String getUserName();
67
69
 
68
- @Config("smtp_enable")
69
- @ConfigDefault("true")
70
- public String getSmtpEnable();
70
+ @Config("template")
71
+ @ConfigDefault("\"\"")
72
+ public String getEmailTemplate();
73
+
74
+ @Config("is_html")
75
+ @ConfigDefault("false")
76
+ public boolean getIsHTML();
77
+
78
+ @Config("enable_starttls")
79
+ @ConfigDefault("\"true\"")
80
+ public String getEnableStarttls();
71
81
  }
72
82
 
73
83
  @Override
@@ -75,6 +85,7 @@ public class SendEmailOutputPlugin
75
85
  Schema schema, int taskCount,
76
86
  OutputPlugin.Control control) {
77
87
  PluginTask task = config.loadConfig(PluginTask.class);
88
+ System.out.println(task);
78
89
  control.run(task.dump());
79
90
  return Exec.newConfigDiff();
80
91
  }
@@ -203,7 +214,7 @@ public class SendEmailOutputPlugin
203
214
  try {
204
215
  Properties properties = new Properties();
205
216
  properties.put("mail.smtp.auth", task.getAuth());
206
- properties.put("mail.smtp.starttls.enable", task.getSmtpEnable());
217
+ properties.put("mail.smtp.starttls.enable", task.getEnableStarttls());
207
218
 
208
219
  properties.put("mail.smtp.ssl.protocols", task.getProtocol());
209
220
  properties.put("mail.smtp.host", task.getHost());
@@ -242,11 +253,18 @@ public class SendEmailOutputPlugin
242
253
  }
243
254
  message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(listStringTo));
244
255
  message.setSubject(task.getSubject());
245
-
246
- if (task.getFileType().equalsIgnoreCase("html")) {
247
- setHtmlContent(message, mapList, schema);
248
- } else if (task.getFileType().equalsIgnoreCase("json")) {
249
- setJsonContent(message, mapList, schema);
256
+ if(task.getEmailTemplate().length()!=0){
257
+ if(task.getIsHTML()) {
258
+ setEmailTemplate(message, mapList, schema, task);
259
+ }else{
260
+ setTextContent(message,mapList,schema,task);
261
+ }
262
+ }else{
263
+ if (task.getFormatType().equalsIgnoreCase("html")) {
264
+ setHtmlContent(message,mapList,schema);
265
+ } else if (task.getFormatType().equalsIgnoreCase("json")) {
266
+ setJsonContent(message, mapList, schema);
267
+ }
250
268
  }
251
269
  return message;
252
270
  } catch (Exception e) {
@@ -255,16 +273,15 @@ public class SendEmailOutputPlugin
255
273
  }
256
274
  }
257
275
 
258
- private static void setHtmlContent(Message message, ArrayList<LinkedHashMap<String, Object>> mapList, Schema schema) {
276
+ private static void setEmailTemplate(Message message, ArrayList<LinkedHashMap<String, Object>> mapList, Schema schema,PluginTask task) {
259
277
 
260
278
  try {
261
279
  MimeMultipart multipart = new MimeMultipart();
262
280
  BodyPart messageBodyPart = new MimeBodyPart();
263
281
  StringBuilder email = new StringBuilder();
282
+ StringBuilder email1= new StringBuilder();
264
283
 
265
- email.append("<!DOCTYPE html><html><body>"
266
- + "<table style='border:1px solid #96D4D4;border-collapse: collapse;width: 100%'>");
267
-
284
+ email.append("<table style='border:1px solid #96D4D4;border-collapse: collapse;width: 100%'>");
268
285
  email.append("<tr>");
269
286
  for (int i = 0; i < schema.size(); i++) {
270
287
  email.append("<th style='border:2px solid black; border-collapse: collapse;border-color: #96D4D4'>");
@@ -283,9 +300,71 @@ public class SendEmailOutputPlugin
283
300
  }
284
301
  email.append("<tr>");
285
302
  }
286
- email.append("</table></body></html>");
303
+ email.append("</table>");
304
+ email.append("<br>");
305
+ String s=null;
306
+ try {
307
+ File sourceFile = new File(task.getEmailTemplate());
308
+ org.jsoup.nodes.Document doc = Jsoup.parse(sourceFile, "UTF-8");
309
+ org.jsoup.nodes.Element elements = doc.body();
310
+ s=StringUtils.replace(String.valueOf(elements),"{{data}}",email.toString());
311
+
312
+ } catch (Exception e) {
313
+ // TODO Auto-generated catch block
314
+ e.printStackTrace();
315
+ }
316
+ email1.append(s);
317
+ messageBodyPart.setContent(email1.toString(), "text/html");
318
+ multipart.addBodyPart(messageBodyPart);
319
+ message.setContent(multipart);
320
+ } catch (Exception e) {
321
+ System.out.println("Not able to send data");
322
+ throw new RuntimeException(e);
323
+ }
287
324
 
288
- messageBodyPart.setContent(email.toString(), "text/html");
325
+ }
326
+ private static void setTextContent(Message message, ArrayList<LinkedHashMap<String, Object>> mapList, Schema schema,PluginTask task) {
327
+
328
+ try {
329
+ MimeMultipart multipart = new MimeMultipart();
330
+ BodyPart messageBodyPart = new MimeBodyPart();
331
+ StringBuilder email = new StringBuilder();
332
+
333
+ StringBuilder email1 = new StringBuilder();
334
+
335
+ email.append("\n");
336
+ email.append("\n");
337
+ for (int i = 0; i < schema.size(); i++) {
338
+ email.append(schema.getColumn(i).getName().split("/")[0]);
339
+ email.append(",");
340
+ }
341
+ email.append("\n");
342
+
343
+ for (int i = 0; i < mapList.size(); i++) {
344
+ Map<String, Object> map = mapList.get(i);
345
+ email.append("\n");
346
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
347
+ email.append(entry.getValue());
348
+ email.append(",");
349
+ }
350
+ email.append("\n");
351
+ }
352
+
353
+ String s=null;
354
+ try {
355
+ File sourceFile = new File(task.getEmailTemplate());
356
+ org.jsoup.nodes.Document doc = Jsoup.parse(sourceFile, "UTF-8");
357
+ String elements = doc.text();
358
+ s=StringUtils.replace(String.valueOf(elements),"{{data}}",email.toString());
359
+
360
+ } catch (Exception e) {
361
+ // TODO Auto-generated catch block
362
+ e.printStackTrace();
363
+ }
364
+ // email1.append("\n");
365
+ email1.append(s);
366
+ email1.append("\n");
367
+ messageBodyPart.setText(email1.toString());
289
368
  multipart.addBodyPart(messageBodyPart);
290
369
  message.setContent(multipart);
291
370
  } catch (Exception e) {
@@ -308,6 +387,55 @@ public class SendEmailOutputPlugin
308
387
 
309
388
  }
310
389
 
390
+ private static void setHtmlContent(Message message, ArrayList<LinkedHashMap<String, Object>> mapList, Schema schema) {
391
+
392
+ try {
393
+ MimeMultipart multipart = new MimeMultipart();
394
+ BodyPart messageBodyPart = new MimeBodyPart();
395
+ StringBuilder email = new StringBuilder();
396
+
397
+ email.append("<!DOCTYPE html><html><body>");
398
+ email.append("<p>Hello Team,</p>");
399
+ email.append("<header>\n" +
400
+ " <h3>This is the daily ETL Report</h3>\n" +
401
+ "</header>");
402
+
403
+ email.append("<table style='border:1px solid #96D4D4;border-collapse: collapse;width: 100%'>");
404
+
405
+ email.append("<tr>");
406
+ for (int i = 0; i < schema.size(); i++) {
407
+ email.append("<th style='border:2px solid black; border-collapse: collapse;border-color: #96D4D4'>");
408
+ email.append(schema.getColumn(i).getName().split("/")[0]);
409
+ email.append("</th>");
410
+ }
411
+ email.append("</tr>");
412
+
413
+ for (int i = 0; i < mapList.size(); i++) {
414
+ Map<String, Object> map = mapList.get(i);
415
+ email.append("<tr >");
416
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
417
+ email.append("<td style='border:1px solid black;border-collapse: collapse;text-align:center;border-color: #96D4D4'>");
418
+ email.append(entry.getValue());
419
+ email.append("</td>");
420
+ }
421
+ email.append("<tr>");
422
+ }
423
+ email.append("</table>");
424
+ email.append("<footer>\n" +
425
+ " <p>Thanks</p>\n" +
426
+ " <p>By ETL process</p>\n" +
427
+ "</footer>");
428
+ email.append("</body></html>");
429
+
430
+ messageBodyPart.setContent(email.toString(), "text/html");
431
+ multipart.addBodyPart(messageBodyPart);
432
+ message.setContent(multipart);
433
+ } catch (Exception e) {
434
+ System.out.println("Not able to send data");
435
+ throw new RuntimeException(e);
436
+ }
437
+
438
+ }
311
439
  @Override
312
440
  public void finish() {
313
441
 
@@ -1,5 +1,5 @@
1
- package org.embulk.output.send_email;
2
-
3
- public class TestSendEmailOutputPlugin
4
- {
5
- }
1
+ package org.embulk.output.send_email;
2
+
3
+ public class TestSendEmailOutputPlugin
4
+ {
5
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-send_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Infoobjects Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-26 00:00:00.000000000 Z
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,7 @@ files:
53
53
  - classpath/activation-1.1.jar
54
54
  - classpath/commons-codec-1.10.jar
55
55
  - classpath/commons-logging-1.2.jar
56
- - classpath/embulk-output-send_email-0.1.2.jar
56
+ - classpath/embulk-output-send_email-0.1.4.jar
57
57
  - classpath/google-api-client-1.26.0.jar
58
58
  - classpath/google-api-client-java6-1.26.0.jar
59
59
  - classpath/google-api-services-gmail-v1-rev83-1.23.0.jar
@@ -66,11 +66,14 @@ files:
66
66
  - classpath/httpcore-4.4.9.jar
67
67
  - classpath/j2objc-annotations-1.1.jar
68
68
  - classpath/jackson-core-2.9.6.jar
69
+ - classpath/jsoup-1.10.3.jar
69
70
  - classpath/jsr305-3.0.2.jar
70
71
  - classpath/mail-1.4.7.jar
71
72
  - config/checkstyle/checkstyle.xml
72
73
  - config/checkstyle/default.xml
73
74
  - example/config.yml
75
+ - example/email.html
76
+ - example/email.txt
74
77
  - gradle/wrapper/gradle-wrapper.jar
75
78
  - gradle/wrapper/gradle-wrapper.properties
76
79
  - gradlew