Showing posts with label langcode. Show all posts
Showing posts with label langcode. Show all posts

Monday 29 October 2018

How to send email programmatically in Drupal 8?

Sometime we need to send email on any events or after creation of blog or article or in any e-commerce site for send confirmation mail after successful completion of order. So In this Blog we are going to explore how to send email programmatically in drupal 8.
We just have to use `hook_mail` function in '.module' file. here two simple steps for send email:

1. Define your properties like Subject, body, headers etc in hook_mail() function.
2. Use Mail Manager to send email.

When we need to send email programmatically, we need to specify the module name that implements hook_mail().
Now have a look of implementation.

<?php

/**
 * Implements hook_mail().
 */

function [module_name]_mail($key, &$message, $params) {
    $options = array(
        'langcode' => $message['langcode'],
    );
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
    switch ($key) {
        case 'create_event':
            $message['from'] = \Drupal::config('system.site')->get('mail');
            $message['subject'] = t('Notification: @subject', array('@subject' => $params['subject']), $options);
            $message['body'][] = SafeMarkup::checkPlain($params['message']);
            break;
    }
}
?>

Here $key is defines one template identified as test_message.
The two other arguments are $params and $message. here $parms is an array of data that needs to go in the email and that is passed from mail manager.
So here is final step for send mail via `mailManager`.

<?php
/**
* Implements hook_entity_insert().
*/

function [module_name]_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
    if ($entity->getEntityTypeId() !== 'node' || ($entity->getEntityTypeId() === 'node' && $entity->bundle() !== 'event')) {
      return;
    }   
    $user_email = \Drupal::currentUser()->getEmail();
    $key = 'create_event';
    $module = <module_name>;
    // mail sending to reviewer users
    $msg1 = "Hello, <br /><br />";
    $msg1.= "Please check your content item, that is published by publisher.";
    $msg1.= "Kind regards";

    $mailManager = \Drupal::service('plugin.manager.mail');
    $to = $user_email;
    $params['message'] = t($msg1);
    $params['subject'] = "Project name workflow for published Notification.";
    $langcode = \Drupal::currentUser()->getPreferredLangcode();
    $send = true;

    $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);   

    if ($result['result'] !== true) {
      drupal_set_message(t('There was a problem sending your message.'), 'error');
    }
    else {
      drupal_set_message(t('Your message has been sent.'));
    }   
}
?>

By default, PHPMail, which is use for default_mail() function in PHP. So this will triggered whenever any event will be created, here
$send = the boolean value which is indicating whether the email should be actually sent or not.
$key = template id.


Note: If have any suggestions or issue regarding 'How to send email programmatically in Drupal 8?' then you can ask by comments.  
   

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...