From 3535056f5c92f744e86586b49fad9882ba39484e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrique=20Lopes=20N=C3=B3brega?= Date: Fri, 26 Jul 2024 18:30:10 -0300 Subject: [PATCH] Add Sendgrid mailer for real email integration Related to #17 Integrate Sendgrid email provider for real email sending. * **Add SendgridMailer class**: Implement the `Mailer` interface, use Sendgrid API to send real emails, and add a constructor to accept Sendgrid API key. * **Update application.yml**: Add Sendgrid API configuration and `sendgrid.api.key` property to store the Sendgrid API key. * **Modify FakeMailer class**: Remove the `@Primary` annotation and add a `@Profile("dev")` annotation to use this class only in the development environment. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/henriqueln7/api-mercadolivrev2/issues/17?shareId=XXXX-XXXX-XXXX-XXXX). --- .../apimlv2/shared/mail/FakeMailer.java | 4 +- .../apimlv2/shared/mail/SendgridMailer.java | 47 +++++++++++++++++++ src/main/resources/application.yml | 6 ++- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/mercadolivre/apimlv2/shared/mail/SendgridMailer.java diff --git a/src/main/java/com/mercadolivre/apimlv2/shared/mail/FakeMailer.java b/src/main/java/com/mercadolivre/apimlv2/shared/mail/FakeMailer.java index dbfd997..be11954 100644 --- a/src/main/java/com/mercadolivre/apimlv2/shared/mail/FakeMailer.java +++ b/src/main/java/com/mercadolivre/apimlv2/shared/mail/FakeMailer.java @@ -1,12 +1,12 @@ package com.mercadolivre.apimlv2.shared.mail; -import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import java.text.MessageFormat; @Component -@Primary +@Profile("dev") public class FakeMailer implements Mailer { @Override public void sendText(String to, String subject, String body) { diff --git a/src/main/java/com/mercadolivre/apimlv2/shared/mail/SendgridMailer.java b/src/main/java/com/mercadolivre/apimlv2/shared/mail/SendgridMailer.java new file mode 100644 index 0000000..276815f --- /dev/null +++ b/src/main/java/com/mercadolivre/apimlv2/shared/mail/SendgridMailer.java @@ -0,0 +1,47 @@ +package com.mercadolivre.apimlv2.shared.mail; + +import com.sendgrid.Method; +import com.sendgrid.Request; +import com.sendgrid.SendGrid; +import com.sendgrid.helpers.mail.Mail; +import com.sendgrid.helpers.mail.objects.Content; +import com.sendgrid.helpers.mail.objects.Email; +import com.sendgrid.helpers.mail.objects.Personalization; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +@Profile("!dev") +public class SendgridMailer implements Mailer { + + private final SendGrid sendGrid; + + public SendgridMailer(@Value("${sendgrid.api.key}") String apiKey) { + this.sendGrid = new SendGrid(apiKey); + } + + @Override + public void sendText(String to, String subject, String body) { + Email from = new Email("no-reply@mercadolivre.com"); + Email toEmail = new Email(to); + Content content = new Content("text/plain", body); + Mail mail = new Mail(from, subject, toEmail, content); + + Personalization personalization = new Personalization(); + personalization.addTo(toEmail); + mail.addPersonalization(personalization); + + Request request = new Request(); + try { + request.setMethod(Method.POST); + request.setEndpoint("mail/send"); + request.setBody(mail.build()); + sendGrid.api(request); + } catch (IOException ex) { + throw new RuntimeException("Failed to send email", ex); + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index cb1ca42..1b01383 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,4 +12,8 @@ spring: accept-case-insensitive-enums: true jwt: secret: jdk02kdadnjndiawd - expTime: 86400 \ No newline at end of file + expTime: 86400 + +sendgrid: + api: + key: YOUR_SENDGRID_API_KEY