GithubHelp home page GithubHelp logo

kmitrovic / templated-mail-sender Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 112 KB

A simple library for sending templated, localized emails. The email templates themselves are stored locally on disk (and can be in git).

Shell 0.23% Java 99.16% HTML 0.61%

templated-mail-sender's Introduction

templated-mail-sender

A simple library for sending templated, localized emails. The email templates themselves are stored locally on disk (and can be in git). Designed to be easily embedded within a JVM environment.

Sending a message directly via the message queue

If you know where a mail-sender is running, you can craft a JSON message and put it directly onto the queue.

Simply issue a SET command against the kestrel server and use the following exemplary JSON as a guide:

{
  "event": "queue_tmail"      <!-- required, must be queue_tmail or message will be rejected -->
  "message": {
    "templateName" : "test_template",   <!-- required, refers to a set of files (possibly localized), located in
                                             the emailTemplateBaseDir, which is configured in the YML config -->
    "toName" : "Some User"              <!-- optional, default is to leave empty -->
    "toEmail" : "[email protected]"      <!-- required -->
    "locale" : "fr_fr"                  <!-- optional, default is en_us -->
    "parameters" : {                    <!-- optional, and as shown here, you can use map-style values for objects -->
      "someParam" : "Some Value"
      "anotherParam" : "Another Value"
      "user" : {
        "id": "some-user-id"
        "name": "some-user-name"
      }
    }
  }
}

Running a client locally within the JVM

Install the templated-mail-sender jar locally:

cd /where/you/code
git clone [email protected]:cobbzilla/templated-mail-sender.git
cd templated-mail-sender
mvn clean package install

Include this maven dependency in your pom.xml

<dependency>
  <groupId>org.cobbzilla</groupId>
   <artifactId>templated-mail-sender</artifactId>
   <version>1.0.0-SNAPSHOT</version>
</dependency>

Initialize a client object

final TemplatedMailConfiguration mailConfiguration = new TemplatedMailConfiguration();
... set various things on the configuration, or initialize from yml  ...

mailClient = new TemplatedMailClient(mailConfiguration);
mailClient.init();

Send an email from Java

Map<String, String> params = new HashMap<>();
params.put("testParam", "testValue");

Map<String, String> user = new HashMap<>();
user.put("id", "some-user-id");
user.put("name", "some-user-name");
params.put("user", user);

TemplatedMail testMessage = new TemplatedMail();
testMessage.setTemplateName("test_template");
testMessage.setToEmail("[email protected]");
testMessage.setLocale("en_us");
testMessage.setEmailMessage(emailMessage);
testMessage.setParameters(params);
mailClient.send(testMessage);

Creating a new email template

In your emailTemplateBaseDir directory (use TemplatedMailConfiguration.setEmailTemplateBaseDir or initialize via yml) create a new directory for your email. Let's say the emailTemplateBaseDir is /home/tout/email_templates and we want to create a new template named "foo/mail". We'll need to define, at a minimum:

/home/tout/email_templates/foo/mail.fromEmail.mustache  (the "from" email)
/home/tout/email_templates/foo/mail.subject.mustache  (the subject line)
/home/tout/email_templates/foo/mail.textMessage.mustache  (for mail clients that do not support HTML email)

Optionally, we can also specify:

/home/tout/email_templates/foo/mail.fromName.mustache  (the "from" name)
/home/tout/email_templates/foo/mail.cc.mustache  (a "cc" email, just one)
/home/tout/email_templates/foo/mail.bcc.mustache  (a "bcc" email, just one)
/home/tout/email_templates/foo/mail.htmlMessage.mustache  (for mail clients that DO support HTML email)

The contents of the above files are mustache templates. Please see http://mustache.github.com/mustache.5.html for more information on how to use mustache. Within the templates, you can reference any attributes that you expect to receive in the message.parameters that clients will send along with their requests to queue emails.

Localizing an email template

Create versions that include the localization string (use all lowercase), for example:

/home/tout/email_templates/foo/mail.textMessage_en_us.mustache
/home/tout/email_templates/foo/mail.textMessage_fr_fr.mustache
/home/tout/email_templates/foo/mail.textMessage_fr.mustache

The template system will find the best match it can. For example:

  • If the user's locale is "fr_fr" (French, France), they will get the mail.textMessage_fr_fr.mustache version
  • If the user's locale is "fr_ca" (French, Canada), they will get the mail.textMessage_fr.mustache version
  • If the user's locale is "es_es" (Spanish, Spain), they will get the mail.textMessage.mustache version (default version), since no other locale-specific templates match.

templated-mail-sender's People

Contributors

cobbzilla avatar kmitrovic avatar

Watchers

 avatar  avatar

Forkers

doooovla

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.