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.core.impl.schedule; 017 018import org.quartz.JobDataMap; 019import org.quartz.JobExecutionContext; 020 021import org.ametys.core.schedule.Schedulable; 022import org.ametys.core.util.mail.SendMailHelper; 023import org.ametys.plugins.core.schedule.Scheduler; 024 025/** 026 * A {@link Schedulable} job for sending emails. 027 */ 028public class SendMailSchedulable extends AbstractStaticSchedulable 029{ 030 /** The key for the sender of the email */ 031 public static final String SENDER_KEY = "sender"; 032 /** The key for the recipients of the email */ 033 public static final String RECIPIENTS_KEY = "recipients"; 034 /** The key for the subject of the email */ 035 public static final String SUBJECT_KEY = "subject"; 036 /** The key for the body of the email */ 037 public static final String BODY_KEY = "body"; 038 /** The key for the HTML state of the body of the email */ 039 public static final String IS_HTML_BODY_KEY = "html"; 040 041 @Override 042 public void execute(JobExecutionContext context) throws Exception 043 { 044 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 045 String sender = (String) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + SENDER_KEY); 046 String recipients = (String) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + RECIPIENTS_KEY); 047 String subject = (String) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + SUBJECT_KEY); 048 String body = (String) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + BODY_KEY); 049 boolean isHtml = jobDataMap.getBoolean(Scheduler.PARAM_VALUES_PREFIX + IS_HTML_BODY_KEY); 050 051 StringBuilder builder = new StringBuilder(); // the builder for the addresses separated by a space 052 for (String recipient : recipients.split("\\n")) 053 { 054 if (builder.length() != 0) 055 { 056 builder.append(" "); 057 } 058 builder.append(recipient.trim()); 059 } 060 061 if (isHtml) 062 { 063 SendMailHelper.sendMail(subject, body, null, builder.toString(), sender); 064 } 065 else 066 { 067 SendMailHelper.sendMail(subject, null, body, builder.toString(), sender); 068 } 069 } 070}