Expand my Community achievements bar.

SOLVED

Email template subject encoding

Avatar

Level 2

Hello,

we have a long-term problem with encoding in email subjects, where characters like "šáěíéščřý" are not shown correctly. Until now, we have been changing the subjects to use basic ascii letters. But now we are expanding to russian customers, and with azbuka (cyrilic) letters we can no longer do this.

In principle, our mechanism is the same as described here: https://adobe-consulting-services.github.io/acs-aem-commons/features/e-mail/email-api/index.html

Where they are discussing the same problem in the comments below the article, with a final workaround: https://github.com/Adobe-Consulting-Services/acs-aem-commons/pull/1030

However, that would still mean to separate the subject from the template. That would mean additional work with moving the subjects from templates to the i18n dictionary.

So, is this the only solution as to now?

Best Regards

Martin

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Apache Sling Request Parameter Handling configuration for the Default parameter encoding is set correctly to UTF-8, that is not the problem.

With lack of any other answers the workaround mentioned here is still the best approach https://github.com/Adobe-Consulting-Services/acs-aem-commons/pull/1030

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @Martin-Nekula,

 

Please check Apache Sling Request Parameter Handling configuration for the Default parameter encoding" for your instance. Generally this encoding is used to display or send the data [which can include special characters as well]. If you are using the AEM default Day CQ Mail Service checking this would be helpful.

 

Hope this helps.

 

Thanks,

Kiran Vedantam.

Avatar

Level 2

Hello Kiran,

thank you for the answer. Yes, we are using the default one and the encoding is set to UTF-8. I think that shouldn´t be a problem, because the body of the emails is always displayed correctly and if we set the subject in code via

org.apache.commons.mail.HtmlEmail.setSubject()

 then the subject is displayed correctly as well. Problem is when the subject is taken from the mail template.

 

Avatar

Correct answer by
Level 2

Apache Sling Request Parameter Handling configuration for the Default parameter encoding is set correctly to UTF-8, that is not the problem.

With lack of any other answers the workaround mentioned here is still the best approach https://github.com/Adobe-Consulting-Services/acs-aem-commons/pull/1030

Avatar

Administrator
Thank you for sharing the solution with AEM community.


Kautuk Sahni

Avatar

Level 3

I'm facing the same Problem. We are using the emailtemplates where you can set the subject.

But if you have characters like "ä","ö","ü" in the emailtemplate in the subject line the characters not working

so "ü" will show like "ü".
As mentioned above, everything is set to UTF-8. And also there is no problem in the email body with the spcial characters.

As a workaround I changed the emailtemplate.txt to ANSI. But it should work with UTF-8 encoding. I think this is a bug. and Adobe should check the problem. 

 

In the code we call the send method with the required parameters, where the emailTemplatePath is the path to a emailtemplate in .txt format

 

 

private boolean sendMail(SlingHttpServletRequest request, String emailReceiver, String emailTemplatePath, String mandant) {
		boolean result = false;

		final FormGsonRequestModel formularGsonModel = getGsonModel(request);
		if (formularGsonModel != null) {
			formularGsonModel.setMandant(mandant);

			final ObjectMapper oMapper = new ObjectMapper();
			final Map<String, String> map = oMapper.convertValue(formularGsonModel, Map.class);

			final MailTemplate mailTemplate = MailTemplate.create(emailTemplatePath, request.getResourceResolver().adaptTo(Session.class));

			try {
				handleMailSubmission(emailReceiver, map, mailTemplate);

				// if reached, Mail got sent.
				result = true;
			} catch (MailingException | EmailException | MessagingException | IOException e) {
				log.error("Error during form submission. ", e);
			}
		}
		if (!result) {
			log.error("No json data found in request {}", request.getRequestPathInfo());
		}

		return result;
	}

	private void handleMailSubmission(String emailReceiver, Map<String, String> map, MailTemplate mailTemplate) throws IOException, MessagingException, EmailException {
		final HtmlEmail email = getHtmlEmail(emailReceiver, map, mailTemplate);
		MessageGateway<HtmlEmail> messageGateway = getMessageGatewayService().getGateway(HtmlEmail.class);
		if (messageGateway != null) {
			messageGateway.send(email);
		} else {
			throw new MailingException("Problems with the Email Server Configs");
		}

	}

	@NotNull
	private HtmlEmail getHtmlEmail(String emailReceiver, Map<String, String> map, MailTemplate mailTemplate) throws IOException, MessagingException, EmailException {
		HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(map), HtmlEmail.class);
		email.setCharset(StandardCharsets.UTF_8.name());
		email.addTo(emailReceiver);
		return email;
	}

 

 

 emailtemplate looks like this:

 

 

From: donotreply@company.com
Subject: Special chars like äöü works just with ANSI encoding "Looks like a bug"

<h1>Some Header</h1>

Firstname: ${firstname}

Lastname: ${lastname}


Email: ${email}