Send & receive email
All Membrane programs are capable of sending and receiving emails. Receiving an email works similarly to how HTTP endpoints are handled. All you need to do is export an email
function from the root of your program and use the program’s unique email address.
To get a program’s email address, click “Copy Email Address” above the email
function signature. Or right-click the program name in the Navigator and select “Copy Email Address”.
Basic Example
This program receives an email and uses the email
program to forward the email to the currently signed in user’s inbox. In order to run this program make sure you add email
as a dependency.
import { const nodes: {
readonly email: email.Root;
}
nodes } from "membrane";
// Handler to receive emails
export async function function email({ to, from, subject, text, ...rest }: {
[x: string]: any;
to: any;
from: any;
subject: any;
text: any;
}): Promise<void>
email({ to: any
to, from: any
from, subject: any
subject, text: any
text, ...rest: {
[x: string]: any;
}
rest }) {
// Send yourself an email
await const nodes: {
readonly email: email.Root;
}
nodes.email: email.handles.Root
Note that this node will only be present if the email
program is added as a connection.
email.email.handles.Root.send: (args: {
subject: string;
body: string;
}) => Promise<void>
send({
subject: string
subject: "Received a new email from Membrane!",
body: string
body: `
Program ${to: any
to} received an email from ${from: any
from} titled ${subject: any
subject}
Contents below
---
${text: any
text}
`,
});
}
Attachments
Email handlers support receiving attachments via the attachments
property on the object passed to the function. attachments
is an array of attachment objects with a downloadUrl
and name
. Attachments are automatically deleted from our backend after 30 minutes of being received. Please reach out to contact@membrane.io if you need more time.
You can access attachment data by fetch
’ing from the downloadUrl
and parsing like so:
const { name, downloadUrl } = attachments[0];
const data = await fetch(downloadUrl);
const buffer = await data.bytes(); // OR: await data.arrayBuffer()