Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDEMPIERE-6063: Customization of email sent when server starts #2264

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 47 additions & 5 deletions org.adempiere.base/src/org/compiere/util/WebEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.apache.ecs.xhtml.tr;
import org.compiere.Adempiere;
import org.compiere.model.MClient;
import org.compiere.model.MMailText;
import org.compiere.model.MSysConfig;
import org.compiere.model.MSystem;
import org.compiere.model.SystemIDs;

Expand Down Expand Up @@ -186,15 +188,55 @@ public static boolean initWeb (ServletContext context)
// Logging now initiated
if (log.isLoggable(Level.INFO)) log.info(info.toString());
//
MClient client = MClient.get(Env.getCtx(), 0);
MSystem system = MSystem.get(Env.getCtx());
client.sendEMail(client.getRequestEMail(),
"Server started: " + system.getName() + " (" + WebUtil.getServerName() + ")",
"ServerInfo: " + context.getServerInfo(), null);

serverStartEMail(context);

return s_initOK;
} // initWeb

/*
* Send an email when the server starts
* @param context servlet context
*/
private static void serverStartEMail(ServletContext context) {

boolean mailSent = false;

MClient client = MClient.get(Env.getCtx(), 0);
MSystem system = MSystem.get(Env.getCtx());

String recipient = MSysConfig.getValue("EMAIL_SERVER_START_RECIPIENT", 0, 0);
if (Util.isEmpty(recipient) || !EMail.validate(recipient))
recipient = client.getRequestEMail();

int mailtextID = MSysConfig.getIntValue("EMAIL_SERVER_START_MAILTEXT_ID", 0, 0);
if (mailtextID > 0) {

try {
MMailText mt = new MMailText(Env.getCtx(), mailtextID, null);
mt.setPO(client);
String subject = mt.getMailHeader();
subject = subject.replace("#SYSTEM_NAME#", system.getName());
subject = subject.replace("#SERVER_NAME#", WebUtil.getServerName());

String message = mt.getMailText(true);
message = message.replace("#SERVER_INFO#", context.getServerInfo());
message = message.replace("#ADEMPIERE_VERSION#", Adempiere.getVersion());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead using a new convention, I think it is better to use back the current mail template convention for variable (i.e @variableName@) and use back the existing mail template parse function. For variables that's not coming from MSystem and MClient, you can create a new Java class for that (i.e the plain Java bean class should have getServerName, getServerInfo and getVersion methods).


client.sendEMail(recipient, subject, message, null);

mailSent = true;

} catch (Exception e) {
log.warning("Can't send customized email when server starts: " + e.toString());
}
}

if (!mailSent)
client.sendEMail(client.getRequestEMail(),
"Server started: " + system.getName() + " (" + WebUtil.getServerName() + ")",
"ServerInfo: " + context.getServerInfo(), null);
}

/**************************************************************************
* Get Base Directory entry.
Expand Down