public static void sendEmailNotification()

in app/src/main/java/org/apache/roller/weblogger/util/MailUtil.java [254:496]


    public static void sendEmailNotification(WeblogEntryComment commentObject,
                                             RollerMessages messages, 
                                             I18nMessages resources,
                                             boolean notifySubscribers) 
            throws MailingException {

        // TODO: Factor out email notification from moderate message to owner.

        WeblogEntry entry = commentObject.getWeblogEntry();
        Weblog weblog = entry.getWebsite();
        User user = entry.getCreator();
        
        // Only send email if email notification is enabled, or a pending message that needs moderation.
        if (!commentObject.getPending()) {
            boolean notify = WebloggerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");
            if (!notify) {
                // notifications disabled, just bail
                return;
            } else {
                log.debug("Comment notification enabled ... preparing email");
            }
        } else {
            log.debug("Pending comment...sending moderation email to blog owner");
        }

        // build list of email addresses to send notification to
        Set<String> subscribers = new TreeSet<>();
        
        // If we are to notify subscribers, then...
        if (commentObject.getApproved() && notifySubscribers) {
            log.debug("Sending notification email to all subscribers");

            // Get all the subscribers to this comment thread
            List<WeblogEntryComment> comments = entry.getComments(true, true);
            for (WeblogEntryComment comment : comments) {
                if (!StringUtils.isEmpty(comment.getEmail())) {
                    // if user has commented twice, count the most recent notify setting
                    // also, don't send a routing email to the person who made the comment.
                    if (comment.getNotify() && !comment.getEmail().equals(commentObject.getEmail())) {
                        // only add those with valid email
                        if (comment.getEmail().matches(EMAIL_ADDR_REGEXP)) {
                            log.info("Add to subscribers list : " + comment.getEmail());
                            subscribers.add(comment.getEmail());
                        }
                    } else {
                        // remove user who doesn't want to be notified
                        log.info("Remove from subscribers list : " + comment.getEmail());
                        subscribers.remove(comment.getEmail());
                    }
                }
            }
        } else {
            log.debug("Sending notification email only to weblog owner");
        }

        //------------------------------------------
        // Form the messages to be sent -
        // The owner email gets the subscriber message with additional header and footer info added
        
        // Determine with mime type to use for e-mail
        StringBuilder msg = new StringBuilder();
        StringBuilder ownermsg = new StringBuilder();
        boolean isPlainText = !WebloggerRuntimeConfig.getBooleanProperty("users.comments.htmlenabled");
        
        // first the common stub message for the owner and commenters (if applicable)
        if (!isPlainText) {
            msg.append("<html><body style=\"background: white; ");
            msg.append(" color: black; font-size: 12px\">");
        }
        
        if (!StringUtils.isEmpty(commentObject.getName())) {
            msg.append(commentObject.getName() + " "
                    + resources.getString("email.comment.wrote")+": ");
        } else {
            msg.append(resources.getString("email.comment.anonymous")+": ");
        }
        
        msg.append((isPlainText) ? "\n\n" : "<br /><br />");

        // Don't escape the content if email will be sent as plain text
        msg.append((isPlainText) ? commentObject.getContent()
        : Utilities.transformToHTMLSubset(Utilities.escapeHTML(commentObject.getContent())));
        
        msg.append((isPlainText) ? "\n\n----\n"
                : "<br /><br /><hr /><span style=\"font-size: 11px\">");
        msg.append(resources.getString("email.comment.respond") + ": ");
        msg.append((isPlainText) ? "\n" : "<br />");
        
        // Build link back to comment
        String commentURL = WebloggerFactory.getWeblogger()
            .getUrlStrategy().getWeblogCommentsURL(weblog, null, entry.getAnchor(), true);
        
        if (isPlainText) {
            msg.append(commentURL);
        } else {
            msg.append("<a href=\""+commentURL+"\">"+commentURL+"</a></span>");
        }
        
        // next the additional information that is sent to the blog owner
        // Owner gets an email if it's pending and/or he's turned on notifications
        if (commentObject.getPending() || weblog.getEmailComments()) {
            // First, list any messages from the system that were passed in:
            if (messages.getMessageCount() > 0) {
                ownermsg.append((isPlainText) ? "" : "<p>");
                ownermsg.append(resources.getString("commentServlet.email.thereAreSystemMessages"));
                ownermsg.append((isPlainText) ? "\n\n" : "</p>");
                ownermsg.append((isPlainText) ? "" : "<ul>");
            }
            for (Iterator<RollerMessage> it = messages.getMessages(); it.hasNext();) {
                RollerMessage rollerMessage = it.next();
                ownermsg.append((isPlainText) ? "" : "<li>");
                ownermsg.append(MessageFormat.format(resources.getString(
                    rollerMessage.getKey()), (Object[])rollerMessage.getArgs()) );
                ownermsg.append((isPlainText) ? "\n\n" : "</li>");
            }
            if (messages.getMessageCount() > 0) {
                ownermsg.append((isPlainText) ? "\n\n" : "</ul>");
            }

            // Next, list any validation error messages that were passed in:
            if (messages.getErrorCount() > 0) {
                ownermsg.append((isPlainText) ? "" : "<p>");
                ownermsg.append(resources.getString("commentServlet.email.thereAreErrorMessages"));
                ownermsg.append((isPlainText) ? "\n\n" : "</p>");
                ownermsg.append((isPlainText) ? "" : "<ul>");
            }
            for (Iterator<RollerMessage> it = messages.getErrors(); it.hasNext();) {
                RollerMessage rollerMessage = it.next();
                ownermsg.append((isPlainText) ? "" : "<li>");
                ownermsg.append(MessageFormat.format(resources.getString(
                    rollerMessage.getKey()), (Object[])rollerMessage.getArgs()) );
                ownermsg.append((isPlainText) ? "\n\n" : "</li>");
            }
            if (messages.getErrorCount() > 0) {
                ownermsg.append((isPlainText) ? "\n\n" : "</ul>");
            }

            ownermsg.append(msg);

            ownermsg.append((isPlainText) ? "\n\n----\n" :
                "<br /><br /><hr /><span style=\"font-size: 11px\">");

            // commenter email address: allow blog owner to reply via email instead of blog comment
            if (!StringUtils.isBlank(commentObject.getEmail())) {
                ownermsg.append(resources.getString("email.comment.commenter.email") + ": " + commentObject.getEmail());
                ownermsg.append((isPlainText) ? "\n\n" : "<br/><br/>");
            }
            // add link to weblog edit page so user can login to manage comments
            ownermsg.append(resources.getString("email.comment.management.link") + ": ");
            ownermsg.append((isPlainText) ? "\n" : "<br/>");

            Map<String, String> parameters = new HashMap<>();
            parameters.put("bean.entryId", entry.getId());
            String deleteURL = WebloggerFactory.getWeblogger().getUrlStrategy().getActionURL(
                    "comments", "/roller-ui/authoring", weblog.getHandle(), parameters, true);

            if (isPlainText) {
                ownermsg.append(deleteURL);
            } else {
                ownermsg.append(
                        "<a href=\"" + deleteURL + "\">" + deleteURL + "</a></span>");
                msg.append("</Body></html>");
                ownermsg.append("</Body></html>");
            }
        }

        // determine email subject
        String subject;
        if (commentObject.getPending()) {
            subject = resources.getString("email.comment.moderate.title") + ": ";
        } else {
            if ((subscribers.size() > 1) ||
                    (StringUtils.equals(commentObject.getEmail(), user.getEmailAddress()))) {
                subject= "RE: "+resources.getString("email.comment.title")+": ";
            } else {
                subject = resources.getString("email.comment.title") + ": ";
            }
        }
        subject += entry.getTitle();
        
        // send message to email recipients
        try {
            // use either the weblog configured from address or the site configured from address
            String from = weblog.getEmailAddress();
            if(StringUtils.isEmpty(from)) {
                from = user.getEmailAddress();
            }

            boolean isHtml = !isPlainText;
            
            if (commentObject.getPending() || weblog.getEmailComments()) {
                if(isHtml) {
                    sendHTMLMessage(
                            from,
                            new String[]{user.getEmailAddress()},
                            null,
                            null,
                            subject,
                            ownermsg.toString());
                } else {
                    sendTextMessage(
                            from,
                            new String[]{user.getEmailAddress()},
                            null,
                            null,
                            subject,
                            ownermsg.toString());
                }
            }

            // now send to subscribers
            if (notifySubscribers && !subscribers.isEmpty()) {
                // Form array of commenter addrs
                String[] commenterAddrs = subscribers.toArray(String[]::new);

                if (isHtml) {
                    sendHTMLMessage(
                            from, 
                            null,
                            null,
                            commenterAddrs,
                            subject, 
                            msg.toString());
                } else {
                    sendTextMessage(
                            from, 
                            null,
                            null,
                            commenterAddrs,
                            subject, 
                            msg.toString());
                }
            }
        } catch (Exception e) {
            log.warn("Exception sending comment notification mail", e);
            // This will log the stack trace if debug is enabled
            if (log.isDebugEnabled()) {
                log.debug(e);
            }
        }
        
        log.debug("Done sending email message");
    }