View Source Swoosh.Attachment (Swoosh v1.16.4)

Struct representing an attachment in an email.

Usage

For all usecases of new/2 see the function documentation.

Inline Example

new()
|> to({avenger.name, avenger.email})
|> from({"Red Skull", "red_skull@villains.org"})
|> subject("End Game invitation QR Code")
|> html_body(~s|<h1>Hello #{avenger.name}</h1> Here is your QR Code <img src="cid:qrcode.png">|)
|> text_body("Hello #{avenger.name}. Please find your QR Code attached.\n")
|> attachment(
  Swoosh.Attachment.new(
    {:data, invitation_qr_code_binary},
    filename: "qrcode.png",
    content_type: "image/png",
    type: :inline)
)
|> VillainMailer.deliver()

Summary

Types

@type content_encoding() :: :raw | :base64
@type t() :: %Swoosh.Attachment{
  cid: String.t() | nil,
  content_type: String.t(),
  data: binary() | nil,
  filename: String.t(),
  headers: [{String.t(), String.t()}],
  path: String.t() | nil,
  type: :inline | :attachment
}

Functions

Link to this function

get_content(attachment, encoding \\ :raw)

View Source
@spec get_content(
  %Swoosh.Attachment{
    cid: term(),
    content_type: term(),
    data: term(),
    filename: term(),
    headers: term(),
    path: term(),
    type: term()
  },
  content_encoding()
) :: binary() | no_return()
@spec new(binary() | struct() | {:data, binary()}, Keyword.t() | map()) ::
  %Swoosh.Attachment{
    cid: term(),
    content_type: term(),
    data: term(),
    filename: term(),
    headers: term(),
    path: term(),
    type: term()
  }

Creates a new Attachment

Examples:

Attachment.new("/path/to/attachment.png")
Attachment.new("/path/to/attachment.png", filename: "image.png")
Attachment.new("/path/to/attachment.png", filename: "image.png", content_type: "image/png")
Attachment.new(params["file"]) # Where params["file"] is a %Plug.Upload
Attachment.new({:data, File.read!("/path/to/attachment.png")}, filename: "image.png", content_type: "image/png")

Examples with inline-attachments:

Attachment.new("/path/to/attachment.png", type: :inline)
Attachment.new("/path/to/attachment.png", filename: "image.png", type: :inline)
Attachment.new("/path/to/attachment.png", filename: "image.png", content_type: "image/png", type: :inline)
Attachment.new(params["file"], type: :inline) # Where params["file"] is a %Plug.Upload
Attachment.new({:data, File.read!("/path/to/attachment.png")}, filename: "image.png", content_type: "image/png", type: :inline)

Inline attachments by default use their filename (or basename of the path if filename is not specified) as cid, in relevant adapters.

Attachment.new("/data/file.png", type: :inline)

Gives you something like this:

<img src="cid:file.png">

You can optionally override this default by passing in the cid option:

Attachment.new("/data/file.png", type: :inline, cid: "custom-cid")