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, right click the program name from the Membrane Navigator home and select “Copy Email Address” from the context menu.
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 const const email: (args: {
id: string;
to: string;
from?: string;
cc?: string;
subject: string;
html: string;
text: string;
replyText?: string;
replyTo?: string;
inReplyTo?: string;
attachments?: Json;
}) => void
email: resolvers.interface resolvers.Root
Root["email"] = (args: {
id: string;
to: string;
from?: string;
cc?: string;
subject: string;
html: string;
text: string;
replyText?: string;
replyTo?: string;
inReplyTo?: string;
attachments?: any;
}
args) => {
const { const to: string
to, const from: string | undefined
from, const subject: string
subject, const text: string
text } = args: {
id: string;
to: string;
from?: string;
cc?: string;
subject: string;
html: string;
text: string;
replyText?: string;
replyTo?: string;
inReplyTo?: string;
attachments?: any;
}
args;
// 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 ${const to: string
to} received an email from ${const from: string | undefined
from} titled ${const subject: string
subject}
Contents below
---
${const text: string
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()