upd: transform existing mails into mail templates
authorFelix Dörre <felix@dogcraft.de>
Thu, 28 Jul 2016 16:34:27 +0000 (18:34 +0200)
committerFelix Dörre <felix@dogcraft.de>
Fri, 29 Jul 2016 08:28:45 +0000 (10:28 +0200)
Change-Id: Ib0e9174ccf2cca11b5dba11e23da3af12bb7494c

src/org/cacert/gigi/dbObjects/SupportNotificationMail.templ [new file with mode: 0644]
src/org/cacert/gigi/dbObjects/SupportedUser.java
src/org/cacert/gigi/email/MailProbe.java
src/org/cacert/gigi/email/MailProbe.templ [new file with mode: 0644]
src/org/cacert/gigi/pages/PasswordResetMail.templ [new file with mode: 0644]
src/org/cacert/gigi/pages/PasswordResetPage.java

diff --git a/src/org/cacert/gigi/dbObjects/SupportNotificationMail.templ b/src/org/cacert/gigi/dbObjects/SupportNotificationMail.templ
new file mode 100644 (file)
index 0000000..42254b7
--- /dev/null
@@ -0,0 +1,9 @@
+Subject: [<?=${ticket}?>] <?=${subject}?>
+
+<?=_Hi?>,
+
+<?=_supporter ${supporter} triggered:?>
+
+<?=$action?>
+
+RA DB
index 193a32b0853492069c33059b007510c2636eb279..975ef9b51e02055fb444819a4330d43a42cadbab 100644 (file)
@@ -1,18 +1,15 @@
 package org.cacert.gigi.dbObjects;
 
 import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.util.HashMap;
 import java.util.Locale;
 
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
-import org.cacert.gigi.email.SendMail;
 import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.template.MailTemplate;
 import org.cacert.gigi.output.template.Outputable;
-import org.cacert.gigi.output.template.SprintfCommand;
 import org.cacert.gigi.util.DayDate;
 import org.cacert.gigi.util.ServerConstants;
 
@@ -87,19 +84,18 @@ public class SupportedUser {
         target.revokeGroup(supporter, toMod);
     }
 
+    private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
+
     public void sendSupportNotification(String subject, Outputable message) {
         try {
-            StringWriter sw = new StringWriter();
-            PrintWriter outMail = new PrintWriter(sw);
-            outMail.print("Hi," + "\n\n");
-            SprintfCommand.createSimple("supporter {0} triggered:", supporter.getPreferredName().toString()).output(outMail, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
-            outMail.print("\n\n");
-            message.output(outMail, Language.getInstance(Locale.ENGLISH), new HashMap<String, Object>());
-            outMail.print("\n\n");
-            outMail.print("RA DB");
-            outMail.close();
+            HashMap<String, Object> vars = new HashMap<>();
+            vars.put("supporter", supporter.getPreferredName().toString());
+            vars.put("action", message);
+            vars.put("ticket", this.getTicket());
+            vars.put("subject", subject);
+
             String supportemailaddress = "support@" + ServerConstants.getWwwHostName().replaceFirst("^www\\.", "");
-            SendMail.getInstance().sendMail(supportemailaddress, "[" + this.getTicket() + "] RA DB " + subject, sw.toString(), supportemailaddress, null, null, null, null, false);
+            supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
         } catch (IOException e) {
             e.printStackTrace();
         }
index 987c036ef5203b85ddd9e471730bcd131052dbe9..4da2648ae83d36f6eed4138829c390e7be839620 100644 (file)
@@ -1,28 +1,20 @@
 package org.cacert.gigi.email;
 
 import java.io.IOException;
+import java.util.HashMap;
 
 import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.template.MailTemplate;
 import org.cacert.gigi.util.ServerConstants;
 
 public class MailProbe {
 
+    private static final MailTemplate mailProbe = new MailTemplate(MailProbe.class.getResource("MailProbe.templ"));
+
     public static void sendMailProbe(Language l, String type, int id, String hash, String address) throws IOException {
-        StringBuffer body = new StringBuffer();
-        body.append(l.getTranslation("Thanks for signing up with SomeCA.org, below is the link you need to open to verify your account. Once your account is verified you will be able to start issuing certificates till your hearts' content!"));
-        body.append("\n\nhttps://");
-        body.append(ServerConstants.getWwwHostNamePortSecure());
-        body.append("/verify?type=");
-        body.append(type);
-        body.append("&id=");
-        body.append(id);
-        body.append("&hash=");
-        body.append(hash);
-        body.append("\n\n");
-        body.append(l.getTranslation("Best regards"));
-        body.append("\n");
-        body.append(l.getTranslation("SomeCA.org Support!"));
-        EmailProvider.getInstance().sendMail(address, "[SomeCA.org] " + l.getTranslation("Mail Probe"), body.toString(), "support@cacert.org", null, null, null, null, false);
+        HashMap<String, Object> vars = new HashMap<>();
+        vars.put("link", "https://" + ServerConstants.getWwwHostNamePortSecure() + "/verify?type=" + type + "&id=" + id + "&hash=" + hash);
+        mailProbe.sendMail(l, vars, address);
     }
 
 }
diff --git a/src/org/cacert/gigi/email/MailProbe.templ b/src/org/cacert/gigi/email/MailProbe.templ
new file mode 100644 (file)
index 0000000..c6f818f
--- /dev/null
@@ -0,0 +1,9 @@
+Subject: <?=_Mail Probe?>
+
+<?=_Thanks for signing up with SomeCA.org, below is the link you need to open to verify your account. Once your account is verified you will be able to start issuing certificates till your hearts' content!?>
+
+<?=$link?>
+
+<?=_Best regards?>
+
+<?=_SomeCA.org Support!?>
diff --git a/src/org/cacert/gigi/pages/PasswordResetMail.templ b/src/org/cacert/gigi/pages/PasswordResetMail.templ
new file mode 100644 (file)
index 0000000..dd27407
--- /dev/null
@@ -0,0 +1,12 @@
+Subject: <?=$subject?>
+
+<?=_Hi?>,
+
+<?=$method?>
+
+<?=$link?>
+
+<?=_This process will expire in ${hour_max} hours.?>
+
+<?=_Best regards?>
+<?=_SomeCA.org Support?>
index 1be5643c30d08e1ee16cd5e5e62f6df388c6f103..45abb59c8978ba1d29c5b844217cface94673124 100644 (file)
@@ -2,7 +2,6 @@ package org.cacert.gigi.pages;
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.net.URLEncoder;
 import java.util.HashMap;
 import java.util.Map;
@@ -13,10 +12,9 @@ import javax.servlet.http.HttpServletResponse;
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.dbObjects.User;
-import org.cacert.gigi.email.SendMail;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.template.Form;
-import org.cacert.gigi.output.template.SprintfCommand;
+import org.cacert.gigi.output.template.MailTemplate;
 import org.cacert.gigi.output.template.Template;
 import org.cacert.gigi.util.AuthorizationContext;
 import org.cacert.gigi.util.RandomToken;
@@ -114,30 +112,20 @@ public class PasswordResetPage extends Page {
         return true;
     }
 
+    private static final MailTemplate passwordResetMail = new MailTemplate(PasswordResetPage.class.getResource("PasswordResetMail.templ"));
+
     public static void initPasswordResetProcess(PrintWriter out, User targetUser, HttpServletRequest req, String aword, Language l, String method, String subject) {
         String ptok = RandomToken.generateToken(32);
         int id = targetUser.generatePasswordResetTicket(Page.getUser(req), ptok, aword);
         try {
-            StringWriter sw = new StringWriter();
-            PrintWriter outMail = new PrintWriter(sw);
-            outMail.print(l.getTranslation("Hi,") + "\n\n");
-            outMail.print(method);
-            outMail.print("\n\nhttps://");
-            outMail.print(ServerConstants.getWwwHostNamePortSecure() + PasswordResetPage.PATH);
-            outMail.print("?id=");
-            outMail.print(id);
-            outMail.print("&token=");
-            outMail.print(URLEncoder.encode(ptok, "UTF-8"));
-            outMail.print("\n");
-            outMail.print("\n");
-            SprintfCommand.createSimple("This process will expire in {0} hours.", Integer.toString(HOUR_MAX)).output(outMail, l, new HashMap<String, Object>());
-            outMail.print("\n");
-            outMail.print("\n");
-            outMail.print(l.getTranslation("Best regards"));
-            outMail.print("\n");
-            outMail.print(l.getTranslation("SomeCA.org Support!"));
-            outMail.close();
-            SendMail.getInstance().sendMail(Page.getUser(req).getEmail(), "[SomeCA.org] " + subject, sw.toString(), "support@cacert.org", null, null, null, null, false);
+            HashMap<String, Object> vars = new HashMap<>();
+            vars.put("subject", subject);
+            vars.put("method", method);
+            vars.put("link", "https://" + ServerConstants.getWwwHostNamePortSecure() + PasswordResetPage.PATH //
+                    + "?id=" + id + "&token=" + URLEncoder.encode(ptok, "UTF-8"));
+            vars.put("hour_max", HOUR_MAX);
+
+            passwordResetMail.sendMail(l, vars, Page.getUser(req).getEmail());
             out.println(Page.getLanguage(req).getTranslation("Password reset successful."));
         } catch (IOException e) {
             e.printStackTrace();