/** * @param tenant the tenant of the shop the customer checked out from * @param customerEmail the email of the customer * @param locale the locale of the customer * @return an optional notification email object, present if all information is there and valid and the template * exists, absent otherwise */ private MailTemplate getCustomerNotificationEmail(Tenant tenant, String customerEmail, Locale locale) { return new MailTemplate() .from(getTenantContactEmail(tenant)) .to(customerEmail) .template("order-paid-customer") .locale(locale); }
private Optional<Path> resolveTemplate(Path base, MailTemplate mail) { Path templatePath = null; if (mail.locale().isPresent()) { templatePath = fileManager.resolvePermanentFilePath( base.resolve("emails")).resolve("localized") .resolve(mail.locale().get().toLanguageTag()) .resolve(mail.template() + ".twig"); if (!templatePath.toFile().isFile()) { templatePath = fileManager.resolvePermanentFilePath( base.resolve("emails")).resolve("localized") .resolve(mail.locale().get().getLanguage()) .resolve(mail.template() + ".twig"); } } if (templatePath == null || !templatePath.toFile().isFile()) { // Failed to find a localized mail, try the default one templatePath = fileManager.resolvePermanentFilePath(base.resolve("emails")) .resolve(mail.template() + ".twig"); } return templatePath.toFile().isFile() ? Optional.of(templatePath) : Optional.<Path>absent(); }
@Override public void sendTemplateMail(MailTemplate mail, Map<String, Object> context, Tenant tenant) throws MailException { Optional<Path> resolved = resolveTenantTemplate(mail, tenant); if (!resolved.isPresent()) { resolved = resolveDefaultTemplate(mail); } if (!resolved.isPresent() || !resolved.get().toFile().isFile()) { throw new MailException("Mail template not found"); } try { String result = templateRenderer.renderAsString(resolved.get(), context); List<String> parts = Lists.newArrayList(Splitter.on(DELIMITER).trimResults().split(result)); if (parts.size() != 3) { throw new MailException("Failed to render email. Is the mail extending mail.twig ?"); } mail.subject(parts.get(0)); if (!Strings.isNullOrEmpty(parts.get(1))) { mail.text(parts.get(1)); } if (!Strings.isNullOrEmpty(parts.get(2))) { mail.html(parts.get(2)); } mailService.sendEmail(mail); } catch (TemplateRenderingException e) { throw new MailException("Failed to render mail template", e); } }
"invoice-" + invoiceNumber.getNumber() + ".pdf"); customerNotificationEmail.addAttachment(attachment); tenantNotificationEmail.addAttachment(attachment); } catch (InvoicingException e) { logger.error("Failed to to generate invoice for email", e);
/** * @param tenant the tenant of the shop the customer checked out from * @param locale the main locale of the tenant * @return an optional notification email object, present if all information is there and valid and the template * exists, absent otherwise */ private MailTemplate getTenantNotificationEmail(Tenant tenant, Locale locale) { return new MailTemplate() .from(generalSettings.getNotificationsEmail()) .to(getTenantContactEmail(tenant)) .template("order-paid-tenant") .locale(locale); }
private void sendPasswordResetMail(User user, final String secret, AccountsSettings settings) { MailTemplate mailTemplate = new MailTemplate().template("password-reset").to(user.getEmail()) .from(generalSettings.getNotificationsEmail()); try { Map<String, Object> context = Maps.newHashMap(); for (UserDataSupplier supplier : userDataSuppliers.values()) { supplier.supply(user, context); } String passwordResetUriLink; if (!Strings.isNullOrEmpty(settings.getUserPasswordResetUriTemplate().getValue())) { passwordResetUriLink = settings.getUserPasswordResetUriTemplate().getValue(); } else { passwordResetUriLink = urlHelper.getContextWebURL("/login/reset-password/${resetKey}").toString(); } SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(); groovy.text.Template uriTemplate = templateEngine.createTemplate(passwordResetUriLink); context.put("resetLink", uriTemplate.make(new HashMap() { { put("resetKey", secret); } }).toString()); context.put("siteName", siteSettings.getName()); mailTemplateService.sendTemplateMail(mailTemplate, context); } catch (MailException | ClassNotFoundException | IOException e) { logger.error("Failed to send validation email", e); } }
private void sendValidationMail(final User createdUser, AccountsSettings settings, Map<String, Object> additionalContext) { MailTemplate mailTemplate = new MailTemplate().template("account-validation").to(createdUser.getEmail()) .from(generalSettings.getNotificationsEmail()); try { Map<String, Object> context = Maps.newHashMap(); context.putAll(additionalContext); String validationUriTemplate; if (!Strings.isNullOrEmpty(settings.getUserValidationUriTemplate().getValue())) { validationUriTemplate = settings.getUserValidationUriTemplate().getValue(); } else { validationUriTemplate = urlHelper.getContextWebURL("/account/validation/${validationKey}").toString(); } SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(); groovy.text.Template uriTemplate = templateEngine.createTemplate(validationUriTemplate); context.put("validationLink", uriTemplate.make(new HashMap() { { put("validationKey", createdUser.getValidationKey()); } }).toString()); context.put("siteName", siteSettings.getName()); mailTemplateService.sendTemplateMail(mailTemplate, context); } catch (MailException | ClassNotFoundException | IOException e) { logger.error("Failed to send validation email", e); } }