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.Map;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023
024import org.ametys.core.util.mail.SendMailHelper;
025
026import com.opensymphony.module.propertyset.PropertySet;
027import com.opensymphony.workflow.FunctionProvider;
028import com.opensymphony.workflow.WorkflowException;
029
030import jakarta.mail.MessagingException;
031
032/**
033 * OS workflow function to send mail after an action is triggered.
034 * The author of a form entry is also notified if the receipt is set
035 */
036public class SendMailFunction extends AbstractLogEnabled implements Component, FunctionProvider
037{
038    /** Actually send the email ? */
039    public static final String SEND_MAIL = "send-request-information-mail";
040    
041    /** Body of the email */
042    public static final String SENDER = "sender";
043    
044    /** Body of the email */
045    public static final String RECIPIENT = "recipient";
046
047    /** Subject of the email */
048    public static final String SUBJECT = "subject";
049    
050    /** Body of the email */
051    public static final String BODY = "body";
052    
053    @Override
054    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
055    {
056        // If "send-mail" is set to true or is not present, send the mail.
057        boolean dontSendMail = "false".equals(transientVars.get(SEND_MAIL));
058        if (dontSendMail)
059        {
060            return;
061        }
062        
063        String sender = (String) transientVars.get(SENDER);
064        String recipient = (String) transientVars.get(RECIPIENT);
065        String subject = (String) transientVars.get(SUBJECT);
066        String body = (String) transientVars.get(BODY);
067        
068        try
069        {
070            SendMailHelper.newMail()
071                          .withSubject(subject)
072                          .withTextBody(body)
073                          .withSender(sender)
074                          .withRecipient(recipient)
075                          .sendMail();
076        }
077        catch (MessagingException | IOException e)
078        {
079            getLogger().warn("Could not send a workflow notification mail to " + recipient, e);
080        }
081    }
082}