19 package org.turro.mail.recipients;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.function.Consumer;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.Strings;
29 import org.apache.commons.mail.EmailException;
30 import org.turro.action.Contacts;
31 import org.turro.action.MailAttachment;
32 import org.turro.action.MailSenders;
33 import org.turro.command.Command;
34 import org.turro.command.Context;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.security.IUser;
37 import org.turro.elephant.util.Messages;
38 import org.turro.i18n.I_;
39 import org.turro.log.SystemLogType;
40 import org.turro.log.SystemLogger;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.util.PhraseBuilder;
50 private final ArrayList<MailAttachment> attachments =
new ArrayList<>();
52 private final String title;
63 if(attachments.isEmpty()) {
68 public Object execute(Context context) {
71 if(contacts !=
null) {
72 String mailString = getMailString(contacts);
73 if(!Strings.isBlank(mailString)) {
76 ma.attachment = File.createTempFile(
"attach_",
"_mail");
78 }
catch (IOException ex) {
83 for(MailAttachment ma : attachments) {
84 ma.attachment.delete();
85 SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, ma.path,
"sent", mailString);
97 private String getMailString(Collection<IContact> iParticipantsEmail) {
98 PhraseBuilder pb =
new PhraseBuilder();
99 for(
IContact c : iParticipantsEmail) {
102 pb.addWord(c.getName() +
": " + email);
103 pb.addPendingSeparator(
",");
106 return pb.toString();
109 private void sendAttachments(Collection<IContact> contacts, ArrayList<MailAttachment> attachments,
110 String comment, String mailString, Consumer command) {
112 MailSenders.getPool()
113 .addContacts(contacts)
114 .addMailAttachments(attachments)
116 .setRoot(
"/attachments")
117 .put(
"comment", comment)
118 .put(
"mas", attachments)
119 .sendTemplate(
"send", ElephantContext.getSiteName() +
": " + title);
120 Messages.info(I_.get(
"Send documents")).add(I_.get(
"Sent")).paragraph().add(mailString.replaceAll(
"\\,",
"\n")).show();
121 }
catch (EmailException ex) {
122 Logger.getLogger(SendAttachments.class.getName()).log(Level.SEVERE,
null, ex);
static String logMsg(String msg)
static String getComment()
static void selectRecipients(Collection< IContact > recipients, Command command)
Collection< IContact > getRecipients()
abstract void fillAttachment(File attachment, Object entity)
void addAttachment(Object entity, String path, String name)
void sendAttachments(Collection< IContact > contacts)
SendAttachments(String title)
static final String CONNECTOR_EMAIL