選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. templated-mail-sender
  2. ==========================
  3. A simple library for sending templated, localized emails. The email templates themselves are stored locally
  4. on disk (and can be in git). Designed to be easily embedded within a JVM environment.
  5. ## Sending a message directly via the message queue
  6. If you know where a mail-sender is running, you can craft a JSON message and put it directly onto the queue.
  7. Simply issue a SET command against the kestrel server and use the following exemplary JSON as a guide:
  8. {
  9. "event": "queue_tmail" <!-- required, must be queue_tmail or message will be rejected -->
  10. "message": {
  11. "templateName" : "test_template", <!-- required, refers to a set of files (possibly localized), located in
  12. the emailTemplateBaseDir, which is configured in the YML config -->
  13. "toName" : "Some User" <!-- optional, default is to leave empty -->
  14. "toEmail" : "user@example.com" <!-- required -->
  15. "locale" : "fr_fr" <!-- optional, default is en_us -->
  16. "parameters" : { <!-- optional, and as shown here, you can use map-style values for objects -->
  17. "someParam" : "Some Value"
  18. "anotherParam" : "Another Value"
  19. "user" : {
  20. "id": "some-user-id"
  21. "name": "some-user-name"
  22. }
  23. }
  24. }
  25. }
  26. ## Running a client locally within the JVM
  27. ### Install the templated-mail-sender jar locally:
  28. cd /where/you/code
  29. git clone git@github.com:cobbzilla/templated-mail-sender.git
  30. cd templated-mail-sender
  31. mvn clean package install
  32. ### Include this maven dependency in your pom.xml
  33. <dependency>
  34. <groupId>org.cobbzilla</groupId>
  35. <artifactId>templated-mail-sender</artifactId>
  36. <version>1.0.0-SNAPSHOT</version>
  37. </dependency>
  38. ### Initialize a client object
  39. final TemplatedMailConfiguration mailConfiguration = new TemplatedMailConfiguration();
  40. ... set various things on the configuration, or initialize from yml ...
  41. mailClient = new TemplatedMailClient(mailConfiguration);
  42. mailClient.init();
  43. ### Send an email from Java
  44. Map<String, String> params = new HashMap<>();
  45. params.put("testParam", "testValue");
  46. Map<String, String> user = new HashMap<>();
  47. user.put("id", "some-user-id");
  48. user.put("name", "some-user-name");
  49. params.put("user", user);
  50. TemplatedMail testMessage = new TemplatedMail();
  51. testMessage.setTemplateName("test_template");
  52. testMessage.setToEmail("recipient@example.com");
  53. testMessage.setLocale("en_us");
  54. testMessage.setEmailMessage(emailMessage);
  55. testMessage.setParameters(params);
  56. mailClient.send(testMessage);
  57. ## Creating a new email template
  58. In your emailTemplateBaseDir directory (use TemplatedMailConfiguration.setEmailTemplateBaseDir or initialize via yml)
  59. create a new directory for your email. Let's say the emailTemplateBaseDir is /home/tout/email_templates and we want
  60. to create a new template named "foo/mail". We'll need to define, at a minimum:
  61. /home/tout/email_templates/foo/mail.fromEmail.mustache (the "from" email)
  62. /home/tout/email_templates/foo/mail.subject.mustache (the subject line)
  63. /home/tout/email_templates/foo/mail.textMessage.mustache (for mail clients that do not support HTML email)
  64. Optionally, we can also specify:
  65. /home/tout/email_templates/foo/mail.fromName.mustache (the "from" name)
  66. /home/tout/email_templates/foo/mail.cc.mustache (a "cc" email, just one)
  67. /home/tout/email_templates/foo/mail.bcc.mustache (a "bcc" email, just one)
  68. /home/tout/email_templates/foo/mail.htmlMessage.mustache (for mail clients that DO support HTML email)
  69. The contents of the above files are mustache templates. Please see http://mustache.github.com/mustache.5.html
  70. for more information on how to use mustache. Within the templates, you can reference any attributes that you
  71. expect to receive in the message.parameters that clients will send along with their requests to queue emails.
  72. ## Localizing an email template
  73. Create versions that include the localization string (use all lowercase), for example:
  74. /home/tout/email_templates/foo/mail.textMessage_en_us.mustache
  75. /home/tout/email_templates/foo/mail.textMessage_fr_fr.mustache
  76. /home/tout/email_templates/foo/mail.textMessage_fr.mustache
  77. The template system will find the best match it can. For example:
  78. * If the user's locale is "fr\_fr" (French, France), they will get the mail.textMessage\_fr\_fr.mustache version
  79. * If the user's locale is "fr\_ca" (French, Canada), they will get the mail.textMessage\_fr.mustache version
  80. * 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.