Thanks to visit codestin.com
Credit goes to github.com

Skip to content

roman0x58/pencil-mail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central

PencilMail

Fork of https://github.com/minosiants/pencil with added AWS SES support, various fixes, improved tests, various enhancements, and removal of the Apache Tika dependency.

Overview

PencilMail is a simple SMTP client. Its main goal is to send emails in the simplest possible way.
It is built on top of cats, cats-effect, fs2, and scodec.

PencilMail supports:

  • Text emails (ASCII)
  • MIME emails
  • TLS
  • Authentication

Specifications

Usage

Add dependency to your build.sbt

For Scala 3

libraryDependencies += "io.github.roman0x58" %% "pencilmail" % "3.0.5"

Examples

Create text email

val email = Email.text(
      from"user name <[email protected]>",
      to"[email protected]",
      subject"first email",
      Body.Ascii("hello")
)

Create MIME email with attachment

val email = Email.mime(
     from"[email protected]",
     to"[email protected]",
     subject"привет",
     Body.Utf8("hi there")
) + attachment"path/to/file"

Create MIME email with alternative bodies

val email = Email.mime(
     from"[email protected]",
     to"[email protected]",
     subject"привет",
     Body.Alternative(List(Body.Utf8("hi there3"), Body.Ascii("hi there2")))
)

Send email example

object Main extends IOApp {

  val logger = Slf4jLogger.getLogger[IO]

  override def run(args: List[String]): IO[ExitCode] = {
    val credentials = Credentials(
      Username("[email protected]"),
      Password("password")
    )
    val action = for {
      tls <- Network[IO].tlsContext.system
      client = Client[IO](
        address = SocketAddress(host"localhost", port"25"),
        mode = SmtpMode.Plain,
        credentials = Some(credentials),
        tlsContext = tls,
        logger = logger)
      response <- client.send(email)
    } yield response

    action.attempt.flatMap {
      case Right(replies) =>
        logger.debug(replies)
      case Left(error) =>
        error match {
          case e: Error => logger.debug(e.toString)
          case e: Throwable => logger.debug(e.getMessage)
        }
    }.unsafeRunSync()
  }
}

Development

Creating test truststore

keytool -import -trustcacerts -keystore test-truststore.jks -storepass changeit -alias mailpit -file certificate.crt

Running AWS SES integration tests

sbt -Daws.username=username -Daws.password=password -Daws.from=from -Daws.to=to

Docker Mailserver

For testing purposes, Docker Mailserver can be used.

About

Functional SMTP client for Scala with AWS SES support and MIME/TLS/auth enhancements

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 8