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.util.mail.SendMailHelper;
026import org.ametys.plugins.workflow.EnhancedFunction;
027import org.ametys.runtime.i18n.I18nizableText;
028
029import com.opensymphony.module.propertyset.PropertySet;
030import com.opensymphony.workflow.WorkflowException;
031
032import jakarta.mail.MessagingException;
033
034/**
035 * OS workflow function to send mail after an action is triggered.
036 * The author of a form entry is also notified if the receipt is set
037 */
038public class SendMailFunction extends AbstractLogEnabled implements Component, EnhancedFunction
039{
040    /** Actually send the email ? */
041    public static final String SEND_MAIL = "send-request-information-mail";
042    
043    /** Sender of the email */
044    public static final String SENDER = "sender";
045    
046    /** Recipient of the email */
047    public static final String RECIPIENT = "recipient";
048
049    /** Subject of the email */
050    public static final String SUBJECT = "subject";
051    
052    /** Body of the email */
053    public static final String BODY = "body";
054    
055    @Override
056    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
057    {
058        // If "send-mail" is set to true or is not present, send the mail.
059        boolean dontSendMail = "false".equals(transientVars.get(SEND_MAIL));
060        if (dontSendMail)
061        {
062            return;
063        }
064        
065        String sender = (String) transientVars.get(SENDER);
066        String recipient = (String) transientVars.get(RECIPIENT);
067        String subject = (String) transientVars.get(SUBJECT);
068        String body = (String) transientVars.get(BODY);
069        
070        try
071        {
072            SendMailHelper.newMail()
073                          .withAsync(true)
074                          .withSubject(subject)
075                          .withTextBody(body)
076                          .withSender(sender)
077                          .withRecipient(recipient)
078                          .sendMail();
079        }
080        catch (MessagingException | IOException e)
081        {
082            getLogger().warn("Could not send a workflow notification mail to " + recipient, e);
083        }
084    }
085
086    @Override
087    public List<FunctionArgument> getArguments()
088    {
089        return List.of();
090    }
091
092    @Override
093    public I18nizableText getDescription(Map<String, String> argumentsValues)
094    {
095        return new I18nizableText("plugin.forms", "PLUGINS_FORMS_SEND_MAIL_FUNCTION_DESCRIPTION");
096    }
097}