package cz.muni.fi.rtc.teacherWorkbench.importer;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import cz.muni.fi.rtc.teacherWorkbench.importer.exceptions.ConfigurationException;
import cz.muni.fi.rtc.teacherWorkbench.importer.exceptions.ImporterException;
import cz.muni.fi.rtc.teacherWorkbench.model.ImportedPerson;
/**
* Mass mailer.
* @author Jan Stastny
*
*/
public class Mailer {
private ImporterConfiguration configuration;
private Log log = LogFactory
.getLog(cz.muni.fi.rtc.teacherWorkbench.importer.Mailer.class);
/**
* Constructor
* @param conf Configuration
*/
public Mailer(ImporterConfiguration conf) {
configuration = conf;
}
/**
* Sends email to all receivers.
* Body and subject can contain special strings which will get substituted
* for the current values. These are
*
* - %%UID%% -- User's UID
*
- %%FIRST%% -- User's first name
*
- %%LAST%% -- User's last name
*
- %%EMAIL%% -- User's email address
*
- %%PASS%% -- Password
*
- %%RTCADDRESS%% -- RTC server address. Taken from configuration. For
* example https://localhost:9443/jazz
*
*
* @param subject
* Email subject. Can contain special characters
* @param body
* Email body. Can contain special characters
* @throws ImporterException
* Wraps other exceptions. Thrown when arguments are not valid
*/
public void sendEmailAnnouncement(String subject, String body,
Collection receivers) throws ImporterException {
if (configuration.getRtcAddress() == null
|| configuration.getRtcAddress().equals("")) {
throw new ConfigurationException("RtcAddress not configured");
}
if (configuration.getSmtpHost() == null
|| configuration.getSmtpHost().equals("")) {
throw new ConfigurationException("SMTP host not configured");
}
if (configuration.getEmailFromAddress() == null
|| configuration.getEmailFromAddress().equals("")) {
throw new ConfigurationException("EmailFromAddress not configured");
}
if (configuration.getEmailFromName() == null
|| configuration.getEmailFromName().equals("")) {
throw new ConfigurationException("EmailFromName not configured");
}
if (subject == null) {
throw new ImporterException(new IllegalArgumentException("subject"));
}
if (body == null) {
throw new ImporterException(new IllegalArgumentException("body"));
}
if (receivers == null) {
throw new ImporterException(new IllegalArgumentException(
"receivers"));
}
// Check mandatory fields
for (ImportedPerson person : receivers) {
if(person.getFirstName() == null) {
throw new ImporterException("firstName not set", new IllegalArgumentException(person.toString()));
}
if(person.getLastName() == null) {
throw new ImporterException("lastName not set", new IllegalArgumentException(person.toString()));
}
if(person.getEmailAddress() == null || person.getEmailAddress().equals("")) {
throw new ImporterException("emailAddress not set", new IllegalArgumentException(person.toString()));
}
if(person.getUID() == null) {
throw new ImporterException("UID not set", new IllegalArgumentException(person.toString()));
}
if(person.getRawPassword() == null) {
throw new ImporterException("RAW password not set", new IllegalArgumentException(person.toString()));
}
}
subject = subject.replaceAll("%%RTCADDRESS%%", configuration
.getRtcAddress());
body = body.replaceAll("%%RTCADDRESS%%", configuration.getRtcAddress());
for (ImportedPerson person : receivers) {
String s = subject;
String b = body;
// Replace subject variables
s = s.replaceAll("%%UID%%", person.getUID());
s = s.replaceAll("%%FIRST%%", person.getFirstName());
s = s.replaceAll("%%LAST%%", person.getLastName());
s = s.replaceAll("%%EMAIL%%", person.getEmailAddress());
s = s.replaceAll("%%PASS%%", person.getRawPassword());
// Replace body variables
b = b.replaceAll("%%UID%%", person.getUID());
b = b.replaceAll("%%FIRST%%", person.getFirstName());
b = b.replaceAll("%%LAST%%", person.getLastName());
b = b.replaceAll("%%EMAIL%%", person.getEmailAddress());
b = b.replaceAll("%%PASS%%", person.getRawPassword());
try {
SimpleEmail email = new SimpleEmail();
email.setCharset("UTF-8");
email.setHostName(configuration.getSmtpHost());
email.setSmtpPort(configuration.getSmtpPort());
email.setTLS(configuration.getSmtpTls());
if (configuration.getSmtpUser() != null
&& !configuration.getSmtpUser().equals("")) {
email.setAuthentication(configuration.getSmtpUser(),
configuration.getSmtpPassword());
}
email.addTo(person.getEmailAddress(), person.getFirstName()
+ " " + person.getLastName());
email.setFrom(configuration.getEmailFromAddress(),
configuration.getEmailFromName());
email.setSubject(s);
email.setMsg(b);
email.send();
log.info("Sent mail to " + person.getEmailAddress());
} catch (EmailException e) {
log.error(e);
}
}
}
}