Use gmail to send invoices (#2130)

This commit is contained in:
Weimin Yu 2023-08-29 14:25:54 -04:00 committed by GitHub
parent 57592d787c
commit ee3ece8c56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 26 deletions

View file

@ -723,6 +723,18 @@ public final class RegistryConfig {
.collect(toImmutableList()); .collect(toImmutableList());
} }
/**
* Returns an optional return email address that overrides the default {@code reply-to} address
* in outgoing invoicing email messages.
*/
@Provides
@Config("invoiceReplyToEmailAddress")
public static Optional<InternetAddress> provideInvoiceReplyToEmailAddress(
RegistryConfigSettings config) {
return Optional.ofNullable(config.billing.invoiceReplyToEmailAddress)
.map(RegistryConfig::parseEmailAddress);
}
/** /**
* Returns the file prefix for the invoice CSV file. * Returns the file prefix for the invoice CSV file.
* *

View file

@ -170,6 +170,7 @@ public class RegistryConfigSettings {
/** Configuration for monthly invoices. */ /** Configuration for monthly invoices. */
public static class Billing { public static class Billing {
public List<String> invoiceEmailRecipients; public List<String> invoiceEmailRecipients;
public String invoiceReplyToEmailAddress;
public String invoiceFilePrefix; public String invoiceFilePrefix;
} }

View file

@ -382,6 +382,8 @@ icannReporting:
billing: billing:
invoiceEmailRecipients: [] invoiceEmailRecipients: []
# Optional return address that overrides the default.
invoiceReplyToEmailAddress: null
invoiceFilePrefix: REG-INV invoiceFilePrefix: REG-INV
rde: rde:

View file

@ -120,7 +120,8 @@ public final class GmailClient {
MimeMessage msg = MimeMessage msg =
new MimeMessage(Session.getDefaultInstance(new Properties(), /* authenticator= */ null)); new MimeMessage(Session.getDefaultInstance(new Properties(), /* authenticator= */ null));
msg.setFrom(this.outgoingEmailAddressWithUsername); msg.setFrom(this.outgoingEmailAddressWithUsername);
msg.setReplyTo(new InternetAddress[] {replyToEmailAddress}); msg.setReplyTo(
new InternetAddress[] {emailMessage.replyToEmailAddress().orElse(replyToEmailAddress)});
msg.addRecipients( msg.addRecipients(
RecipientType.TO, toArray(emailMessage.recipients(), InternetAddress.class)); RecipientType.TO, toArray(emailMessage.recipients(), InternetAddress.class));
msg.setSubject(emailMessage.subject()); msg.setSubject(emailMessage.subject());

View file

@ -23,12 +23,13 @@ import com.google.common.io.CharStreams;
import com.google.common.net.MediaType; import com.google.common.net.MediaType;
import google.registry.config.RegistryConfig.Config; import google.registry.config.RegistryConfig.Config;
import google.registry.gcs.GcsUtils; import google.registry.gcs.GcsUtils;
import google.registry.groups.GmailClient;
import google.registry.reporting.billing.BillingModule.InvoiceDirectoryPrefix; import google.registry.reporting.billing.BillingModule.InvoiceDirectoryPrefix;
import google.registry.util.EmailMessage; import google.registry.util.EmailMessage;
import google.registry.util.EmailMessage.Attachment; import google.registry.util.EmailMessage.Attachment;
import google.registry.util.SendEmailService;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Optional;
import javax.inject.Inject; import javax.inject.Inject;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import org.joda.time.YearMonth; import org.joda.time.YearMonth;
@ -36,11 +37,12 @@ import org.joda.time.YearMonth;
/** Utility functions for sending emails involving monthly invoices. */ /** Utility functions for sending emails involving monthly invoices. */
public class BillingEmailUtils { public class BillingEmailUtils {
private final SendEmailService emailService; private final GmailClient gmailClient;
private final YearMonth yearMonth; private final YearMonth yearMonth;
private final InternetAddress outgoingEmailAddress; private final InternetAddress outgoingEmailAddress;
private final InternetAddress alertRecipientAddress; private final InternetAddress alertRecipientAddress;
private final ImmutableList<InternetAddress> invoiceEmailRecipients; private final ImmutableList<InternetAddress> invoiceEmailRecipients;
private final Optional<InternetAddress> replyToEmailAddress;
private final String billingBucket; private final String billingBucket;
private final String invoiceFilePrefix; private final String invoiceFilePrefix;
private final String invoiceDirectoryPrefix; private final String invoiceDirectoryPrefix;
@ -48,20 +50,22 @@ public class BillingEmailUtils {
@Inject @Inject
BillingEmailUtils( BillingEmailUtils(
SendEmailService emailService, GmailClient gmailClient,
YearMonth yearMonth, YearMonth yearMonth,
@Config("gSuiteOutgoingEmailAddress") InternetAddress outgoingEmailAddress, @Config("gSuiteOutgoingEmailAddress") InternetAddress outgoingEmailAddress,
@Config("alertRecipientEmailAddress") InternetAddress alertRecipientAddress, @Config("alertRecipientEmailAddress") InternetAddress alertRecipientAddress,
@Config("invoiceEmailRecipients") ImmutableList<InternetAddress> invoiceEmailRecipients, @Config("invoiceEmailRecipients") ImmutableList<InternetAddress> invoiceEmailRecipients,
@Config("invoiceReplyToEmailAddress") Optional<InternetAddress> replyToEmailAddress,
@Config("billingBucket") String billingBucket, @Config("billingBucket") String billingBucket,
@Config("invoiceFilePrefix") String invoiceFilePrefix, @Config("invoiceFilePrefix") String invoiceFilePrefix,
@InvoiceDirectoryPrefix String invoiceDirectoryPrefix, @InvoiceDirectoryPrefix String invoiceDirectoryPrefix,
GcsUtils gcsUtils) { GcsUtils gcsUtils) {
this.emailService = emailService; this.gmailClient = gmailClient;
this.yearMonth = yearMonth; this.yearMonth = yearMonth;
this.outgoingEmailAddress = outgoingEmailAddress; this.outgoingEmailAddress = outgoingEmailAddress;
this.alertRecipientAddress = alertRecipientAddress; this.alertRecipientAddress = alertRecipientAddress;
this.invoiceEmailRecipients = invoiceEmailRecipients; this.invoiceEmailRecipients = invoiceEmailRecipients;
this.replyToEmailAddress = replyToEmailAddress;
this.billingBucket = billingBucket; this.billingBucket = billingBucket;
this.invoiceFilePrefix = invoiceFilePrefix; this.invoiceFilePrefix = invoiceFilePrefix;
this.invoiceDirectoryPrefix = invoiceDirectoryPrefix; this.invoiceDirectoryPrefix = invoiceDirectoryPrefix;
@ -74,13 +78,14 @@ public class BillingEmailUtils {
String invoiceFile = String.format("%s-%s.csv", invoiceFilePrefix, yearMonth); String invoiceFile = String.format("%s-%s.csv", invoiceFilePrefix, yearMonth);
BlobId invoiceFilename = BlobId.of(billingBucket, invoiceDirectoryPrefix + invoiceFile); BlobId invoiceFilename = BlobId.of(billingBucket, invoiceDirectoryPrefix + invoiceFile);
try (InputStream in = gcsUtils.openInputStream(invoiceFilename)) { try (InputStream in = gcsUtils.openInputStream(invoiceFilename)) {
emailService.sendEmail( gmailClient.sendEmail(
EmailMessage.newBuilder() EmailMessage.newBuilder()
.setSubject(String.format("Domain Registry invoice data %s", yearMonth)) .setSubject(String.format("Domain Registry invoice data %s", yearMonth))
.setBody( .setBody(
String.format("Attached is the %s invoice for the domain registry.", yearMonth)) String.format("Attached is the %s invoice for the domain registry.", yearMonth))
.setFrom(outgoingEmailAddress) .setFrom(outgoingEmailAddress)
.setRecipients(invoiceEmailRecipients) .setRecipients(invoiceEmailRecipients)
.setReplyToEmailAddress(replyToEmailAddress)
.setAttachment( .setAttachment(
Attachment.newBuilder() Attachment.newBuilder()
.setContent(CharStreams.toString(new InputStreamReader(in, UTF_8))) .setContent(CharStreams.toString(new InputStreamReader(in, UTF_8)))
@ -100,7 +105,7 @@ public class BillingEmailUtils {
/** Sends an e-mail to the provided alert e-mail address indicating a billing failure. */ /** Sends an e-mail to the provided alert e-mail address indicating a billing failure. */
void sendAlertEmail(String body) { void sendAlertEmail(String body) {
try { try {
emailService.sendEmail( gmailClient.sendEmail(
EmailMessage.newBuilder() EmailMessage.newBuilder()
.setSubject(String.format("Billing Pipeline Alert: %s", yearMonth)) .setSubject(String.format("Billing Pipeline Alert: %s", yearMonth))
.setBody(body) .setBody(body)

View file

@ -125,6 +125,9 @@ public class GmailClientTest {
assertThat(mimeMessage.getRecipients(RecipientType.TO)).asList().containsExactly(toAddr); assertThat(mimeMessage.getRecipients(RecipientType.TO)).asList().containsExactly(toAddr);
assertThat(mimeMessage.getRecipients(RecipientType.CC)).asList().containsExactly(ccAddr); assertThat(mimeMessage.getRecipients(RecipientType.CC)).asList().containsExactly(ccAddr);
assertThat(mimeMessage.getRecipients(RecipientType.BCC)).asList().containsExactly(bccAddr); assertThat(mimeMessage.getRecipients(RecipientType.BCC)).asList().containsExactly(bccAddr);
assertThat(mimeMessage.getReplyTo())
.asList()
.containsExactly(new InternetAddress("replyTo@example.com"));
assertThat(mimeMessage.getSubject()).isEqualTo("My subject"); assertThat(mimeMessage.getSubject()).isEqualTo("My subject");
assertThat(mimeMessage.getContent()).isInstanceOf(MimeMultipart.class); assertThat(mimeMessage.getContent()).isInstanceOf(MimeMultipart.class);
MimeMultipart parts = (MimeMultipart) mimeMessage.getContent(); MimeMultipart parts = (MimeMultipart) mimeMessage.getContent();
@ -137,6 +140,23 @@ public class GmailClientTest {
assertThat(attachment.getContent()).isEqualTo("foo,bar\nbaz,qux"); assertThat(attachment.getContent()).isEqualTo("foo,bar\nbaz,qux");
} }
@Test
public void toMimeMessage_overrideReplyToAddr() throws Exception {
InternetAddress fromAddr = new InternetAddress("from@example.com", "My sender");
InternetAddress toAddr = new InternetAddress("to@example.com");
InternetAddress replyToAddr = new InternetAddress("some-addr@another.com");
EmailMessage emailMessage =
EmailMessage.newBuilder()
.setFrom(fromAddr)
.setRecipients(ImmutableList.of(toAddr))
.setReplyToEmailAddress(replyToAddr)
.setSubject("My subject")
.setBody("My body")
.build();
MimeMessage mimeMessage = getGmailClient(true).toMimeMessage(emailMessage);
assertThat(mimeMessage.getReplyTo()).asList().containsExactly(replyToAddr);
}
@Test @Test
public void toGmailMessage() throws Exception { public void toGmailMessage() throws Exception {
MimeMessage mimeMessage = mock(MimeMessage.class); MimeMessage mimeMessage = mock(MimeMessage.class);

View file

@ -27,11 +27,12 @@ import com.google.cloud.storage.BlobId;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.net.MediaType; import com.google.common.net.MediaType;
import google.registry.gcs.GcsUtils; import google.registry.gcs.GcsUtils;
import google.registry.groups.GmailClient;
import google.registry.util.EmailMessage; import google.registry.util.EmailMessage;
import google.registry.util.EmailMessage.Attachment; import google.registry.util.EmailMessage.Attachment;
import google.registry.util.SendEmailService;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Optional;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import org.joda.time.YearMonth; import org.joda.time.YearMonth;
@ -42,39 +43,44 @@ import org.mockito.ArgumentCaptor;
/** Unit tests for {@link google.registry.reporting.billing.BillingEmailUtils}. */ /** Unit tests for {@link google.registry.reporting.billing.BillingEmailUtils}. */
class BillingEmailUtilsTest { class BillingEmailUtilsTest {
private SendEmailService emailService; private GmailClient gmailClient;
private BillingEmailUtils emailUtils; private BillingEmailUtils emailUtils;
private GcsUtils gcsUtils; private GcsUtils gcsUtils;
private ArgumentCaptor<EmailMessage> contentCaptor; private ArgumentCaptor<EmailMessage> contentCaptor;
@BeforeEach @BeforeEach
void beforeEach() throws Exception { void beforeEach() throws Exception {
emailService = mock(SendEmailService.class); gmailClient = mock(GmailClient.class);
gcsUtils = mock(GcsUtils.class); gcsUtils = mock(GcsUtils.class);
when(gcsUtils.openInputStream(BlobId.of("test-bucket", "results/REG-INV-2017-10.csv"))) when(gcsUtils.openInputStream(BlobId.of("test-bucket", "results/REG-INV-2017-10.csv")))
.thenReturn( .thenReturn(
new ByteArrayInputStream("test,data\nhello,world".getBytes(StandardCharsets.UTF_8))); new ByteArrayInputStream("test,data\nhello,world".getBytes(StandardCharsets.UTF_8)));
contentCaptor = ArgumentCaptor.forClass(EmailMessage.class); contentCaptor = ArgumentCaptor.forClass(EmailMessage.class);
emailUtils = emailUtils = getEmailUtils(Optional.of(new InternetAddress("reply-to@test.com")));
new BillingEmailUtils( }
emailService,
new YearMonth(2017, 10), private BillingEmailUtils getEmailUtils(Optional<InternetAddress> replyToAddress)
new InternetAddress("my-sender@test.com"), throws Exception {
new InternetAddress("my-receiver@test.com"), return new BillingEmailUtils(
ImmutableList.of( gmailClient,
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")), new YearMonth(2017, 10),
"test-bucket", new InternetAddress("my-sender@test.com"),
"REG-INV", new InternetAddress("my-receiver@test.com"),
"results/", ImmutableList.of(
gcsUtils); new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")),
replyToAddress,
"test-bucket",
"REG-INV",
"results/",
gcsUtils);
} }
@Test @Test
void testSuccess_emailOverallInvoice() throws MessagingException { void testSuccess_emailOverallInvoice() throws MessagingException {
emailUtils.emailOverallInvoice(); emailUtils.emailOverallInvoice();
verify(emailService).sendEmail(contentCaptor.capture()); verify(gmailClient).sendEmail(contentCaptor.capture());
EmailMessage emailMessage = contentCaptor.getValue(); EmailMessage emailMessage = contentCaptor.getValue();
EmailMessage expectedContent = EmailMessage expectedContent =
EmailMessage.newBuilder() EmailMessage.newBuilder()
@ -84,6 +90,7 @@ class BillingEmailUtilsTest {
new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com"))) new InternetAddress("hello@world.com"), new InternetAddress("hola@mundo.com")))
.setSubject("Domain Registry invoice data 2017-10") .setSubject("Domain Registry invoice data 2017-10")
.setBody("Attached is the 2017-10 invoice for the domain registry.") .setBody("Attached is the 2017-10 invoice for the domain registry.")
.setReplyToEmailAddress(new InternetAddress("reply-to@test.com"))
.setAttachment( .setAttachment(
Attachment.newBuilder() Attachment.newBuilder()
.setContent("test,data\nhello,world") .setContent("test,data\nhello,world")
@ -94,11 +101,21 @@ class BillingEmailUtilsTest {
assertThat(emailMessage).isEqualTo(expectedContent); assertThat(emailMessage).isEqualTo(expectedContent);
} }
@Test
void testSuccess_emailOverallInvoiceNoReplyOverride() throws Exception {
emailUtils = getEmailUtils(Optional.empty());
emailUtils.emailOverallInvoice();
verify(gmailClient).sendEmail(contentCaptor.capture());
EmailMessage emailMessage = contentCaptor.getValue();
assertThat(emailMessage.replyToEmailAddress()).isEmpty();
}
@Test @Test
void testFailure_emailsAlert() throws MessagingException { void testFailure_emailsAlert() throws MessagingException {
doThrow(new RuntimeException(new MessagingException("expected"))) doThrow(new RuntimeException(new MessagingException("expected")))
.doNothing() .doNothing()
.when(emailService) .when(gmailClient)
.sendEmail(contentCaptor.capture()); .sendEmail(contentCaptor.capture());
RuntimeException thrown = RuntimeException thrown =
assertThrows(RuntimeException.class, () -> emailUtils.emailOverallInvoice()); assertThrows(RuntimeException.class, () -> emailUtils.emailOverallInvoice());
@ -108,14 +125,14 @@ class BillingEmailUtilsTest {
.hasMessageThat() .hasMessageThat()
.isEqualTo("javax.mail.MessagingException: expected"); .isEqualTo("javax.mail.MessagingException: expected");
// Verify we sent an e-mail alert // Verify we sent an e-mail alert
verify(emailService, times(2)).sendEmail(contentCaptor.capture()); verify(gmailClient, times(2)).sendEmail(contentCaptor.capture());
validateAlertMessage(contentCaptor.getValue(), "Emailing invoice failed due to expected"); validateAlertMessage(contentCaptor.getValue(), "Emailing invoice failed due to expected");
} }
@Test @Test
void testSuccess_sendAlertEmail() throws MessagingException { void testSuccess_sendAlertEmail() throws MessagingException {
emailUtils.sendAlertEmail("Alert!"); emailUtils.sendAlertEmail("Alert!");
verify(emailService).sendEmail(contentCaptor.capture()); verify(gmailClient).sendEmail(contentCaptor.capture());
validateAlertMessage(contentCaptor.getValue(), "Alert!"); validateAlertMessage(contentCaptor.getValue(), "Alert!");
} }

View file

@ -49,6 +49,9 @@ public abstract class EmailMessage {
// TODO(b/279671974): remove `from` after migration. // TODO(b/279671974): remove `from` after migration.
public abstract InternetAddress from(); public abstract InternetAddress from();
/** Optional return email address that overrides the default. */
public abstract Optional<InternetAddress> replyToEmailAddress();
public abstract ImmutableSet<InternetAddress> ccs(); public abstract ImmutableSet<InternetAddress> ccs();
public abstract ImmutableSet<InternetAddress> bccs(); public abstract ImmutableSet<InternetAddress> bccs();
@ -69,6 +72,10 @@ public abstract class EmailMessage {
public abstract Builder setFrom(InternetAddress from); public abstract Builder setFrom(InternetAddress from);
public abstract Builder setReplyToEmailAddress(InternetAddress replyToEmailAddress);
public abstract Builder setReplyToEmailAddress(Optional<InternetAddress> replyToEmailAddress);
public abstract Builder setBccs(Collection<InternetAddress> bccs); public abstract Builder setBccs(Collection<InternetAddress> bccs);
public abstract Builder setCcs(Collection<InternetAddress> ccs); public abstract Builder setCcs(Collection<InternetAddress> ccs);