1. Documentation
  2. Application (WaaS)

Implementing Email Functionality

This documentation outlines the steps for creating Mailgun domains for tenants. This enables your customers to send emails using their own domains, enhancing the professionalism and credibility of their communication.

Understanding wildcloud's email capabilities: It's important to note that wildcloud does not provide email services as part of its hosting offerings. This means that if you wish to integrate email functionality into your applications or websites hosted on wildcloud, you'll need to rely on external, third-party email services.

Integrating Third-Party Email Services For those looking to incorporate email capabilities into their wildcloud applications, leveraging a third-party service is a practical solution. One such service is Mailgun. Mailgun offers reliable email delivery and management features, making it an excellent choice for handling email communications.

Example of Automation with Mailgun To give you an idea of how this integration and automation can be implemented, this guide provides a detailed walkthrough of setting up Mailgun domains for your tenants. This process involves using specific plugins and API interactions to automate the configuration and management of email domains, ensuring a smooth and user-friendly experience for both you as the administrator and your tenants.

Let's dive in! 🚀

Requirements for Integration

  • Code Snippets Plugin: Essential for leveraging the tenant lifecycle hooks functionality.
  • WP Mail SMTP by WPForms: Integrates tenant sites with Mailgun for email services.
  • Mailgun Account: A Mailgun account is necessary for configuring the WP Mail SMTP plugin.

Configuring WP Mail SMTP Plugin

  1. Installation and Setup:
    • Install the WP Mail SMTP plugin.
    • Navigate to the plugin's settings and configure it to enable sending emails using your domain, following the on-screen instructions.

Creating Mailgun Domains for New Tenants

  1. Preparation:

    • Ensure the Code Snippets plugin is installed and activated.
    • Use the WaaS plugin or API for tenant creation, ensuring each tenant is provisioned under a custom domain rather than a default wildcloud URL.
  2. PHP Snippet for Mailgun Domain Creation:

    • Implement a PHP snippet in your environment. This snippet should:
      • Trigger upon tenant creation or main domain change.
      • Retrieve the current domain, excluding wildcloud-generated domains.
      • Use the Mailgun API to register a new domain.
    • The snippet uses WP Mail SMTP's API key to communicate with Mailgun’s API.
  3. Snippet Execution Process:

    • Upon the creation of a tenant, the snippet reads the API key from WP Mail SMTP.
    • It then executes a call to Mailgun’s API, resulting in the addition of a new domain in your Mailgun account, corresponding to the tenant's domain.

PHP Snippet Code:

// Mailgun domain creation and update upon tenant creation or domain change.
add_action('wpcs_tenant_created', 'create_mailgun_domain');
add_action('wpcs_tenant_main_domain_changed', 'create_mailgun_domain');

function create_mailgun_domain() {
    $current_domain = str_replace(['http://', 'https://'], '', get_site_url());

    if (strpos($current_domain, '.wpcs.io') !== false) {
        return;
    }

    $mailgun_api_key = WPMailSMTP\Options::init()->get('mailgun', 'api_key');
    $auth_string = base64_encode("api:$mailgun_api_key");
    $mailgun_api_url = 'https://api.mailgun.net/v3/domains';

    $mailgun_domain = "mailgun.$current_domain";
    $response = wp_remote_post($mailgun_api_url, [ 
        'body' => ['name' => $mailgun_domain],
        'headers' => ['Authorization' => "Basic $auth_string"],
    ]);

    if (!is_wp_error($response)) {
        update_option('mailgun_domain_information', $response['body']);
        WPMailSMTP\Options::init()->set(['mailgun' => ['domain' => $mailgun_domain]], false, false);
    }
}

Informing Customers About DNS Configuration

  • After the Mailgun domain is created, it's crucial to inform customers about the DNS configuration required for domain validation.
  • Use the data from the wp_remote_post response to guide customers in setting up DNS records.

Displaying DNS Requirements in Tenant Dashboard

  1. Dashboard Widget Integration:

    • Add a second PHP snippet to create a dashboard widget in the admin area of the tenant's site.
    • This widget will display the DNS requirements for Mailgun domain validation.
  2. Widget Display and Functionality:

    • The widget retrieves and displays DNS information stored from the previous Mailgun API interaction.
    • It presents this information in an accessible format, detailing record types, names, and values.

PHP Snippet Code for Dashboard Widget:

add_action('wp_dashboard_setup', 'register_mailgun_domain_info_widget');
  
function register_mailgun_domain_info_widget() {
    wp_add_dashboard_widget('mailgun_domain_info_widget', 'Configure your E-mail', 'display_mailgun_domain_info');
}

function display_mailgun_domain_info() {
    $info = json_decode(get_option('mailgun_domain_information'));
    echo "<p>Welcome! To enable E-mails, add these DNS records:</p><p>";
    foreach ($info->sending_dns_records as $record) {
        echo "<div>Type: <input readonly value=\"{$record->record_type}\" /></div>";
        echo "<div>Name: <input readonly value=\"{$record->name}\" /></div>";
        echo "<div>Value: <input readonly value=\"{$record->value}\" /></div><br />";
    }
    echo "</p>";
}

Conclusion

By following this guide and implementing the provided PHP snippets, you can seamlessly integrate Mailgun domain setup into your multi-tenant WordPress environment. This automation not only enhances the efficiency of email domain configuration but also elevates the overall user experience for your tenants.

Note:

  • Verify compatibility with your current WP Mail SMTP plugin version (this guide uses version 2.6.0 as reference).
  • Enhance the dashboard widget with additional styling and support information to further assist tenants.

For personalized support or specific scenarios, the wildcloud team is available for consultation via our White Glove Services.