Bijlagen toevoegen aan e-mails

Je kunt bestanden als bijlage meesturen via de Wesender API. Bijlagen worden als Base64-encoded string aangeleverd in de attachments-array.

Hoe werkt het?

  • Converteer je bestand naar een Base64-string
  • Geef de bestandsnaam, Base64-content en MIME-type op
  • Voeg de bijlage toe aan de attachments-array in de API-aanroep

Ondersteunde bestandstypen

Bestandstype Content-Type
PDF application/pdf
JPEG image/jpeg
PNG image/png
Excel (.xlsx) application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Word (.docx) application/vnd.openxmlformats-officedocument.wordprocessingml.document
CSV text/csv
ZIP application/zip

Limieten

  • Maximale grootte per bijlage: 10 MB
  • Maximale totale grootte per e-mail: 20 MB
  • Voor grotere bestanden: gebruik een downloadlink in de e-mailtekst

Bestand als bijlage (Node.js)

import { readFileSync } from "fs"

const pdfBase64 = readFileSync("./factuur.pdf").toString("base64")

await fetch("https://api.wesender.nl/emails", {
  method:  "POST",
  headers: {
    "Authorization": `Bearer ${process.env.WESENDER_API_KEY}`,
    "Content-Type":  "application/json",
  },
  body: JSON.stringify({
    from:    "noreply@mijndomein.nl",
    to:      ["klant@voorbeeld.nl"],
    subject: "Je factuur van juni 2026",
    html:    "<p>In de bijlage vind je je factuur. Bij vragen kun je altijd contact met ons opnemen.</p>",
    attachments: [
      {
        filename:     "factuur-2026-06.pdf",
        content:      pdfBase64,
        content_type: "application/pdf",
      },
    ],
  }),
})

Meerdere bijlagen tegelijk

const attachments = [
  {
    filename:     "factuur.pdf",
    content:      readFileSync("./factuur.pdf").toString("base64"),
    content_type: "application/pdf",
  },
  {
    filename:     "specificatie.xlsx",
    content:      readFileSync("./specificatie.xlsx").toString("base64"),
    content_type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  },
]

await fetch("https://api.wesender.nl/emails", {
  method:  "POST",
  headers: {
    "Authorization": `Bearer ${process.env.WESENDER_API_KEY}`,
    "Content-Type":  "application/json",
  },
  body: JSON.stringify({
    from: "noreply@mijndomein.nl",
    to:   ["klant@voorbeeld.nl"],
    subject: "Factuur + specificatie",
    html:    "<p>Zie bijlage voor je factuur en gedetailleerde specificatie.</p>",
    attachments,
  }),
})

Dynamisch PDF genereren (Node.js met pdfkit)

Genereer een PDF in het geheugen en stuur het direct mee zonder tussenopslag op schijf:

import PDFDocument from "pdfkit"

function genereerFactuurPdf(orderId: string, bedrag: number): Promise<Buffer> {
  return new Promise((resolve) => {
    const doc    = new PDFDocument({ margin: 50 })
    const chunks: Buffer[] = []

    doc.on("data", chunk => chunks.push(chunk))
    doc.on("end",  ()    => resolve(Buffer.concat(chunks)))

    doc.fontSize(20).text("Factuur", { align: "center" })
    doc.moveDown()
    doc.fontSize(12)
       .text(`Ordernummer: ${orderId}`)
       .text(`Bedrag: €${bedrag.toFixed(2)}`)
       .text(`Datum: ${new Date().toLocaleDateString("nl-NL")}`)

    doc.end()
  })
}

// Gebruik:
const pdfBuffer = await genereerFactuurPdf("INV-2026-001", 149.95)
await fetch("https://api.wesender.nl/emails", {
  // ...
  body: JSON.stringify({
    // ...
    attachments: [{
      filename:     "factuur.pdf",
      content:      pdfBuffer.toString("base64"),
      content_type: "application/pdf",
    }],
  }),
})

Foutoplossing

  • 413 Request Too Large — de bijlage overschrijdt de maximale bestandsgrootte van 10 MB; gebruik een downloadlink
  • Bijlage beschadigd — zorg dat je een correcte Base64-encoding gebruikt zonder extra witruimte of regeleinden
  • Verkeerde MIME-type — gebruik het juiste content_type voor je bestandstype (zie tabel hierboven)