Uploaded image for project: 'Runtime'
  1. Runtime
  2. RUNTIME-3228

[Script] Add a mail utils

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Duplicate
    • Icon: Major Major
    • 4.3.0
    • 4.3.0
    • Plugin core
    • None
    • 4.3 RC2

      <?xml version="1.0" encoding="UTF-8"?>
      <function class="org.ametys.plugins.core.ui.script.StaticConfigurableScriptBinding">
          <descriptions>
              <description>
                  <name>SendMail.asText</name>
                  <text i18n="true">PLUGINS_RUNTIME_SCRIPT_FCT_SEND_MAIL_AS_TEXT</text>
              </description>
              <description>
                  <name>SendMail.asHTML</name>
                  <text i18n="true">PLUGINS_RUNTIME_SCRIPT_FCT_SEND_MAIL_AS_HTML</text>
              </description>
          </descriptions>
          <script>
      var SendMail = (function() {
          // Imports
          var Config = Java.type("org.ametys.runtime.config.Config");
          var SendMailHelper = Java.type("org.ametys.core.util.mail.SendMailHelper");
          var SimpleDateFormat = Java.type("java.text.SimpleDateFormat");
          var DateUtil = Java.type("java.util.Date");
          var Arrays = Java.type("java.util.Arrays");
          
          // Variables
          var SENDER = Config.getInstance().getValue("smtp.mail.from");
          var dateFormat = new SimpleDateFormat("dd/MM/yyyy");
          var current = new DateUtil();
          
          function asHTML(subject, body, recipients)
          {
              _send(subject, body, null, recipients);
          }
          
          function asText(subject, body, recipients)
          {
              _send(subject, null, body, recipients);
          }
          
          function _send(subject, bodyAsHTML, bodyAsText, recipients)
          {
              var subjectPrefix = "[Script - " + dateFormat.format(current) + "] ";
              var recipientsAsList = _jsArrayToJavaList(recipients);
              SendMailHelper.sendMail(subjectPrefix + subject, bodyAsHTML, bodyAsText, recipientsAsList, SENDER);
          }
          
          function _jsArrayToJavaList(array)
          {
              return Arrays.asList(array);
          }
          
          return {
              asHTML: asHTML,
              asText: asText
          };
      })();
          </script>
      </function>

      Here are i18n keys (french version only):

          <message key="PLUGINS_RUNTIME_SCRIPT_FCT_SEND_MAIL_AS_TEXT">envoi d'un mail au format texte à la liste de destinataires donnée (ex: &lt;i&gt;SendMail.asText("Sujet du mail", "Corps du mail", ["destinataire1@mail.org", "destinataire2@mail.org"])&lt;/i&gt;)</message>
          <message key="PLUGINS_RUNTIME_SCRIPT_FCT_SEND_MAIL_AS_HTML">envoi d'un mail au format HTML à la liste de destinataires donnée (ex: &lt;i&gt;SendMail.asHTML("Sujet du mail", "Corps du mail", ["destinataire1@mail.org", "destinataire2@mail.org"])&lt;/i&gt;)</message>
      

      Use it as :

      SendMail.asText("test sujet", "test corps", ["mymail@mail.org", "mysecondmail@mail.org"]);
      SendMail.asHTML("test sujet", "<span style='color:red'>test corps rouge</span>", ["mymail@mail.org"]);

            Unassigned Unassigned
            bmaurel Bérénice Maurel
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: