PageSourceSearch

https://sidemail.io/assets/BLgTMBXM.js

js sidemail.io collected 2026-09-25 20:13:52 UTC 9,324 bytes, 164 lines download raw bytes

1import{j as e}from"./DJx4IWN9.js";const l={path:"/docs/email-attachments/",title:"Sending files as email attachments"};function d(t){const n={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",ul:"ul",...t.components},{HintInfo:i,MdxImage:s,MultiLanguageCodeBlock:c,MultiLanguageCodeBlockItem:a}=n;return i||r("HintInfo"),s||r("MdxImage"),c||r("MultiLanguageCodeBlock"),a||r("MultiLanguageCodeBlockItem"),e.jsxs(e.Fragment,{children:[e.jsx(n.h1,{children:"Sending files as email attachments"}),`
2`,e.jsx(n.p,{children:"With Sidemail.io, it's easy to send files as email attachments. To attach a file, you'll need to encode it as Base64 and put the encoded file string into the JSON body of the API request. You must also specify the file name that will show up to the recipient of the email."}),`
3`,e.jsx(i,{children:e.jsx(n.p,{children:"While attachments are supported, we recommend sending a file link instead. When sending file as email attachments, take into consideration longer deliverity times as the attached files makes the email larger and therefore slower to send, receive and process. There may be also rare cases where emails end up in spam folder due to file attachments."})}),`
4`,e.jsxs(c,{children:[e.jsx(a,{language:"javascript",children:e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-javascript",children:`const fs = require("fs");\r
5const pdfBuffer = fs.readFileSync("./invoice.pdf");\r
6const attachment = sidemail.fileToAttachment("invoice.pdf", pdfBuffer);\r
7\r
8await sidemail.sendEmail({\r
9    toAddress: "[email protected]",\r
10    fromAddress: "[email protected]",\r
11    subject: "Invoice",\r
12    text: "Invoice attached.",\r
13    attachments: [attachment],\r
14});
15`})})}),e.jsx(a,{language:"ruby",children:e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-ruby",children:`require "sidemail"\r
16\r
17file_content = File.read("invoice.pdf")\r
18attachment = Sidemail.file_to_attachment("invoice.pdf", file_content)\r
19\r
20sm = Sidemail.new(api_key: "replace-with-your-api-key")\r
21sm.send_email(\r
22    toAddress: "[email protected]",\r
23    fromAddress: "[email protected]",\r
24    subject: "Invoice",\r
25    text: "Invoice attached.",\r
26    attachments: [attachment]\r
27)
28`})})}),e.jsx(a,{language:"python",children:e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-python",children:`with open("invoice.pdf", "rb") as f:\r
29    attachment = Sidemail.file_to_attachment("invoice.pdf", f.read())\r
30\r
31sm.send_email(\r
32    toAddress="[email protected]",\r
33    fromAddress="[email protected]",\r
34    subject="Invoice",\r
35    text="Invoice attached.",\r
36    attachments=[attachment],\r
37)
38`})})}),e.jsx(a,{language:"php",children:e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-php",children:`$pdfData = file_get_contents('./invoice.pdf');\r
39$attachment = Sidemail::fileToAttachment('invoice.pdf', $pdfData);\r
40\r
41$sidemail->sendEmail([\r
42    'toAddress'   => '[email protected]',\r
43    'fromAddress' => '[email protected]',\r
44    'subject'     => 'Invoice',\r
45    'text'        => 'Invoice attached.',\r
46    'attachments' => [$attachment],\r
47]);
48`})})}),e.jsx(a,{language:"curl",children:e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-bash",children:`curl -X POST https://api.sidemail.io/v1/email/send \\\r
49    -H "Content-Type: application/json" \\\r
50    -H "Authorization: Bearer replace-with-your-api-key" \\\r
51    -d '{\r
52        "toAddress": "[email protected]",\r
53        "fromAddress": "[email protected]",\r
54        "subject": "Invoice",\r
55        "text": "Invoice attached.",\r
56        "attachments": [ { "name": "file.txt", "content": "dmFsaWQgY29udGVudA==" } ]\r
57    }'
58`})})})]}),`
59`,e.jsx(n.h2,{children:"Limitations"}),`
60`,e.jsxs(n.ul,{children:[`
61`,e.jsxs(n.li,{children:["The maximum size of all encoded attachments combined must be less than ",e.jsx(n.code,{children:"5242880"})," characters long (5 MB)."]}),`
62`,e.jsxs(n.li,{children:["Following file types are allowed: ",e.jsx(n.code,{children:".jpg"})," ",e.jsx(n.code,{children:".jpeg"})," ",e.jsx(n.code,{children:".pdf"})," ",e.jsx(n.code,{children:".csv"})," ",e.jsx(n.code,{children:".html"}
62)," ",e.jsx(n.code,{children:".png"})," ",e.jsx(n.code,{children:".gif"})," ",e.jsx(n.code,{children:".json"})," ",e.jsx(n.code,{children:".txt"})," ",e.jsx(n.code,{children:".docx"})," ",e.jsx(n.code,{children:".xlsx"})," ",e.jsx(n.code,{children:".pptx"}),"."]}),`
63`]}),`
64`,e.jsxs(n.p,{children:["If you need another file type allowed or have related requests: ",e.jsx(n.a,{href:"/contact/",children:"contact us"}),"."]}),`
65`,e.jsx(n.h2,{children:"Dashboard preview"}),`
66`,e.jsx(n.p,{children:"You can see historically sent emails and their attachments (and download them) in your Sidemail.io dashboard (by clicking on an email in your project's sending history)."}),`
67`,e.jsx(n.p,{children:e.jsx(s,{src:"./attachments-dashboard.jpg",alt:"Attachment preview in Sidemail.io dashboard"})}),`
68`,e.jsx(n.h2,{children:"More examples"}),`
69`,e.jsx(n.h3,{children:"Send email with an attachment"}),`
70`,e.jsxs(n.p,{children:["To send an email with attachment the JSON data in the API request should include the attachments ",e.jsx(n.code,{children:"array"})," with an attachment ",e.jsx(n.code,{children:"object"})," which must include name and Base64 content. For more details, read the send email ",e.jsx(n.a,{href:"/docs/api/email/",children:"API reference"}),"."]}),`
71`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-javascript",children:`{\r
72	"toAddress": "[email protected]",\r
73	"fromAddress": "[email protected]",\r
74	"fromName": "Your app name",\r
75	"templateName": "Invoice",\r
76	"attachments": [\r
77        {\r
78            "name": "file.txt",\r
79            "content": "dmFsaWQgY29udGVudA=="\r
80        },\r
81    ]\r
82}
83`})}),`
84`,e.jsx(n.h3,{children:"Send email with multiple attachments"}),`
85`,e.jsx(n.p,{children:"Note that attachment content in the following example is just for showcase and it will result in broken files if you try to send it during testing."}),`
86`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-javascript",children:`{\r
87	"toAddress": "[email protected]",\r
88	"fromAddress": "[email protected]",\r
89	"fromName": "Your app name",\r
90	"templateName": "Invoice",\r
91	"attachments": [\r
92        {\r
93            "name": "text-file.txt",\r
94            "content": "dmFsaWQgY29udGVudA=="\r
95        },\r
96		{\r
97            "name": "image-file.jpg",\r
98            "content": "dmFsaWQgY29udGVudA=="\r
99        },\r
100        {\r
101            "name": "document.pdf",\r
102            "content": "dmFsaWQgY29udGVudA=="\r
103        },\r
104    ]\r
105}
106`})}),`
107`,e.jsx(n.h3,{children:"Send inline image in custom HTML"}),`
108`,e.jsxs(n.p,{children:["To embed an image inside custom HTML, reference it with a ",e.jsx(n.code,{children:"cid:"})," URL and set the same ",e.jsx(n.code,{children:"cid"})," value on the attachment. The ",e.jsx(n.code,{children:"cid"})," value should not include angle brackets."]}),`
109`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-javascript",children:`{\r
110	"toAddress": "[email protected]",\r
111	"fromAddress": "[email protected]",\r
112	"fromName": "Your app name",\r
113	"subject": "Inline image example",\r
114	"html": "<p>Hello!</p><img src=\\"cid:logo\\">",\r
115	"attachments": [\r
116        {\r
117            "name": "logo.png",\r
118            "content": "dmFsaWQgY29udGVudA==",\r
119            "cid": "logo"\r
120        },\r
121    ]\r
122}
123`})}),`
124`,e.jsx(n.h3,{children:"Fetch image from URL and send it as email attachment"}),`
125`,e.jsx(n.p,{children:"Fetch random image from Unsplash and send it as email attachment with Node.js."}),`
126`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-js",children:`const configureSidemail = require("sidemail");\r
127const sidemail = configureSidemail({ apiKey: "replace-with-your-api-key" });\r
128const imgBuffer = await (await fetch("https://source.unsplash.com/random")).buffer();\r
129 \r
130const response = await sidemail.sendEmail({\r
131	toAddress: "[email protected]",\r
132	fromAddress: "[email protected]",\r
133	fromName: "Your app name",\r
134	templateName: "Invoice",\r
135	attachments: [\r
136        {\r
137            name: "unsplash-random-image.jpg",\r
138            content: imgBuffer.toString("base64"),\r
139        },\r
140    ]\r
141});
142`})}),`
143`,e.jsx(n.h3,{children:"Send local file as email attachment"}),`
144`,e.jsx(n.p,{children:"Read local file from disk and send it as email attachment with Node.js."}),`
145`,e.jsx(n.pre,{children:e.jsx(n.code,{className:"language-js",children:`const fs = require('fs/promises');\r
146const path = require("path");\r
147const configureSidemail = require("sidemail");\r
148const sidemail = configureSidemail({ apiKey: "replace-with-your-api-key" });\r
149const filePath = path.resolve(__dirname, "./invoice.pdf");\r
150const encodedFile = await fs.readFile(filePath, { encoding: "base64" });\r
151 \r
152const response = await sidemail.sendEmail({\r
153	toAddress: "[email protected]",\r
154	fromAddress: "[email protected]",\r
155	fromName: "Your app name",\r
156	templateName: "Invoice",\r
157	attachments: [\r
158        {\r
159            name: "invoice.pdf",\r
160            content: encodedFile,\r
161        },\r
162    ]\r
163});
164`})})]})}function m(t={}){const{wrapper:n}=t.components||{};return n?e.jsx(n,{...t,children:e.jsx(d,{...t})}):d(t)}function r(t,n){throw new Error("Expected component `"+t+"` to be defined: you likely forgot to import, pass, or provide it.")}export{m as default,l as meta};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.