Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ MailerSend Java SDK
- [Send an email](#send-an-email)
- [Add CC, BCC recipients](#add-cc-bcc-recipients)
- [Send a template-based email](#send-a-template-based-email)
- [Send a template-based email in a specific language](#send-a-template-based-email-in-a-specific-language)
- [Personalization](#personalization)
- [Send email with attachment](#send-email-with-attachment)
- [Send email with inline attachment](#send-email-with-inline-attachment)
Expand Down Expand Up @@ -306,6 +307,45 @@ public void sendEmail() {
}
```

### Send a template-based email in a specific language

The optional `language` field is a language code that selects a template translation. It is only meaningful when a template id is set and is ignored
for raw HTML/text sends. Supported values: `de`, `en`, `es`, `fr`, `it`, `lt`, `nl`, `pl`, `pt-BR`.

```java
import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

Email email = new Email();

email.setFrom("name", "your email");

Recipient recipient = new Recipient("name", "your@recipient.com");

email.addRecipient(recipient);

email.setTemplateId("Your MailerSend template ID");
email.setLanguage("pt-BR");

MailerSend ms = new MailerSend();

ms.setToken("Your API token");

try {

MailerSendResponse response = ms.emails().send(email);
System.out.println(response.messageId);
} catch (MailerSendException e) {

e.printStackTrace();
}
}
```

### Personalization

```java
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/mailersend/sdk/emails/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public class Email {
@SerializedName("template_id")
public String templateId;

@SerializedName("language")
public String language;

@SerializedName("tags")
public ArrayList<String> tags = new ArrayList<String>();

Expand Down Expand Up @@ -384,10 +387,23 @@ public void setPlain(String plain) {
* @param templateId a {@link java.lang.String} object.
*/
public void setTemplateId(String templateId) {

this.templateId = templateId;
}



/**
* Sets the email's template translation language as a language code
* (e.g. "de", "fr", "pt-BR"). Only meaningful when a template id is set;
* ignored for raw html/text sends.
*
* @param language a {@link java.lang.String} object.
*/
public void setLanguage(String language) {

this.language = language;
}


/**
* Adds a personalization for the given recipient
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/mailersend/sdk/tests/EmailConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,38 @@ public void testCamelCaseAddReplyTo() {
assertEquals("reply name 2", email.replyTo.name);
assertEquals("reply2@test.com", email.replyTo.email);
}


/**
* Tests that setLanguage stores the language and it serializes as "language" in JSON
*/
@Test
public void testLanguageSerialization() {

Email email = TestHelper.createBasicEmail(true);

email.setTemplateId("neqvygm021wl0p7w");
email.setLanguage("pt-BR");

assertEquals("pt-BR", email.language);

String json = email.serializeForSending();

assertTrue(json.contains("\"language\""));
assertTrue(json.contains("pt-BR"));
}


/**
* Tests that the language field is omitted from the JSON when not set
*/
@Test
public void testLanguageOmittedWhenNotSet() {

Email email = TestHelper.createBasicEmail(true);

String json = email.serializeForSending();

assertTrue(!json.contains("\"language\""));
}
}
Loading