Email

Sending Emails

Your mashlets can send emails individually or in bulk. Messages are queued for sending and can be configured with an optional delay if you would like to send them at a later time. You can send plain text messages or use Foldr’s template editor to send rich HTML messages.

The simplest way to send a message is to use the send method.

send

mash.comms.mail.send(String: recipient, String: subject, String|Dictionary: body, Array: ?attachments = [], Array: ?cc = [], Array? bcc = [], Number: ?delay = null)

Send an email message.

Parameters

recipient

The email address to send the message to.

subject

The subject of the email.

body

The body of the email. By default this will be sent as HTML.

Notes

If you specify a string for the body then the default email format will be HTML. If you wish to send the email as plain text you can provide a dictionary like so { raw: "Message text" }.

attachments (optional)

An array of File objects to send as attachments with the message.

cc (optional)

An array of email addresses to include in the CC field of the message.

bcc (optional)

An array of email addresses to include in the BCC field of the message.

delay (optional)

A number representing a period in seconds to wait before sending the message.

Natural

# Send a message to a single recipient immediately
mash.comms.mail.send("[email protected]", "Hello from mash", "This is a test email from mash")

printline "Message sent"

# Send a plain text message to a single recipient immediately
mash.comms.mail.send("[email protected]", {raw: "Hello from mash in plain text" }, "This is a test email from mash")

printline "Message sent"

# Send a message with an attachment and CC and BCC recipients in an hour
set file to mash.file(138,"attachment.pdf")

mash.comms.mail.send("[email protected]", "Hello from mash", "This is a test email from mash with an attachment", [file], ["[email protected]"], ["[email protected]"], 3600)

printline "Message queued for delivery"

Standard

# Send a message to a single recipient immediately
mash.comms.mail.send("[email protected]", "Hello from mash", "This is a test email from mash")

printline("Message sent")

# Send a plain text message to a single recipient immediately
mash.comms.mail.send("[email protected]", {raw: "Hello from mash in plain text" }, "This is a test email from mash")

printline("Message sent")

# Send a message with an attachment and CC and BCC recipients in an hour
file = mash.file(138,"attachment.pdf")

mash.comms.mail.send("[email protected]", "Hello from mash", "This is a test email from mash with an attachment", [file], ["[email protected]"], ["[email protected]"], 3600)

printline("Message queued for delivery")

Output

"Message sent"
"Message sent"
"Message queued for delivery"

Templates

You can make use of Foldr’s message template designer to create rich HTML templates for your emails. It’s easy to replace values inside the templates and send them using MaSH.

sendWithTemplate

mash.comms.mail.sendWithTemplate(number: template, String: recipient, String: subject, Dictionary: data, Array: ?attachments = [], Array: ?cc = [], Array? bcc = [], Number: ?delay = null)

Send an email message using a template designed with the Foldr template designer.

Parameters

template

The ID of the Foldr template to use for the message.

recipient

The email address to send the message to.

subject

The subject of the email.

data

A dictionary containing key/value pairs representing the values to be used in the template. The keys are the template variables.

attachments (optional)

An array of File objects to send as attachments with the message.

cc (optional)

An array of email addresses to include in the CC field of the message.

bcc (optional)

An array of email addresses to include in the BCC field of the message.

delay (optional)

A number representing a period in seconds to wait before sending the message.

Natural

# The template variables {{name}} and {{occupation}} will be replaced as below
set templateVars to { name: "Grace Hopper", occupation: "Computer Scientist" }

mash.comms.mail.sendWithTemplate(5, "[email protected]", "Hello from mash", templateVars)

printline "Message sent to {{ templateVars.name }}"

Standard

# The template variables {{name}} and {{occupation}} will be replaced as below
templateVars = { name: "Grace Hopper", occupation: "Computer Scientist" }

mash.comms.mail.sendWithTemplate(5, "[email protected]", "Hello from mash", templateVars)

printline("Message sent to {{ templateVars.name }}")

Output

"Message sent"

The Message Object

The message object allows us to construct a message in an object-oriented manner before sending it.

message

mash.comms.mail.message(Dictionary: ?properties)

Create a MaSH message object.

Properties

attachments

attachments: Array  get/set

An array of File objects to be attached to the message.

bcc

bcc: Array  get/set

An array of email addresses to be used in the BCC field when sending the message.

### body

body: Dictionary|String  get/set

The message body.

Notes

If you specify a string for the body then the default email format will be HTML. If you wish to send the email as plain text you can provide a dictionary like so { raw: "Message text" }.

cc

cc: Array  get/set

An array of email addresses to be used in the CC field when sending the message. Note that if you are using a template then this will be ignored.

data

data: Dictionary  get/set

A dictionary containing key/value pairs representing template variables.

recipient

recipient: String  get/set

The email address to send the message to.

subject

subject: String  get/set

The subject field of the email.

template

template: Number  get/set

The ID of the Foldr template to use for the message body.

Natural

set msg to mash.comms.mail.message()

set msg.recipient to "[email protected]"
set msg.subject to "Hello from mash"
set msg.template to 5
# The template variables {{name}} and {{occupation}} will be replaced as below
set msg.data to { name: "Grace Hopper", occupation: "Computer Scientist" }

# Send the message in one hour
mash.comms.mail.sendMessage(msg, 3600)

printline "Message queued for delivery"

Standard

msg = mash.comms.mail.message()

msg.recipient = "[email protected]"
msg.subject = "Hello from mash"
msg.template = 5
# The template variables {{name}} and {{occupation}} will be replaced as below
msg.data = { name: "Grace Hopper", occupation: "Computer Scientist" }

# Send the message in one hour
mash.comms.mail.sendMessage(msg, 3600)

printline("Message queued for delivery")

Output

Message queued for delivery

← All articles