C3 AI Documentation Home

Email Integration

Integrating an external email service provider into your application is crucial for automating communication processes, such as sending notifications, alerts, and validation emails. This topic walks you through the process of configuring integration with an email provider, using SendGrid as an example, and highlights key functionalities like sending emails and validating email addresses. SendGrid is a cloud-based service that assists in sending emails.

Email integration essentials

Email integration typically revolves around two primary API functions:

  1. send: Enables the application to send emails through the configured email provider.
  2. isValidEmail: Offers real-time, detailed verification of email addresses' validity. Note that some providers may necessitate a dedicated API for this functionality.

Sending an email

Sending an email involves crafting the email message with essential components like recipients, sender, subject, and body.

Here's a basic example:

JavaScript
var recipients = [];
var sender = "<sender email address>";
var subject = "<subject string>";
var emailBody = "<email body>";

// Construct the email message
var mail = Mail.make({
    from: sender, 
    to: recipients, 
    subject: subject,
    content: emailBody,
});

// Send the mail  
Mail.send(mail);

Refer to the Mail Type for detailed information on the Mail entity and its capabilities.

You can use the Mail Type for sending email messages. This Type includes functionalities to send the email, validate email addresses, and format email addresses with optional names.

The Mail Type specifies the structure and attributes for crafting an email, including sender (from), recipients (to, cc, bcc), subject, content, and attachments. It supports both string and Member Types for recipient fields, allowing dynamic resolution of email addresses. It also incorporates a simple templating mechanism for email content and subject lines, utilizing string substitution.

Message size limitation

The total message size, including the message itself, headers, and attachments, should not exceed 20MB.

Configuration requirements

To ensure successful email integration, follow these configuration steps:

Step 1: Specify the default provider and delivery mode

Update MailConfig to set the default email service provider and the delivery mode:

JavaScript
var mailConfig = MailConfig.inst().getConfig(); 
mailConfig.setConfigValue("serviceProvider", SendGrid);
mailConfig.setConfigValue("sendMode", MailSendMode.PROD);

In this example, the email service provider is SendGrid and the MailSendMode is PROD. The PROD setting sends emails live using the configured email service provider.

Step 2: Set provider credentials

Service providers often require API keys for authentication. It's common to need separate keys for sending emails and validating email addresses.

Here's how you can configure both for SendGrid:

JavaScript
// For sending emails
SendGridApi.setApiUrlAndAuth(
    'https://api.sendgrid.com/v3',
    'Bearer <API key>',
);

// For validating email addresses
SendGridEmailAddressValidationApi.setApiUrlAndAuth(
    'https://api.sendgrid.com/v3',
    'Bearer <API key with validation permissions>',
);

Key integrations

  • SendGridApi: Manages sending emails, requiring configuration of the API URL and the authentication token.
  • SendGridEmailAddressValidationApi: Handles email address validation, necessitating its token for operations distinct from sending emails.

Integrating an external email service provider like SendGrid into your system enhances your application's communication capabilities significantly. By following the outlined steps for configuration and leveraging the provided APIs for sending and validating emails, you can ensure efficient and reliable email operations within your application. Remember to adhere to provider-specific requirements, such as API key management and message size limits, to maintain a smooth and effective integration.

Was this page helpful?