postmark-scala

Scala binding for the Postmark API

License

License

MIT
Categories

Categories

Scala Languages
GroupId

GroupId

com.github.sebrichards
ArtifactId

ArtifactId

postmark-scala_2.9.1-1
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

postmark-scala
Scala binding for the Postmark API
Project URL

Project URL

https://github.com/sebrichards/postmark-scala
Project Organization

Project Organization

com.github.sebrichards
Source Code Management

Source Code Management

https://github.com/sebrichards/postmark-scala

Download postmark-scala_2.9.1-1

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.sebrichards/postmark-scala_2.9.1-1/ -->
<dependency>
    <groupId>com.github.sebrichards</groupId>
    <artifactId>postmark-scala_2.9.1-1</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.sebrichards/postmark-scala_2.9.1-1/
implementation 'com.github.sebrichards:postmark-scala_2.9.1-1:1.1'
// https://jarcasting.com/artifacts/com.github.sebrichards/postmark-scala_2.9.1-1/
implementation ("com.github.sebrichards:postmark-scala_2.9.1-1:1.1")
'com.github.sebrichards:postmark-scala_2.9.1-1:jar:1.1'
<dependency org="com.github.sebrichards" name="postmark-scala_2.9.1-1" rev="1.1">
  <artifact name="postmark-scala_2.9.1-1" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.sebrichards', module='postmark-scala_2.9.1-1', version='1.1')
)
libraryDependencies += "com.github.sebrichards" % "postmark-scala_2.9.1-1" % "1.1"
[com.github.sebrichards/postmark-scala_2.9.1-1 "1.1"]

Dependencies

compile (8)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.9.1-1
org.apache.httpcomponents : httpclient jar 4.2.5
commons-io : commons-io jar 2.2
org.json4s : json4s-core_2.9.1-1 jar 3.2.5
org.json4s : json4s-jackson_2.9.1-1 jar 3.2.5
joda-time : joda-time jar 2.3
org.joda : joda-convert jar 1.4
org.slf4j : slf4j-api jar 1.7.5

test (1)

Group / Artifact Type Version
org.specs2 : specs2_2.9.1-1 jar 1.12.4

Project Modules

There are no modules declared in this project.

postmark-scala

This library provides a scala interface to the Postmark API.

Inspiration has been taken from https://github.com/jaredholdcroft/postmark-java, so thanks to Jared for that.

Please note that support for Scala 2.9.x was dropped at version 1.1.1.

Usage

Add the dependency to SBT:

libraryDependencies += "com.github.sebrichards" %% "postmark-scala" % "1.3"

The PostmarkClient is simple enough to use:

import com.github.sebrichards.postmark.Attachment
import com.github.sebrichards.postmark.NameValueMap
import com.github.sebrichards.postmark.PostmarkClient
import com.github.sebrichards.postmark.PostmarkError
import com.github.sebrichards.postmark.PostmarkMessage
import com.github.sebrichards.postmark.PostmarkSuccess

import java.io.File

import org.apache.commons.codec.binary.Base64

val client = new PostmarkClient("POSTMARK_API_TEST")

val message = PostmarkMessage(

  // Required fields
  To = "Recipient <[email protected]>",
  From = "Postmark Sender <[email protected]>",
  Subject = "Test E-Mail",
  TextBody = Some("Hello world"),
  HtmlBody = Some("<p>Hello world</p>"),

  // Optional mail fields
  Cc = Some("Another Recipient <[email protected]>"),
  Bcc = Some("[email protected]"),
  ReplyTo = Some("[email protected]"),

  // Optional attachments
  Attachments = List(
    Attachment("Text File.txt", "text/plain", Base64.encodeBase64String("Hello world".getBytes)),
    Attachment(new File("picture.jpg"))
  ),

  // Optional Postmark fields
  Tag = Some("My Tag"),
  Headers = List(
    NameValueMap("key", "value"),
    NameValueMap("key2", "value2")
  ),
  TrackOpens = true

)

val result: Either[PostmarkError, PostmarkSuccess] = client.send(message)

client.destroy

Alternatively, you can use the PostmarkAutoRetryClient to automatically retry when there's an error at Postmark's end. Note that the send method will block until either success or eventual failure.

All optional fields default to None/Nil.

Some additional points:

  • You need to add your own flavour of SLF4J implementation as a dependency.
  • If you're finished using the Postmark client, call the destroy method before dereferencing it.

Postmark Responses

The client expects the following HTTP codes from Postmark: 200, 401, 422 and 500.

  • For 200, the provided JSON is parsed into PostmarkSuccess.
  • For 401/422, the provided JSON is parsed into PostmarkError.
  • For 500, a PostmarkError is created using the response body.

Note that when using the PostmarkAutoRetryClient, responses with 401/422 are accepted as valid, thus the request will not be re-attempted. Only responses with 500 are deemed worthy of a retry. If this isn't suitable for you, you can easily extend PostmarkClient with your own approach.

Versions

Version
1.1
1.0