An application without mail, it exists? Never seen ! 😆
Several solutions to manage the mails are available:
- Directly in the configuration of the mail event?
It’s possible, it works, but maintenance level is complex and not flexible at all, we forget!
- The second solution is to deport the mails in database, and to use a stored procedure and a series of tags to make the mail generic. A little more details below:
Create the table that will contain the email
First, create a table that will allow to manage the contents of the different mails, we will need a code of the mail, to easily identify process side of the subject of the email and its body:
Example of mail (simple):
- Code : NEWTASK
- Object : [Blog] – Validation of the request %ID% %Title%
- Body : Hello %UserDisplayName%, <br/><br/>You must validate the request %ID% (Title : %Title%).<br /><br /><a href=’%LinkToTask%’>Click here to validate</a>
💡 Tip: As you can see, we can put HTML in the body and the object of the mall, you can make nice emails with lots of colors super easily 😎
Create the stored procedure that will restore the mail
Our process will not be able to directly look for the contents of the email in the table, because the mails have dynamic contents (name of the person who receives the mail, link to the task, information on the request being validated, .. .). A stored procedure will have to be used to generate the body of the mail with the good content.
If you want more details about creating stored procedures and their use in K2, Thomas explains everything in his article on enK2next.
The mail templates contained in the table created above must have tags, which will be replaced by the stored procedure by the real values. In my example, the tags have the form %NomDuTag%, this tag format can be modified according to your needs.
The stored procedure will take at least a parameter:
- The code of the mail that one seeks to return
- The business ID of the request (depending on our application, it may vary)
- The link to the task (must be present in an optional way)
The stored procedure will:
- Retrieve the model of the mail according to the code passed in parameter
- Retrieve information from the business table (business data in the example)
- Replace mail tags with information retrieved from the business table or passed as a parameter
- Resend the mail
Process side, nothing more simple, in our emails event, we will call our stored procedure and inform him the input information as below:
It’s good, we can test now, and here is the result:
It’s your turn 😉