All Membrane programs are capable of sending and receiving emails. Similar to
HTTP endpoints, 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
exportasyncfunction
functionemail():Promise<void>
email() {
const {
const to:any
to,
const from:any
from,
const subject:any
subject,
const text:any
text,...
const rest:any
rest } = {} as any
// 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:any
to} received an email from ${
const from:any
from} titled ${
const subject:any
subject}
Contents below
---
${
const 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: