How to send email in Alfresco 4
-
27-06-2021 - |
Question
I am new to Alfresco (version 4) and I would like to create webscript (lib.js actually) for sending email that has function with following signature:
function sendMail(to, subject, templatePath, templateArgs )
This function would be called from other webscripts when some conditions are met. I have found script like this:
var mail = actions.create("mail");
mail.parameters.to = "receiver_email@some.domain";
mail.parameters.subject = "Test subject";
mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Workflow Notification/wf-email.html.ftl");
var templateArgs = new Array();
templateArgs['workflowTitle'] = "workflowTitle";
templateArgs['workflowPooled'] = true;
templateArgs['workflowDescription'] = "workflowDesc";
templateArgs['workflowId'] = "workflowId";
var templateModel = new Array();
templateModel['args'] = templateArgs;
mail.parameters.template_model = templateModel;
mail.execute(search.findNode("workspace://SpacesStore/9e15aaac-b30b-4266-984f-21fe273a6113"));
but I don't know how to put it in my code, since I don't know three things:
Can I use this code in my scenario, i.e. to make js lib which can be imported and used to send various emails?
If I can, where can I find reference to actions (what to import at the begining of the file)?
What should I put as an argument to mail.execute function?
Solution
mail.execute is the "against" rule. It means that the code asks you to execute the function against a node in alfresco. So, when you send an Email when you upload a document, the mail.execute uses the freshly created node, so you could append in the mail the link to the newly created file.
you could simplify that, anyway, and create a rule, that triggers on some content when it's created/updated, and use the sendmailaction.js as the one that you posted above, automatically to users you want.
otherwise you can create something like this:
function sendMail()
{
var mail = actions.create("mail");
mail.parameters.to = bpm_groupAssignees.properties["cm:email"];
mail.parameters.subject = "New File uplodaded with name " + document.name;
mail.parameters.from = initiator.properties["cm:email"];
mail.parameters.text = "Kindly approve the document: " + document.name;
mail.execute(document);
}
sendMail();