Skip to content

Commit

Permalink
Merge branch '1.5.x' into 2.1.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/files-sync.yml
#	.github/workflows/graalvm.yml
#	.github/workflows/gradle.yml
#	.github/workflows/release.yml
#	email-amazon-ses/src/test/groovy/io/micronaut/email/ses/SesEmailComposerSpec.groovy
#	email/src/main/java/io/micronaut/email/Contact.java
#	gradle.properties
#	gradle/libs.versions.toml
#	gradle/wrapper/gradle-wrapper.jar
#	gradle/wrapper/gradle-wrapper.properties
#	gradlew
#	settings.gradle
  • Loading branch information
sdelamo committed Oct 10, 2023
2 parents 7e457b7 + 75d119e commit dcb86d3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.email.BodyType;
import io.micronaut.email.Contact;
import io.micronaut.email.Email;
Expand Down Expand Up @@ -94,7 +95,9 @@ private SdkBytes bytesOfMessage(@NonNull Message message) throws IOException, Me
private SendEmailRequest sendEmailRequest(@NonNull Email email) {
SendEmailRequest.Builder requestBuilder = SendEmailRequest.builder()
.destination(destinationBuilder(email).build())
.source(email.getFrom().getEmail())
.source(StringUtils.isNotEmpty(email.getFrom().getName()) ?
email.getFrom().getNameAddress() :
email.getFrom().getEmail())
.message(message(email));
if (CollectionUtils.isNotEmpty(email.getReplyToCollection())) {
requestBuilder = requestBuilder.replyToAddresses(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.micronaut.email.ses


import io.micronaut.email.Contact
import io.micronaut.email.Email
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
Expand Down Expand Up @@ -178,4 +178,33 @@ class SesEmailComposerSpec extends Specification {
subject == request.message().subject().data()
!request.destination().ccAddresses()
}

@Unroll
void "from field should allow including the sender name"(String name, String email, String expected) {
given:
Contact from = new Contact(email, name)
String to = "receiver@example.com"
String subject = "Apple Music"

when:
SendEmailRequest request = sesEmailComposer.compose(Email.builder()
.from(from)
.to(to)
.subject(subject)
.body("Lore ipsum body")
.build()) as SendEmailRequest

then:
expected == request.source()
[to] == request.destination().toAddresses().toList()
subject == request.message().subject().data()
!request.destination().ccAddresses()
!request.destination().bccAddresses()

where:
name | email | expected
"John Doe" | "sender@example.com" | "John Doe <sender@example.com>"
"" | "sender@example.com" | "sender@example.com"
null | "sender@example.com" | "sender@example.com"
}
}
31 changes: 31 additions & 0 deletions email/src/main/java/io/micronaut/email/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import io.micronaut.core.util.StringUtils;
import java.util.Objects;

/**
Expand Down Expand Up @@ -73,6 +74,36 @@ public String getName() {
return name;
}

/**
* returns name-addr for a Contact.
* Given:
* Contact(email: 'johnsnow@example.com, name: John Snow)
*
* When:
* Contact::getNameAddress()
*
* Then:
* {@literal John Snow <johnsnow@example.com>}
*
* Given:
* Contact(email: 'johnsnow@example.com, name: null)
*
* When:
* Contact::getNameAddress()
*
* Then:
* {@literal <johnsnow@example.com>}
*
* @see <a href="https://www.rfc-editor.org/rfc/rfc5322#section-3.4">Address Specification</a>
* @return An optional name that indicates the name of the recipient that could be displayed to the user of a mail application, and the email address enclosed in angle brackets
*/
@NonNull
public String getNameAddress() {
return StringUtils.isNotEmpty(getName()) ?
String.format("%s <%s>", name, email) :
"<" + email + ">";
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
16 changes: 16 additions & 0 deletions email/src/test/groovy/io/micronaut/email/ContactSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.micronaut.email

import io.micronaut.core.beans.BeanIntrospection
import spock.lang.Specification
import spock.lang.Unroll

class ContactSpec extends Specification {
void "Contact is annotated with @Introspected"() {
Expand All @@ -11,4 +12,19 @@ class ContactSpec extends Specification {
then:
noExceptionThrown()
}

@Unroll
void "Contact::getNameAddress"(String email, String name, String expected) {
when:
new Contact(email, name).getNameAddress()


then:
noExceptionThrown()

where:
email | name | expected
'johnsnow@example.com' | 'John Snow' | 'John Snow <johnsnow@example.com>'
'johnsnow@example.com' | null | '<johnsnow@example.com>'
}
}
2 changes: 1 addition & 1 deletion src/main/docs/guide/integrations/javamail.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You need to provide beans of type api:io.micronaut.email.javamail.sender.MailPro

As an alternative to providing your own api:io.micronaut.email.javamail.sender.SessionProvider[] for authentication, you can configure password based authentication via configuration:

[source, yaml]
[configuration]
----
javamail:
authentication:
Expand Down

0 comments on commit dcb86d3

Please sign in to comment.