Pencil

build

Overview

Pencil is a simple smtp client. The main goal is to be able to send emails in the simplest way.
It is build on top of cats, cats-effect, fs2, scodec

Pencil supports:

  • Text email (ascii)
  • Mime email
  • TLS
  • Authorisation

Usage

Add dependency to your build.sbt

libraryDependencies += "com.minosiants" %% "pencil" % "0.3.0"

Examples how to use it

Create text email

val email = Email.text(
      from"user1@mydomain.tld",
      to"user1@example.com",
      subject"first email",
      Body.Ascii("hello")
)

Create mime email

val email = Email.mime(
     from"user1@mydomain.tld",
     to"user1@example.com",
     subject"привет",
     Body.Utf8("hi there")
) + attachment"path/to/file"

Send email

object Main extends IOApp {

  override def run(args: List[String]): IO[ExitCode] =
    Blocker[IO]
      .use { blocker =>
        SocketGroup[IO](blocker).use { sg =>
          TLSContext.system[IO](blocker).flatMap { tls =>
          val credentials = Credentials(
                              Username("user1@example.com"),
                              Password("12345678")
                            )
          val client = Client[IO]("localhost", 25, Some(credentials))(blocker, sg, tls)
          val result = client.send(email)

          result.attempt
            .map {
              case Right(value) =>
                ExitCode.Success
              case Left(error) =>
                error match {
                  case e: Error => println(e.show)
                  case e: Throwable => println(e.getMessage)
                }
                ExitCode.Error
            }
        }
      }
}

Docker Mailserver

For test purposes Docker Mailserver can be used