<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.infinite-erp.co.id/index.php?action=history&amp;feed=atom&amp;title=How_to_send_emails_on_events</id>
	<title>How to send emails on events - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.infinite-erp.co.id/index.php?action=history&amp;feed=atom&amp;title=How_to_send_emails_on_events"/>
	<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_to_send_emails_on_events&amp;action=history"/>
	<updated>2026-04-06T18:39:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.infinite-erp.co.id/index.php?title=How_to_send_emails_on_events&amp;diff=1103&amp;oldid=prev</id>
		<title>Wikiadmin: Created page with &quot;{{(!)|The contents of this document are available from Openbravo '''3.0MP22'''}}  == Introduction ==  Openbravo provides a mechanism that allows to p...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_to_send_emails_on_events&amp;diff=1103&amp;oldid=prev"/>
		<updated>2021-07-11T00:54:37Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{(!)|The contents of this document are available from Openbravo &amp;#039;&amp;#039;&amp;#039;&lt;a href=&quot;/index.php?title=Release_Notes/3.0MP22&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Release Notes/3.0MP22 (page does not exist)&quot;&gt;3.0MP22&lt;/a&gt;&amp;#039;&amp;#039;&amp;#039;}}  == Introduction ==  Openbravo provides a mechanism that allows to p...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{(!)|The contents of this document are available from Openbravo '''[[Release_Notes/3.0MP22|3.0MP22]]'''}}&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Openbravo provides a mechanism that allows to programmatically generate events to send emails. Event triggering and email implementation is decoupled, being possible to launch generic events, that are listened by other modules that implement the email contents.&lt;br /&gt;
&lt;br /&gt;
This how to explains both, how to trigger the events and how to listen to them to generate the event.&lt;br /&gt;
&lt;br /&gt;
Email server is [[Client#Email_Configuration|configured]] at client level.&lt;br /&gt;
&lt;br /&gt;
== Triggering email events ==&lt;br /&gt;
&lt;br /&gt;
An event is identified simply by a &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Events are triggered programmatically from any piece of code, such as [[How_to_create_a_Standard_Process_Definition|Processes]] or [[How_to_implement_a_business_event_handler|Business Event Handlers]].&lt;br /&gt;
&lt;br /&gt;
To trigger an event you need to:&lt;br /&gt;
&lt;br /&gt;
Inject in your class a &amp;lt;code&amp;gt;EmailEventManager&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  ...&lt;br /&gt;
  @Inject&lt;br /&gt;
  private EmailEventManager emailManager;&lt;br /&gt;
  ...&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use this manager to create the events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  try {&lt;br /&gt;
    boolean sent = emailManager.sendEmail(&amp;quot;EVT_NAME&amp;quot;, &amp;quot;user1@test.abc; user2@test.abc&amp;quot;, data);&lt;br /&gt;
    if (sent) {&lt;br /&gt;
      // email was sent&lt;br /&gt;
    } else {&lt;br /&gt;
      // email was not sent&lt;br /&gt;
    }&lt;br /&gt;
  } catch (EmailEventException e) {&lt;br /&gt;
    // something went wrong...&lt;br /&gt;
  } &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First parameter is the ''event name'' that identifies the event. Second one is the list of recipients, semicolon (;) separated if more than one. And finally, any &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt; with the data that will be used to generate the event. Note even this data is something generic (&amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;), the actual type the listener will expect can vary depending on the event.&lt;br /&gt;
&lt;br /&gt;
== Listening to email events ==&lt;br /&gt;
&lt;br /&gt;
Email events are listened by classes implementing &amp;lt;code&amp;gt;EmailEventContentGenerator&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To listen to an event, you need to create a class of &amp;lt;code&amp;gt;EmailEventContentGenerator&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class MyEmailGenerator implements EmailEventContentGenerator {&lt;br /&gt;
  @Override&lt;br /&gt;
  public boolean isValidEvent(String event, Object data) {&lt;br /&gt;
    return &amp;quot;EVT_NAME&amp;quot;.equals(event);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  public String getSubject(Object data, String event) {&lt;br /&gt;
    return &amp;quot;New email for you&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
  @Override&lt;br /&gt;
  public String getBody(Object data, String event) {&lt;br /&gt;
    User usr = (User) data;&lt;br /&gt;
    return &amp;quot;Hello &amp;quot; + usr.getFirstName;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Override&lt;br /&gt;
  public String getContentType() {&lt;br /&gt;
    return &amp;quot;text/plain; charset=utf-8&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let's explain the main methods to implement (there are some other ones explained in the javadoc):&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;isValidEvent&amp;lt;/code&amp;gt;&lt;br /&gt;
:Determines whether our class listens to the event. Internally, the emailManager iterates over all &amp;lt;code&amp;gt;EmailEventContentGenerator&amp;lt;/code&amp;gt; classes executing this method, email is tried to be sent in case it returns &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;. Following our example, the name of the event we sent before was &amp;quot;EVT_NAME&amp;quot;, so we return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; for this event and &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; for any other one. Note that a single class could listen to several events.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;getSubject&amp;lt;/code&amp;gt;&lt;br /&gt;
:Returns an &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; with the subject of the email. It also receives the data, so this subject can be different based on it.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;code&amp;gt;getBody&amp;lt;/code&amp;gt;&lt;br /&gt;
:Returns an &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; with the body of the email. It also receives the data, so this subject can be different based on it. In this example, we expect data to be a User, so we can cast it to generate the body based on its attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
More advanced bodies can be composed using freemarker templates, in this way it is possible to other module to override the template without needing to do any Java coding. In [[How_to_create_a_Navigation_Bar_Component#Creating_a_template | this how to]], it is explained how templates can be created.&lt;br /&gt;
&lt;br /&gt;
[[Category: HowTo]]&lt;/div&gt;</summary>
		<author><name>Wikiadmin</name></author>
		
	</entry>
</feed>