001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.forms.workflow; 017 018import java.io.IOException; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.logger.AbstractLogEnabled; 024 025import org.ametys.core.ui.mail.StandardMailBodyHelper; 026import org.ametys.core.util.StringUtils; 027import org.ametys.core.util.mail.SendMailHelper; 028import org.ametys.plugins.workflow.EnhancedFunction; 029import org.ametys.plugins.workflow.support.WorkflowHelper.WorkflowVisibility; 030import org.ametys.runtime.i18n.I18nizableText; 031 032import com.opensymphony.module.propertyset.PropertySet; 033import com.opensymphony.workflow.WorkflowException; 034 035import jakarta.mail.MessagingException; 036 037/** 038 * OS workflow function to send mail after an action is triggered. 039 * The author of a form entry is also notified if the receipt is set 040 */ 041public class SendMailFunction extends AbstractLogEnabled implements Component, EnhancedFunction 042{ 043 /** Actually send the email ? */ 044 public static final String SEND_MAIL = "send-request-information-mail"; 045 046 /** Sender of the email */ 047 public static final String SENDER = "sender"; 048 049 /** Recipient of the email */ 050 public static final String RECIPIENT = "recipient"; 051 052 /** Subject of the email */ 053 public static final String SUBJECT = "subject"; 054 055 /** Body of the email */ 056 public static final String BODY = "body"; 057 058 @Override 059 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 060 { 061 // If "send-mail" is set to true or is not present, send the mail. 062 boolean dontSendMail = "false".equals(transientVars.get(SEND_MAIL)); 063 if (dontSendMail) 064 { 065 return; 066 } 067 068 String sender = (String) transientVars.get(SENDER); 069 String recipient = (String) transientVars.get(RECIPIENT); 070 String subject = (String) transientVars.get(SUBJECT); 071 String body = (String) transientVars.get(BODY); 072 073 try 074 { 075 String htmlBody = StandardMailBodyHelper.newHTMLBody() 076 .withTitle(StringUtils.escapeHTML(subject)) 077 .withMessage(StringUtils.escapeHTML(body).replaceAll("\n\r", "<br/>")) 078 .build(); 079 080 SendMailHelper.newMail() 081 .withAsync(true) 082 .withSubject(subject) 083 .withHTMLBody(htmlBody) 084 .withSender(sender) 085 .withRecipient(recipient) 086 .sendMail(); 087 } 088 catch (MessagingException | IOException e) 089 { 090 getLogger().warn("Could not send a workflow notification mail to " + recipient, e); 091 } 092 } 093 094 @Override 095 public FunctionType getFunctionExecType() 096 { 097 return FunctionType.POST; 098 } 099 100 101 public I18nizableText getLabel() 102 { 103 return new I18nizableText("plugin.forms", "PLUGINS_FORMS_SEND_MAIL_FUNCTION_LABEL"); 104 } 105 106 @Override 107 public I18nizableText getFullLabel(Map<String, String> argumentsValues) 108 { 109 return new I18nizableText("plugin.forms", "PLUGINS_FORMS_SEND_MAIL_FUNCTION_DESCRIPTION"); 110 } 111 112 @Override 113 public List<WorkflowVisibility> getVisibilities() 114 { 115 List<WorkflowVisibility> visibilities = EnhancedFunction.super.getVisibilities(); 116 visibilities.add(WorkflowVisibility.USER); 117 return visibilities; 118 } 119}