Skip to content

Commit

Permalink
Feature: Add support for sending inline attachments via HTTP API (#399)
Browse files Browse the repository at this point in the history
Optional settings for Attachment ContentID & ContentType
  • Loading branch information
axllent committed Dec 5, 2024
1 parent 22cae16 commit 3e7d4f8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
30 changes: 23 additions & 7 deletions server/apiv1/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,33 @@ type SendRequest struct {
Subject string

// Message body (text)
// example: This is the text body
// example: Mailpit is awesome!
Text string

// Message body (HTML)
// example: <p style="font-family: arial">Mailpit is <b>awesome</b>!</p>
// example: <div style="text-align:center"><p style="font-family: arial; font-size: 24px;">Mailpit is <b>awesome</b>!</p><p><img src="cid:mailpit-logo" /></p></div>
HTML string

// Attachments
Attachments []struct {
// Base64-encoded string of the file content
// required: true
// example: VGhpcyBpcyBhIHBsYWluIHRleHQgYXR0YWNobWVudA==
// example: iVBORw0KGgoAAAANSUhEUgAAAEEAAAA8CAMAAAAOlSdoAAAACXBIWXMAAAHrAAAB6wGM2bZBAAAAS1BMVEVHcEwRfnUkZ2gAt4UsSF8At4UtSV4At4YsSV4At4YsSV8At4YsSV4At4YsSV4sSV4At4YsSV4At4YtSV4At4YsSV4At4YtSV8At4YsUWYNAAAAGHRSTlMAAwoXGiktRE5dbnd7kpOlr7zJ0d3h8PD8PCSRAAACWUlEQVR42pXT4ZaqIBSG4W9rhqQYocG+/ys9Y0Z0Br+x3j8zaxUPewFh65K+7yrIMeIY4MT3wPfEJCidKXEMnLaVkxDiELiMz4WEOAZSFghxBIypCOlKiAMgXfIqTnBgSm8CIQ6BImxEUxEckClVQiHGj4Ba4AQHikAIClwTE9KtIghAhUJwoLkmLnCiAHJLRKgIMsEtVUKbBUIwoAg2C4QgQBE6l4VCnApBgSKYLLApCnCa0+96AEMW2BQcmC+Pr3nfp7o5Exy49gIADcIqUELGfeA+bp93LmAJp8QJoEcN3C7NY3sbVANixMyI0nku20/n5/ZRf3KI2k6JEDWQtxcbdGuAqu3TAXG+/799Oyyas1B1MnMiA+XyxHp9q0PUKGPiRAau1fZbLRZV09wZcT8/gHk8QQAxXn8VgaDqcUmU6O/r28nbVwXAqca2mRNtPAF5+zoP2MeN9Fy4NgC6RfcbgE7XITBRYTtOE3U3C2DVff7pk+PkUxgAbvtnPXJaD6DxulMLwOhPS/M3MQkgg1ZFrIXnmfaZoOfpKiFgzeZD/WuKqQEGrfJYkyWf6vlG3xUgTuscnkNkQsb599q124kdpMUjCa/XARHs1gZymVtGt3wLkiFv8rUgTxitYCex5EVGec0Y9VmoDTFBSQte2TfXGXlf7hbdaUM9Sk7fisEN9qfBBTK+FZcvM9fQSdkl2vj4W2oX/bRogO3XasiNH7R0eW7fgRM834ImTg+Lg6BEnx4vz81rhr+MYPBBQg1v8GndEOrthxaCTxNAOut8WKLGZQl+MPz88Q9tAO/hVuSeqQAAAABJRU5ErkJggg==
Content string
// Filename
// required: true
// example: AttachedFile.txt
// example: mailpit.png
Filename string
// Optional Content Type for the the attachment.
// If this field is not set (or empty) then the content type is automatically detected.
// required: false
// example: image/png
ContentType string
// Optional Content-ID (`cid`) for attachment.
// If this field is set then the file is attached inline.
// required: false
// example: mailpit-logo
ContentID string
}

// Mailpit tags
Expand Down Expand Up @@ -269,9 +279,15 @@ func (d SendRequest) Send(remoteAddr string) (string, error) {
if err != nil {
return "", fmt.Errorf("error decoding base64 attachment \"%s\": %s", a.Filename, err.Error())
}

mimeType := http.DetectContentType(b)
msg = msg.AddAttachment(b, mimeType, a.Filename)
contentType := http.DetectContentType(b)
if a.ContentType != "" {
contentType = a.ContentType
}
if a.ContentID != "" {
msg = msg.AddInline(b, contentType, a.Filename, a.ContentID)
} else {
msg = msg.AddAttachment(b, contentType, a.Filename)
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ func TestAPIv1Send(t *testing.T) {
{
"Content": "VGhpcyBpcyBhIHBsYWluI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l+MPz88Q9tAO/hVuSeqQAAAABJRU5ErkJggg==",
"Filename": "logo.png",
"ContentID": "inline-cid",
"ContentType": "overridden/type"
}
],
"ReplyTo": [
Expand Down Expand Up @@ -294,6 +300,10 @@ func TestAPIv1Send(t *testing.T) {
assertEqual(t, `Tag 1,Tag 2`, strings.Join(msg.Tags, ","), "wrong Tags")
assertEqual(t, 1, len(msg.Attachments), "wrong Attachment count")
assertEqual(t, `Attached File.txt`, msg.Attachments[0].FileName, "wrong Attachment name")
assertEqual(t, `text/plain`, msg.Attachments[0].ContentType, "wrong Content-Type")
assertEqual(t, 1, len(msg.Inline), "wrong inline Attachment count")
assertEqual(t, `logo.png`, msg.Inline[0].FileName, "wrong Attachment name")
assertEqual(t, `overridden/type`, msg.Inline[0].ContentType, "wrong Content-Type")

attachmentBytes, err := clientGet(ts.URL + "/api/v1/message/" + resp.ID + "/part/" + msg.Attachments[0].PartID)
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions server/ui/api/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1634,12 +1634,22 @@
"Content": {
"description": "Base64-encoded string of the file content",
"type": "string",
"example": "VGhpcyBpcyBhIHBsYWluIHRleHQgYXR0YWNobWVudA=="
"example": "iVBORw0KGgoAAAANSUhEUgAAAEEAAAA8CAMAAAAOlSdoAAAACXBIWXMAAAHrAAAB6wGM2bZBAAAAS1BMVEVHcEwRfnUkZ2gAt4UsSF8At4UtSV4At4YsSV4At4YsSV8At4YsSV4At4YsSV4sSV4At4YsSV4At4YtSV4At4YsSV4At4YtSV8At4YsUWYNAAAAGHRSTlMAAwoXGiktRE5dbnd7kpOlr7zJ0d3h8PD8PCSRAAACWUlEQVR42pXT4ZaqIBSG4W9rhqQYocG+/ys9Y0Z0Br+x3j8zaxUPewFh65K+7yrIMeIY4MT3wPfEJCidKXEMnLaVkxDiELiMz4WEOAZSFghxBIypCOlKiAMgXfIqTnBgSm8CIQ6BImxEUxEckClVQiHGj4Ba4AQHikAIClwTE9KtIghAhUJwoLkmLnCiAHJLRKgIMsEtVUKbBUIwoAg2C4QgQBE6l4VCnApBgSKYLLApCnCa0+96AEMW2BQcmC+Pr3nfp7o5Exy49gIADcIqUELGfeA+bp93LmAJp8QJoEcN3C7NY3sbVANixMyI0nku20/n5/ZRf3KI2k6JEDWQtxcbdGuAqu3TAXG+/799Oyyas1B1MnMiA+XyxHp9q0PUKGPiRAau1fZbLRZV09wZcT8/gHk8QQAxXn8VgaDqcUmU6O/r28nbVwXAqca2mRNtPAF5+zoP2MeN9Fy4NgC6RfcbgE7XITBRYTtOE3U3C2DVff7pk+PkUxgAbvtnPXJaD6DxulMLwOhPS/M3MQkgg1ZFrIXnmfaZoOfpKiFgzeZD/WuKqQEGrfJYkyWf6vlG3xUgTuscnkNkQsb599q124kdpMUjCa/XARHs1gZymVtGt3wLkiFv8rUgTxitYCex5EVGec0Y9VmoDTFBSQte2TfXGXlf7hbdaUM9Sk7fisEN9qfBBTK+FZcvM9fQSdkl2vj4W2oX/bRogO3XasiNH7R0eW7fgRM834ImTg+Lg6BEnx4vz81rhr+MYPBBQg1v8GndEOrthxaCTxNAOut8WKLGZQl+MPz88Q9tAO/hVuSeqQAAAABJRU5ErkJggg=="
},
"ContentID": {
"description": "Optional Content-ID (`cid`) for attachment.\nIf this field is set then the file is attached inline.",
"type": "string",
"example": "mailpit-logo"
},
"ContentType": {
"description": "Optional Content Type for the the attachment.\nIf this field is not set (or empty) then the content type is automatically detected.",
"type": "string",
"example": "image/png"
},
"Filename": {
"description": "Filename",
"type": "string",
"example": "AttachedFile.txt"
"example": "mailpit.png"
}
}
}
Expand Down Expand Up @@ -1698,7 +1708,7 @@
"HTML": {
"description": "Message body (HTML)",
"type": "string",
"example": "\u003cp style=\"font-family: arial\"\u003eMailpit is \u003cb\u003eawesome\u003c/b\u003e!\u003c/p\u003e"
"example": "\u003cdiv style=\"text-align:center\"\u003e\u003cp style=\"font-family: arial; font-size: 24px;\"\u003eMailpit is \u003cb\u003eawesome\u003c/b\u003e!\u003c/p\u003e\u003cp\u003e\u003cimg src=\"cid:mailpit-logo\" /\u003e\u003c/p\u003e\u003c/div\u003e"
},
"Headers": {
"description": "Optional headers in {\"key\":\"value\"} format",
Expand Down Expand Up @@ -1751,7 +1761,7 @@
"Text": {
"description": "Message body (text)",
"type": "string",
"example": "This is the text body"
"example": "Mailpit is awesome!"
},
"To": {
"description": "\"To\" recipients",
Expand Down

0 comments on commit 3e7d4f8

Please sign in to comment.