Sendgrid scala
Documentation
Sending email
This project uses circe to encode emails and decode responses.
Your services can depend on the availability of an email provider like so:
import cats._
import cats.implicits._
import cats.effect._
import com.clovellytech.sendgrid._
import com.clovellytech.sendgrid.providers.sendgrid._
import io.chrisdavenport.log4cats.Logger
class Service[F[_]: Functor: EmailAlgebra] {
def respond(): F[Unit] = {
EmailAlgebra[F].sendEmail(
EmailData.text(
from = Email("Site", "name@yoursite.com"),
to = Email("user name", "user@gmail.com"),
subject = "Registration update",
text = "Thank you for registering"
)
).as(())
}
}
Now all that is required is to instantiate an email provider given your apikey and configuration:
import scala.concurrent.ExecutionContext.Implicits.global
class Server[F[_]: ConcurrentEffect: ContextShift: Timer: Logger] {
val config = SendgridSettings.default("your api key", EmailsConfig(fromName = "Company", fromEmail = "info@example.com"))
SendgridProvider.resource[F](global, config).use{ implicit provider =>
val service = new Service[F]
service.respond()
}
}
Here's a mock email service that just logs the email intent:
import org.http4s.Status
class MockEmail[F[_]: Applicative: Logger] extends EmailAlgebra[F] {
def sendEmail(email: EmailData): F[Status] = {
(
Logger[F].info("sending email") *>
Logger[F].info(email.toString)
).as(Status.Ok)
}
}
There's a hidden instance of Logger for Id defined, so this works and shows our mock output:
import cats._
implicit val mock: EmailAlgebra[Id] = new MockEmail[Id]
// mock: EmailAlgebra[Id] = repl.MdocSession$App$MockEmail@61717e74
val service = new Service[Id]
// service: Service[Id] = repl.MdocSession$App$Service@3f28c5df
service.respond()
// sending email
// EmailData(List(Personalizations(List(Email(user name,user@gmail.com)))),Email(Site,name@yoursite.com),Registration update,List(ContentElement(text/plain,Thank you for registering)))
// res0: Id[Unit] = ()
Now your Service[F]
class is ready to be used with IO inside of an IOApp.
Future work
- Make the project work for other providers (amazon, G suite, mailchimp, self-managed)