How to send an email from user email templates programmatically? There are some email templates on the admin interface of the user account settings.

I want to load that email template and change the token value dynamically in my custom module and then send that email?

Any one who have some idea?
I have found the solution. Below is the method I followed.

I added the new email template to the user account settings form using the following code.

$form['custom_email_template'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t('Contributor Email on Miles recieved'),
    '#collapsible' =&gt; TRUE,
    '#collapsed' =&gt; TRUE,
    '#tree' =&gt; TRUE,
    '#group' =&gt; 'email',
  );
  $form['custom_email_template']['user_mail_custom_email_template_subject'] = Array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; 'Subject',
    '#default_value' =&gt; variable_get('user_mail_custom_email_template_subject'),
    '#maxlength' =&gt; 180,
  );
  $form['custom_email_template']['user_mail_custom_email_template_body'] = Array(
    '#type' =&gt; 'textarea',
    '#title' =&gt; 'Body',
    '#default_value' =&gt; variable_get('user_mail_custom_email_template_body'),
    '#rows' =&gt; 15,
  );


And then on the 2nd submit handler of the user account settings form I used the following code to save the email content in a drupal variable.

variable_set('user_mail_custom_email_template_subject', $form_state['values']['custom_email_template']['user_mail_custom_email_template_subject']);
variable_set('user_mail_custom_email_template_body', $form_state['values']['custom_email_template']['user_mail_custom_email_template_body']);


and then use the following code to get the content of the email template.

$body = variable_get('user_mail_custom_email_template_body');
$subject = variable_get('user_mail_custom_email_template_subject');


and used the str_replace() function to replace the tokens with the dynamic values.

Hope it will help someone.