0

I need to have a functionality where the user can send the current article in an HTML mail.

I don't know how to do this, I don't have previous experience with Drupal mail system, After some time googling I ended up installing Mail system module (https://www.drupal.org/project/mailsystem) and creating a custom module containing this function:

<?php
/**
* Implements hook_mail().
*/
function custom_mail_mail($key, &$message, $params) {
    $user = User::load(\Drupal::currentUser()->id());
    $user_name = $user::getUsername(); 

    $options = array(
        'langcode' => $message['langcode'],
    );

    $message['from'] = \Drupal::config('system.site')->get('mail');

    switch ($key) {
        case 'article':
        $message['subject'] = $user_name + t(' - Article: @title', array('@title' => $params['node_title']), $options);
        $message['body'] = $params['body']);
        case 'recipe':
        $message['subject'] = $user_name + t(' - Recipe: @title', array('@title' => $params['node_title']), $options);
        $message['body'] = $params['body']);
    break;
    }
}

I also have this in the frontend. enter image description here

This is coming from an included twig template. I don't know how to link the click of the "Send Email" button to the custom module function.

Then I will need to get the current node (Title, body, images) to include it in the body of the email. I will also need to get the current username to include it in the subject and the email written in the input field at the image above to be the recipient mail.

  • drupal.org/project/mimemail – user21641 Nov 9 '17 at 11:40
  • How can I link the button click and the module or PHP ?? Do I need to use ajax? – Bassem Mohamed Nov 9 '17 at 12:14
  • In the form submit handler call \Drupal::service('plugin.manager.mail')->mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) – user21641 Nov 9 '17 at 12:32
  • I Ended up using Forward module, it did everything for me, Thanks – Bassem Mohamed Nov 9 '17 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.