0

I have field_a with select list of 10 value

when field_b is checked and when a value of field_a is alfa or beta or ecc, send email to address1 or 2 or ecc

I try with custom module for advance control of features

function test_mail_node_submit($node, $form, &$form_state) {
 if($node->type == 'rso') {
  if ($node->field_b['und'][0]['value'] == TRUE){
   if ($node->field_a['und'][0]['tid'] == '10'){
    mail('[email protected]', '[email protected]', 'example text');
   }
  }
 }
}

I did not understand how to use drupal_mail, or hook_menu hook_mail_alter

3
  • Are you trying to achieve this with Drupal 7 or 8 ?
    – pbonnefoi
    Commented Jul 4, 2016 at 8:51
  • Drupal 7....I'm sorry for forgeditfulness
    – Mirkozzo
    Commented Jul 4, 2016 at 9:04
  • I tried rules, but I can not set the address from the check field
    – Mirkozzo
    Commented Jul 4, 2016 at 17:50

2 Answers 2

0

I am a little confused about your question. But here is an example for you to send email using hook_mail. Hope you can get some idea from this:

/**
* Implement hook_mail() function
*/
function test_mail_node_mail($key, &$message, $params) {
   $headers = array(
     'MIME-Version' => '1.0',
     'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
     'Content-Transfer-Encoding' => '8Bit',
     'X-Mailer' => 'Drupal'
   );

   foreach ($headers as $key => $value) {
      $message['headers'][$key] = $value;
   }

   $message['subject'] = $params['subject'];
   $message['body'] = $params['body'];
}

/**
* Implement hook_submit() function
*/
function test_mail_node_submit($node, $form, &$form_state) {
   if($node->type == 'rso') {
     if ($node->field_b['und'][0]['value'] == TRUE){
       if ($node->field_a['und'][0]['tid'] == '10'){
          // mail('[email protected]', '[email protected]', 'example text');
          $valid_email = array('[email protected]', '[email protected]');
          $params = array(
             'body' => 'Your body text',
             'subject' => 'This is a subject',
          );
          drupal_mail('test_mail_node', 'HTML', $valid_email, 'en', $params));
       }
     }
  }
}
0
0

thanks for the reply.

I write this code:

function test_mail_mail($key, &$message, $params) {

 switch($key) {
    case 'one':
      $message['subject'] = "mail-1";
      $message['body'][] ="example text 2";
      break;

    case 'two':
      $message['subject'] = "mail-2";
      $message['body'][] = "example text 2";
      break;
  }
}

function test_mail_node_submit($node, $form, &$form_state){
    if($node->type == 'alfa') {
        if ($node->field_q['und'][0]['tid'] == '100'){
                $node->field_2['und'][0]['value'] = 'Mr.Brown';

// drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)
drupal_mail('test_mail', 'mail-1', '<example1.mail.com>', language_default(), $params, 'example2.mail.com');

        }
    }
}

it works but do not understand how to use $ params. In the Body of the Message I would like to insert some variable, for example: $node->field_2['und'][0] ['value'] ((Mr.Brown))

3
  • This is php question. You can use dot. For example, based on you body text, you can write like this: $message['body'][] = "example text 2".$node->field_2['und'][0]['value'];
    – jmu
    Commented Jul 5, 2016 at 18:44
  • I had already tried, result: two errors notice Notice: Undefined variable: node in test_mail_mail() Notice: Trying to get property of non-object in test_mail_mail()
    – Mirkozzo
    Commented Jul 6, 2016 at 11:41
  • I have add: $node = menu_get_object(); ....sorry but I'm a beginner :p
    – Mirkozzo
    Commented Jul 6, 2016 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.