/pencil

Simple smtp client

Primary LanguageScalaApache License 2.0Apache-2.0

Pencil

Join the chat at https://gitter.im/minosiants/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
  • Authentication

Specifications

Usage

Add dependency to your build.sbt

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

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