1

The story : I create lets say a simple module to store input data and sent a mail when you click save. Now i done the part of store data, send mail and retrieve data, but i would like to add tokens to the mail part.

So for now i have something like this.

 function mymodule_form (){
         $form['name'] = array(
        '#title' => t('Your name'),
        '#type' => 'textfield',
        '#size' => 60,
        '#maxlength' => 100,
        '#description' => t('Input your first and last name.'),

        $form['submit'] = array(
        '#value' => 'Save',
        '#type' => 'submit',
        '#submit' => array('mymodule_submit'),
         );

    return $form;

}

Now you have mymodule_submit that saves the data and sends mail.

function mymodule_submit(){

 here goes the part for saving data into table(not going to write it)....

/*mail send*/
    $text=array();
    $subject=array();
    $mail = db_query("SELECT * FROM {mail_data} WHERE mail_id=2"); 
    foreach ($mail as $data){
        $text = $data -> text;
        $subject = $data -> subject;
    }

    $params = array(
    'subject' => $subject,
    'body' => $text,
            );
     $to =$emailto; //db_query that selects from table
     $from =$emailfrom; //db_query that selects from table
    drupal_mail('person', 'my_module', $to, language_default(), $params, $from, $send = TRUE);

    /*end mail send */

}

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

  switch ($key) {
    case "my_module":
        $message['subject'] = $params['subject'];
        $message['body'][] = $params['body'];
      break;

  }
 }

Now I made a form for inserting subject and body of mail hence $mail = db_query("SELECT * FROM {mail_data} WHERE mail_id=2");

My question is how can i allow user to use tokens or something else to add tokens like in drupal account mail settings(like: account activation, account blocked etc...), where you can add tokens to text so that you get :

[user:name],

A site administrator at [site:name] has created an account for you. You may now log in by clicking this link or copying and pasting it to your browser:

[user:one-time-login-url]

So to clarify ... I have a form that user uses to input mail data but for now he can only input text , i want to make it possible for him to use tokens(or something else like drupal_set_message(t('Saved input @var!', array('@var'=>$temp)));) when he inputs data...

Its a form like mymodule_form.

0

You would have to add them to a global token piece like [site] for them to be able to be used anywhere tokens are used.

But to use in a custom module you can use the following hooks:

Generate token info:

/**
 * Implements hook_token_info().
 */
function module_name_token_info() {
  $info = array();
  // Define a new token type.
  $info['types']['my_custom_tokens'] = array(
    'name' => t('My custom tokens'),
    'description' => t('A token type for my custom tokens.'),
  );
  // Define any new tokens.
  $info['tokens']['my_custom_tokens']['my_token'] = array(
    'name' => t('My token'),
    'description' => t('A token I use in my module.'),
  );
  return $info;
}

Give values to token:

/**
 * Implements hook_tokens().
 */
function module_name_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'my_custom_tokens') {
    // Loop through the available tokens.
    foreach ($tokens as $name => $original) {
      // Find our custom tokens by name.
      switch ($name) {
        case 'my_token':
          // Work out the value of our token.
          $value = 'Value of token';
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
      }
    }
  }

  return $replacements;
}

That should get you started with custom tokens. I've used this a few different times with different situations and it works very well.

  • No , I get that but how do you then use it when writing mail in custom module? Lets say mail looks like this: Hi [my_custom_tokens:my_token], How are you today. What do i need to do that so that it replaces [my_custom_tokens:my_token] with a name – 2thecore Jan 11 '15 at 11:47
  • So you want to be able to use the custom tokens in the UI? Or in the module where you are writing the mail code? If it's both you would first have to define the tokens in the module code so the user can use them in the UI and then you would just use the values passed in the module code like normal variables getting passed in the form submit handler. But maybe I'm not getting the complete picture here. – burnsjeremy Jan 11 '15 at 12:45
0

Thanks for the help i did it this way:

  function mymodule_submit(){

 here goes the part for saving data into table(not going to write it)....

/*mail send*/
    $text=array();
    $subject=array();
    $mail = db_query("SELECT * FROM {mail_data} WHERE mail_id=2"); 
    foreach ($mail as $data){
        $text = $data -> text;
        $subject = $data -> subject;
    }

   $params = array(
'subject' => $subject,
'body' => $message,
'!buyer_name'=> $name,
'!buyer_lastname'=> $lastname,
etc...

        );


     $to =$emailto; //db_query that selects from table
     $from =$emailfrom; //db_query that selects from table
    drupal_mail('person', 'my_module', $to, language_default(), $params, $from, $send = TRUE);

    /*end mail send */

}

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

  switch ($key) {
    case "my_module":
        $message['subject'] = $params['subject'];
        $message['body'][] = $params['body'];
      break;

  }
 }

Then i just call !buyer_name !buyer_lastname when writing a text in text box.

Your Answer

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

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