-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45171 from cescoffier/mailer-fire-sent-mail
Send SentMail Instances When Sending Emails
- Loading branch information
Showing
8 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package io.quarkus.mailer; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
|
@@ -28,7 +30,7 @@ public class InjectionTest { | |
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(BeanUsingBareMailClient.class, BeanUsingBlockingMailer.class, | ||
BeanUsingReactiveMailer.class, MailTemplates.class) | ||
BeanUsingReactiveMailer.class, MailTemplates.class, MailListener.class) | ||
.addAsResource("mock-config.properties", "application.properties") | ||
.addAsResource(new StringAsset("" | ||
+ "<html>{name}</html>"), "templates/test1.html") | ||
|
@@ -56,15 +58,33 @@ public class InjectionTest { | |
@Inject | ||
MailTemplates templates; | ||
|
||
@Inject | ||
MailListener listener; | ||
|
||
@Test | ||
public void testInjection() { | ||
beanUsingMutiny.verify(); | ||
beanUsingBare.verify(); | ||
beanUsingBlockingMailer.verify(); | ||
|
||
await().until(() -> listener.getLast() != null); | ||
listener.reset(); | ||
|
||
beanUsingReactiveMailer.verify().toCompletableFuture().join(); | ||
templates.send1(); | ||
templates.send2().await(); | ||
templates.sendNative().await(); | ||
await().until(() -> listener.getLast() != null); | ||
listener.reset(); | ||
|
||
templates.send1().await().indefinitely(); | ||
await().until(() -> listener.getLast() != null); | ||
listener.reset(); | ||
|
||
templates.send2().await().indefinitely(); | ||
await().until(() -> listener.getLast() != null); | ||
listener.reset(); | ||
|
||
templates.sendNative().await().indefinitely(); | ||
await().until(() -> listener.getLast() != null); | ||
listener.reset(); | ||
assertEquals("<html>Me</html>", MailTemplates.Templates.testNative("Me").templateInstance().render()); | ||
} | ||
|
||
|
@@ -139,4 +159,22 @@ Uni<Void> sendNative() { | |
return Templates.testNative("John").to("[email protected]").subject("Test").send(); | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MailListener { | ||
|
||
volatile SentMail last; | ||
|
||
public void onMailSent(@Observes SentMail mail) { | ||
last = mail; | ||
} | ||
|
||
public SentMail getLast() { | ||
return last; | ||
} | ||
|
||
public void reset() { | ||
last = null; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
extensions/mailer/runtime/src/main/java/io/quarkus/mailer/SentMail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.mailer; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Flow; | ||
|
||
/** | ||
* Represents a sent mail. | ||
* Instances of this class are sent using CDI events. | ||
* | ||
* @param from the sender address | ||
* @param to the list of recipients | ||
* @param cc the list of CC recipients | ||
* @param bcc the list of BCC recipients | ||
* @param replyTo the list of reply-to addresses | ||
* @param bounceAddress the bounce address | ||
* @param subject the subject | ||
* @param textBody the text body | ||
* @param htmlBody the HTML body | ||
* @param headers the headers | ||
* @param attachments the attachments | ||
*/ | ||
public record SentMail(String from, | ||
List<String> to, List<String> cc, List<String> bcc, | ||
String replyTo, String bounceAddress, | ||
String subject, String textBody, String htmlBody, | ||
Map<String, List<String>> headers, List<SentAttachment> attachments) { | ||
|
||
/** | ||
* An immutable representation of an attachment that has been sent. | ||
* | ||
* @param name the name | ||
* @param file the file | ||
* @param description the description | ||
* @param disposition the disposition | ||
* @param data the data | ||
* @param contentType the content type | ||
* @param contentId the content ID | ||
*/ | ||
public record SentAttachment(String name, File file, String description, String disposition, | ||
Flow.Publisher<Byte> data, String contentType, String contentId) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters