Skip to content

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.RootRoot["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: stringto, const from: string | undefinedfrom, const subject: stringsubject, const text: stringtext } =
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: stringsubject: "Received a new email from Membrane!", body: stringbody: ` Program ${const to: stringto} received an email from ${const from: string | undefinedfrom} titled ${const subject: stringsubject} Contents below --- ${const text: stringtext} `, }); };

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()