We are in need of sending emails from Drupal through to Sendgrid. As far as we can see, there is no method as yet for doing this, so was looking at ways it could be implemented.

At the moment, a useful hook to add into sendgrid_integration to do this could be something like:

diff --git a/sites/all/modules/contrib/sendgrid_integration/inc/sendgrid.mail.inc b/sites/all/modules/contrib/sendgrid_integration/inc/sendgrid.mail.inc
index 078066a..6aa0c1f 100644
--- a/sites/all/modules/contrib/sendgrid_integration/inc/sendgrid.mail.inc
+++ b/sites/all/modules/contrib/sendgrid_integration/inc/sendgrid.mail.inc
@@ -131,6 +131,9 @@ class SendGridMailSystem implements MailSystemInterface {
       }
     }
 
+    // Allow other modules to modify the message, this was specifically added for templates
+    drupal_alter('sendgrid_integration_message', $sendgrid_message, $message);
+
     //Add cc and bcc in mail if they exist.
     $cc_bcc_keys = ['cc', 'bcc'];
     $address_cc_bcc = [];

After this, I am looking at mappings to map the template_placeholders, and allow us to pass Drupal variables through for these to use. It may require some Drupal dev for each email, but it should alleviate some of the repetitive work.

If there is already anything that could help with this though, or approaches people have used, please let me know and I can take into consideration. Otherwise, I will go down a route similar to the Mandrill modules where it creates a new mapping entity to map templates to mailKeys.

Dan

Comments

retrodans created an issue. See original summary.

retrodans’s picture

For this, it would also be good to dynamically pull in the templates so you don't have to copy/paste them in. To do this, it would be good to get this method added into the code brought in by composer. (note, the below is a copy of the POST with a simple replacement to GET, nothing more)

  /**
   * Makes the actual HTTP request to SendGrid using Guzzle. The form is an
   * array of ready options for SendGrid email.
   *
   * @param string $endpoint
   * @param array $form
   * @return bool|\SendGrid\Response
   */
  public function getRequest($endpoint, $form) {
    $requestoptions = [];
    if (array_key_exists('files', $form)) {
      // If the email contains files we must process as multipart.
      $requestoptions['multipart'] = $this->prepareMultipart($form);
    }
    else {
      $requestoptions['form_params'] = $form;
    }

    // Allow for contection timeout.
    if (isset($this->options['connect_timeout'])) {
      $requestoptions['connect_timeout'] = $this->options['connect_timeout'];
    }

    // Allow for request timeout.
    if (isset($this->options['timeout'])) {
      $requestoptions['timeout'] = $this->options['timeout'];
    }

    try {
      $res = $this->client->request('GET', $endpoint, $requestoptions);
    }
    catch (GuzzleHttp\Exception\ClientException $e) {
      echo 'Sendgrid API has experienced and error completing your request.';
      echo '<pre>';
      var_dump($e);
      echo '</pre>';
      return FALSE;
    }
    $response = new \SendGrid\Response($res->getStatusCode(), $res->getHeaders(), $res->getBody(TRUE), json_decode($res->getBody(TRUE)));

    return $response;
  }
Perignon’s picture

Status: Active » Postponed (maintainer needs more info)
Related issues: +#2696753: Support SendGrid's hosted templates

I am confused by your first statement, because this module does exactly that. This module allows for Drupal to send emails through Sendgrid.

In regards to templates, there is an outstanding issue to support SendGrid's templating system. Other than that, there are other modules that can be used to template emails, such as the HTMLmail module.

retrodans’s picture

The requirement we have at the moment is for Drupal to pass variables to sendgrid, including the sendgrid template to render. It sounds like the outstanding issue on templates is basically what I am developing right now, so wonder if it is something worth sharing, as would at least create a consistent and relatively simple approach to using the Sendgrid templates.

Perignon’s picture

Always willing to take contributions!

Perignon’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)