Search in sources :

Example 1 with EmailConfiguration

use of org.hisp.dhis.email.EmailConfiguration in project dhis2-core by dhis2.

the class EmailMessageSender method sendMessage.

// -------------------------------------------------------------------------
// MessageSender implementation
// -------------------------------------------------------------------------
@Override
public OutboundMessageResponse sendMessage(String subject, String text, String footer, User sender, Set<User> users, boolean forceSend) {
    EmailConfiguration emailConfig = getEmailConfiguration();
    OutboundMessageResponse status = new OutboundMessageResponse();
    String errorMessage = "No recipient found";
    if (emailConfig.getHostName() == null) {
        status.setOk(false);
        status.setDescription(EmailResponse.HOST_CONFIG_NOT_FOUND.getResponseMessage());
        status.setResponseObject(EmailResponse.HOST_CONFIG_NOT_FOUND);
        return status;
    }
    String serverBaseUrl = configurationProvider.getServerBaseUrl();
    String plainContent = renderPlainContent(text, sender);
    String htmlContent = renderHtmlContent(text, footer, serverBaseUrl != null ? HOST + serverBaseUrl : "", sender);
    try {
        HtmlEmail email = getHtmlEmail(emailConfig.getHostName(), emailConfig.getPort(), emailConfig.getUsername(), emailConfig.getPassword(), emailConfig.isTls(), emailConfig.getFrom());
        email.setSubject(getPrefixedSubject(subject));
        email.setTextMsg(plainContent);
        email.setHtmlMsg(htmlContent);
        boolean hasRecipients = false;
        for (User user : users) {
            boolean doSend = forceSend || (Boolean) userSettingService.getUserSetting(UserSettingKey.MESSAGE_EMAIL_NOTIFICATION, user);
            if (doSend && ValidationUtils.emailIsValid(user.getEmail())) {
                if (isEmailValid(user.getEmail())) {
                    email.addBcc(user.getEmail());
                    hasRecipients = true;
                    log.info("Sending email to user: " + user.getUsername() + " with email address: " + user.getEmail());
                } else {
                    log.warn(user.getEmail() + " is not a valid email for user: " + user.getUsername());
                    errorMessage = "No valid email address found";
                }
            }
        }
        if (hasRecipients) {
            email.send();
            log.info("Email sent using host: " + emailConfig.getHostName() + ":" + emailConfig.getPort() + " with TLS: " + emailConfig.isTls());
            status = new OutboundMessageResponse("Email sent", EmailResponse.SENT, true);
        } else {
            status = new OutboundMessageResponse(errorMessage, EmailResponse.ABORTED, false);
        }
    } catch (Exception ex) {
        log.error("Error while sending email: " + ex.getMessage() + ", " + DebugUtils.getStackTrace(ex));
        status = new OutboundMessageResponse("Email not sent: " + ex.getMessage(), EmailResponse.FAILED, false);
    }
    return status;
}
Also used : User(org.hisp.dhis.user.User) EmailConfiguration(org.hisp.dhis.email.EmailConfiguration) HtmlEmail(org.apache.commons.mail.HtmlEmail) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) EmailException(org.apache.commons.mail.EmailException)

Example 2 with EmailConfiguration

use of org.hisp.dhis.email.EmailConfiguration in project dhis2-core by dhis2.

the class EmailMessageSender method sendMessage.

@Override
public OutboundMessageResponse sendMessage(String subject, String text, Set<String> recipients) {
    EmailConfiguration emailConfig = getEmailConfiguration();
    OutboundMessageResponse status = new OutboundMessageResponse();
    String errorMessage = "No recipient found";
    String serverBaseUrl = configurationProvider.getServerBaseUrl();
    if (emailConfig.getHostName() == null) {
        status.setOk(false);
        status.setDescription(EmailResponse.HOST_CONFIG_NOT_FOUND.getResponseMessage());
        status.setResponseObject(EmailResponse.HOST_CONFIG_NOT_FOUND);
        return status;
    }
    try {
        HtmlEmail email = getHtmlEmail(emailConfig.getHostName(), emailConfig.getPort(), emailConfig.getUsername(), emailConfig.getPassword(), emailConfig.isTls(), emailConfig.getFrom());
        email.setSubject(getPrefixedSubject(subject));
        email.setTextMsg(text);
        email.setHtmlMsg(renderHtmlContent(text, null, serverBaseUrl, null));
        boolean hasRecipients = false;
        for (String recipient : recipients) {
            if (isEmailValid(recipient)) {
                email.addBcc(recipient);
                hasRecipients = true;
                log.info("Sending email to : " + recipient);
            } else {
                log.warn(recipient + " is not a valid email");
                errorMessage = "No valid email address found";
            }
        }
        if (hasRecipients) {
            email.send();
            log.info("Email sent using host: " + emailConfig.getHostName() + ":" + emailConfig.getPort() + " with TLS: " + emailConfig.isTls());
            return new OutboundMessageResponse("Email sent", EmailResponse.SENT, true);
        } else {
            status = new OutboundMessageResponse(errorMessage, EmailResponse.ABORTED, false);
        }
    } catch (Exception ex) {
        log.error("Error while sending email: " + ex.getMessage() + ", " + DebugUtils.getStackTrace(ex));
        status = new OutboundMessageResponse("Email not sent: " + ex.getMessage(), EmailResponse.FAILED, false);
    }
    return status;
}
Also used : EmailConfiguration(org.hisp.dhis.email.EmailConfiguration) HtmlEmail(org.apache.commons.mail.HtmlEmail) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse) EmailException(org.apache.commons.mail.EmailException)

Aggregations

EmailException (org.apache.commons.mail.EmailException)2 HtmlEmail (org.apache.commons.mail.HtmlEmail)2 EmailConfiguration (org.hisp.dhis.email.EmailConfiguration)2 OutboundMessageResponse (org.hisp.dhis.outboundmessage.OutboundMessageResponse)2 User (org.hisp.dhis.user.User)1