First, thanks for your work on this module!

I have a site that uses Rules extensively to send emails. Unfortunately, it seems that SendGrid Integration strips out <a>, <strong>, <em>, etc tags...

I have tried Mime Mail (and its Send a HTML email rule) however it seems that it's not compatible with this module (?).

I've also tested Swift Mailer to send via SendGrid SMTP (instead of this module). It does send in HTML format from Rules' Send mail action, but it lacks support for SendGrid categories that I'll need for reporting.

How would I go about sending HTML emails from Rules with SendGrid Integration?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ajmartin created an issue. See original summary.

Perignon’s picture

If the message has the x-html header set in the message array it will be sent as HTML formatted.

I send HTML emails with rules daily but I use HTMLmail module which sets the header.

Also check your mailsystem settings to make sure mail is being processed through your installed modules correctly.

ajmartin’s picture

Thanks for the super quick reply. I'll give HTMLmail a try.

ajmartin’s picture

So, after testing with HTMLmail I am indeed able to send HTML emails via Rules. However... it would be good to include a plain text version, as per SendGrid's recommendations. HTMLmail doesn't seem to do this without Mail Mime, which requires Pear (not possible in my production env) or Include - which has been out of order for two years: www.drupal.org/node/2294753.

Mime Mail does support sending HTML & plain text versions, however it doesn't appear to work well with SendGrid Integration. Perhaps because of the Header Content-Type: multipart/mixed ?

In case it helps, I've attached:

  1. A rule export for plain text email
  2. A rule export for html email
  3. HTMLmail > SendGrid maillog output (using rule #1)
  4. Mime Mail > SendGrid maillog output (using rule #2)
  5. Screenshots of my mail system and mime mail settings, to make sure I'm not stuffing up something simple :)

So, am I missing something? Or is better integration with Mime Mail something that I should add as a feature request?

Perignon’s picture

I will look into what headers Mime mail sets. I have not, to date, used Mime Mail so I will have to do some testing.

As far as the text only emails. I send about 800,000 to 1 million emails a month for marketing and do not include a text version of the HTML message, has not been an issue in my experience of doing this for six years.

I could add code to strip the HTML tags of the message and create a text only version.

Perignon’s picture

A little follow-up. So if text/html is not set as the Content-Type in the headers any HTML tags get stripped from the message to sanitize the message.

I have updated the README with this information and I will investigate the industry norm with the headers and the content types.

  • Perignon committed 7a478f2 on 7.x-1.x
    Issues #2704081: Update documentation about HTML emails and the header...
  • Perignon committed a7d557e on 7.x-1.x
    Issues #2704081: Update documentation about HTML emails and the header...
ajmartin’s picture

Thanks for looking into it, Perignon. Being able to use Mime Mail's send HTML email rule with SendGrid would be really handy!

I could add code to strip the HTML tags of the message and create a text only version.

I believe that this could be accomplished by moving setText() outside the else block on line 171 of sendgrid.mail.inc:

    // Check Content-Type of message body.
    if (strpos($message['headers']['Content-Type'], 'text/html') !== FALSE) {
      $sendgrid_message->setHtml($message['body']);
    }
    // Always include a text/plain version.
    $sendgrid_message->setText(drupal_wrap_mail(drupal_html_to_text($message['body'])));

I've tested the above change briefly; emails sent as HTML are received with both plain text and html versions.

  • Perignon committed 99a2162 on 7.x-1.x
    Issue #2704081 by ajmartin, Perignon: Always send a text message with...
Perignon’s picture

I committed a change that should always send a text version of the email as suggested in #8.

Perignon’s picture

Perignon’s picture

@ajmartin I have been doing a lot of testing with MIMEmail module and have come to the conclusion it is not compatible with Sendgrid unless you are going to use the SMTP module and not the Sendgrid Integration module. I am updating the documentation for the module to reflect this.

While I was able to get the messages to send in HTML with MIMEmail, MIMEmail will embed the CSS into the message through it's own templating system which I cannot get to work correctly. It puts the body of the email into the wrong portion of the template. As much as I tried changing configurations with Mailsystem I cannot get this to stop.

I am going to leave the multipart/mixed code in the SendGrid Integration module so that a message sent as that will function to some extent.

I do not personally use MIMEmail because of the lack of template control, so if someone else wants to do more testing with MIMEmail I will accept any contributions!

MrPeanut’s picture

it is not compatible with Sendgrid unless you are going to use the SMTP module and not the Sendgrid Integration module

Are you saying it is possible to send HTML emails via Rules using the SMTP Authentication Support module alone?

I've also tested Swift Mailer to send via SendGrid SMTP (instead of this module). It does send in HTML format from Rules' Send mail action

I also had trouble with this. My goal is to send HTML emails from Rules (via Queue Mail) through SendGrid. I don't really care which modules I use. What set of modules accomplishes this?

ajmartin’s picture

@Perignon Sounds like a sensible decision. I had poked around a little initially to see if there was a simple solution, but as a novice coder it got beyond me.

Defaulting to sending plain/text in addition to HTML accomplishes all that I need, so thank you for work on this!

ajmartin’s picture

@MrPeanut

Are you saying it is possible to send HTML emails via Rules using the SMTP Authentication Support module alone?

Yes - see sendgrid.com/docs/Integrate/Open_Source_Apps/drupal.html

But for me, using SendGrid Integration is better. By default, Rules will send emails as plain text. If you use HTMLmail and configure Mail System correctly Rules will send HTML emails.

I didn't find Mail System's config particularly intuitive to begin with, so if you need a tip check out the 5_Mail_System___config.png attachment, and swap HTMLMailSystem for MimeMailSystem.

Perignon’s picture

@MrPeanut Just use HTMLmail and everything will work like a charm. MIMEmail added a lot of complexity that you do not need. SendGrid, when used as we do in this module, is used as a web service to send email. It is not SMTP so the mail is not sent in an RFC format.

I submitted, and was accepted, a pull request to update sendgrid.com/docs/Integrate/Open_Source_Apps/drupal.html to include the SendGrid Integration module. On their next update of the docs you will see it. https://github.com/sendgrid/docs/pull/1734

Perignon’s picture

Status: Active » Fixed

Marking fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ashroof’s picture

Any solutions yet

apoc1’s picture

#19 is not working. Gives following error:

Warning: Undefined array key "Content-Type" in Drupal\mimemail\Plugin\Mail\MimeMail->format() (line 102 of modules/mimemail/src/Plugin/Mail/MimeMail.php).
Drupal\mimemail\Plugin\Mail\MimeMail->format(Array) (Line: 43)
Drupal\mailsystem\Adapter->format(Array) (Line: 289)
Drupal\Core\Mail\MailManager->doMail('rules', 'rules_action_mail_rules_send_email', ********@*********', 'site_default', Array, '', 1) (Line: 179)
Drupal\Core\Mail\MailManager->Drupal\Core\Mail\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 180)
Drupal\Core\Mail\MailManager->mail('rules', 'rules_action_mail_rules_send_email', '********@*********', 'site_default', Array, '', 1) (Line: 70)
Drupal\mailsystem\MailsystemManager->mail('rules', 'rules_action_mail_rules_send_email', '********@*********', 'site_default', Array, '') (Line: 122)
Drupal\rules\Plugin\RulesAction\SystemSendEmail->doExecute(Array, 'Er is een nieuwe factuur beschikbaar', 'Dit is een automatische factuur mail', '', NULL)
call_user_func_array(Array, Array) (Line: 139)
Drupal\rules\Core\RulesActionBase->execute() (Line: 110)
Drupal\rules\Plugin\RulesExpression\ActionExpression->executeWithState(Object) (Line: 33)
Drupal\rules\Plugin\RulesExpression\ActionSetExpression->executeWithState(Object) (Line: 121)
Drupal\rules\Plugin\RulesExpression\RuleExpression->executeWithState(Object) (Line: 33)
Drupal\rules\Plugin\RulesExpression\ActionSetExpression->executeWithState(Object) (Line: 147)
Drupal\rules\EventSubscriber\GenericEventSubscriber->onRulesEvent(Object, 'rules_entity_insert:node', Object)
call_user_func(Array, Object, 'rules_entity_insert:node', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, Object) (Line: 167)
rules_entity_insert(Object)
call_user_func_array('rules_entity_insert', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('entity_insert', Array) (Line: 201)
Drupal\Core\Entity\EntityStorageBase->invokeHook('insert', Object) (Line: 800)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('insert', Object) (Line: 530)
Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, ) (Line: 685)
Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, ) (Line: 455)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 801)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 270)
Drupal\node\NodeForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 113)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 593)
Drupal\Core\Form\FormBuilder->processForm('node_factuur_form', Array, Object) (Line: 321)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 706)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

It sends the mail with the HTML included as attachment. Serious questions about security on this solution too.