nortmas
9/27/2017 - 2:43 PM

Send mail

<?php
function some_func() {
  
  $content = '';
  
   $file = [
    'filename' => 'contract.pdf',
    'filemime' => 'application/pdf',
    'filecontent' => $content
  ];
  
  $params['message'] = 'test smtp';
  $params['subject'] = 'title';
  $params['attachments'][] = $file;
  
  $res = \Drupal::service('plugin.manager.mail')->mail('module_name', 'custom_key', 'raerayan@gmail.com', 'en', $params, NULL, TRUE);
}
<?php

/**
 * Implements hook_mail().
 */
function hook_mail($key, &$message, $params) {
 switch ($key) {
   case 'custom_key':
     $message['subject'] = $params['subject'];
     $message['body'][] = $params['message'];
     $message['headers'] = [
       'MIME-Version' => '1.0',
       'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
       'Content-Transfer-Encoding' => '8Bit',
       'From' => \Drupal::config('system.site')->get('mail'),
      ];
     //$message['params']['attachments'][] = $params['attachments'];
     break;
 }
}