MartinSStewart / send-grid / SendGrid


type ApiKey

A SendGrid API key. In order to send an email you must have one of these (see the readme for how to get one).

apiKey : String -> ApiKey

Create an API key from a raw string (see the readme for how to get one).

textEmail : { subject : String.Nonempty.NonemptyString, content : String.Nonempty.NonemptyString, to : List.Nonempty.Nonempty EmailAddress, nameOfSender : String, emailAddressOfSender : EmailAddress } -> Email

Create an email that only contains plain text.

import Email
import List.Nonempty
import String.Nonempty exposing (NonemptyString)

{-| An email to be sent to a recipient's email address.
-}
email : Email.Email -> SendGrid.Email
email recipient =
    SendGrid.textEmail
        { subject = NonemptyString 'S' "ubject"
        , to = List.Nonempty.fromElement recipient
        , content = NonemptyString 'H' "i!"
        , nameOfSender = "test name"
        , emailAddressOfSender =
            -- this-can-be-anything@test.com
            { localPart = "this-can-be-anything"
            , tags = []
            , domain = "test"
            , tld = [ "com" ]
            }
        }

htmlEmail : { subject : String.Nonempty.NonemptyString, content : Email.Html.Html, to : List.Nonempty.Nonempty EmailAddress, nameOfSender : String, emailAddressOfSender : EmailAddress } -> Email

Create an email that contains html.

import Email
import Email.Html
import List.Nonempty
import String.Nonempty exposing (NonemptyString)

{-| An email to be sent to a recipient's email address.
-}
email : Email.Email -> SendGrid.Email
email recipient =
    SendGrid.htmlEmail
        { subject = NonemptyString 'S' "ubject"
        , to = List.Nonempty.fromElement recipient
        , content =
            Email.Html.div
                []
                [ Email.Html.text "Hi!" ]
        , nameOfSender = "test name"
        , emailAddressOfSender =
            -- this-can-be-anything@test.com
            { localPart = "this-can-be-anything"
            , tags = []
            , domain = "test"
            , tld = [ "com" ]
            }
        }

Note that email clients are quite limited in what html features are supported! To avoid accidentally using html that's unsupported by some email clients, the Email.Html and Email.Html.Attributes modules only define tags and attributes with universal support. You can still use Email.Html.node and Email.Html.Attributes.attribute if you want something that might not be universally supported though.

addCc : List EmailAddress -> Email -> Email

Add a list of CC recipients.

addBcc : List EmailAddress -> Email -> Email

Add a list of BCC recipients.

addAttachments : Dict String { content : Bytes, mimeType : String } -> Email -> Email

Attach files to the email. These will usually appear at the bottom of the email.

import Bytes exposing (Bytes)
import SendGrid

attachPngImage : String -> Bytes -> Email -> Email
attachPngImage name image email =
    SendGrid.addAttachments
        (Dict.fromList
            [ ( name ++ ".png"
              , { content = image
                , mimeType = "image/png"
                }
              )
            ]
        )
        email

If you want to include an image file within the body of your email, use Email.Html.inlinePngImg, Email.Html.inlineJpegImg, or Email.Html.inlineGifImg instead.

sendEmail : (Result Error () -> msg) -> ApiKey -> Email -> Platform.Cmd.Cmd msg

Send an email using the SendGrid API.

sendEmailTask : ApiKey -> Email -> Task Error ()

Send an email using the SendGrid API. This is the task version of sendEmail.


type Email


type Error
    = StatusCode400 (List ErrorMessage)
    | StatusCode401 (List ErrorMessage)
    | StatusCode403 ({ errors : List ErrorMessage403, id : Maybe String })
    | StatusCode413 (List ErrorMessage)
    | UnknownError ({ statusCode : Basics.Int, body : String })
    | NetworkError
    | Timeout
    | BadUrl String

Possible error codes we might get back when trying to send an email. Some are just normal HTTP errors and others are specific to the SendGrid API.


type alias ErrorMessage =
{ field : Maybe String
, message : String
, errorId : Maybe String 
}

The content of a generic SendGrid error.


type alias ErrorMessage403 =
{ message : Maybe String
, field : Maybe String
, help : Maybe String 
}

The content of a 403 status code error.