Send Emails from Your Application
Send Emails from Your Application
Section titled “Send Emails from Your Application”In this tutorial, you’ll send emails from your application using a configured mail server.
You’ll build and send an email dynamically, including recipients, subject, content, and optional attachments.
This pattern is commonly used in workflows to send notifications, confirmations, or requests.
What you’ll build
Section titled “What you’ll build”By the end of this tutorial, you will:
- Configure email sending using a mail server
- Build an email object dynamically
- Send emails from your application
- Control recipients, subject, and content
- Integrate email sending into workflows
Prerequisites
Section titled “Prerequisites”- A configured mail server connection (e.g.
emailConfig) - Access to a workflow or script where the email will be sent
- Basic understanding of objects and functions
Step 1: Use your mail configuration
Section titled “Step 1: Use your mail configuration”Ensure you have a mail configuration set up (from the previous tutorial).
This configuration contains:
- Mail server host
- Credentials
- Default sender
Step 2: Build the email object
Section titled “Step 2: Build the email object”Create an email object with the required fields:
const mail = { to: "recipient@example.com", subject: "Your request has been received", html: "<p>Thank you for your request.</p>"};You can also include:
ccbccreplyToattachments
Step 3: Send the email
Section titled “Step 3: Send the email”Use the built-in mail function to send the email:
await sys.mail.sendMail($config.emailConfig, mail);This sends the email using your configured mail server.
Step 4: Send dynamic emails
Section titled “Step 4: Send dynamic emails”You can generate content dynamically:
const mail = { to: emailRecord.from, subject: `Request received: ${emailRecord.subject}`, html: `<p>We received your request and will process it shortly.</p>`};This allows you to:
- Personalize emails
- Use data from workflows
- Generate context-aware responses
Step 5: Add attachments (optional)
Section titled “Step 5: Add attachments (optional)”Attach files to your email:
const mail = { to: "recipient@example.com", subject: "Document attached", html: "<p>Please see attached.</p>", attachments: [ { fileName: "document.pdf", data: fileBuffer } ]};Step 6: Use mock recipients (recommended for testing)
Section titled “Step 6: Use mock recipients (recommended for testing)”To prevent sending real emails during development, enable mock recipients in your configuration.
This ensures all emails are redirected to test addresses.
Result
Section titled “Result”You can now send emails from your application.
This allows you to:
- Notify users
- Respond to incoming requests
- Integrate communication into workflows
What’s happening
Section titled “What’s happening”The system uses your configured mail server to send emails.
When sendMail is called:
- The mail object is constructed
- The mail server connection is used
- The email is sent to the specified recipients
Next steps
Section titled “Next steps”- Combine with incoming email processing
- Send workflow notifications
- Generate email content using AI
- Build automated communication flows
Related guides
Section titled “Related guides”- Sending Emails
- Email Triggers
- Workflow Fundamentals