Thursday 9 July 2015

How to create html mail template for Drupal?

In Drupal 7 by default we can't send html mail, since Drupal will convert all emails to plain text before sending. This could be overridden using Drupal 7htmlmail contributed module. We may also need i>mailsystem module for implementing htmlmail, which is a dependency. The module also give html mail template, which could be customized as well in same way of theme templates.
Firstly, we can look on the configuration details and then theming of html template mail. If there is a need to make the html template for a particular mail with module name and with a particular mail key then we have to create a new settings in 'admin/config/system/mailsystem' namely module_name and key_name respectively. After saving the new settings we have to set 'HTMLMAILSystem' for the new 'Mail System Settings'. Next in htmlmail configuration 'admin/config/system/htmlmail'
  • step-1: List of available templates and descriptions.
  • step-2: Email theme, where the above custom template will hold.
  • step-3: Post-filtering, text format used for filtering the mail.
Find out the template file which meets your need and copy paste them to your theme template and specify it in above config page.
Now theme the copy of mail template as you wish. That's all and you have completed with your config and theming of html mail template.
Using the help of Drupal functions like drupal_mail we can send mail using the 'htmlmail' module template.
We need to specify the module name and key according to template file we use.
Let us assume, if we use 'htmlmail--module_name--key_name.tpl.php' template then drupal_mail call be as below to use this template for the same.

  $module = 'module_name';
  $key = 'key_name';
  $to = $user->mail;
  $language = language_default();
  $name = $user->name;
  $params = array();
  $from = 'no-reply@gmail.com';
  drupal_mail($module, $key, $to, $language, $params, $from);
    
Also it will be a good practice to add hook_mail(), if we call drupal_mail with in our module.
Hope this article about html template mail for Drupal 7 helped you.

How to write MySql single export query

We as a PHP Drupal web platform developer most often has to face the situation to export data from one table to another table of same database or to table of another database (if site uses multiple databases) during the process of any change in normal work flow.

Most of us normally think to export the data from one table to another by firstly querying the required data from source table and then looping through the query result and inserting them to destination table. This can be simply achieved with a single query.
Major drawback of this is that first select query has to be compiled and run in MySQL server then for each query result front-end server has to loop through it and return the corresponding insert query to MySQL, which in turn has to compile and run for each iteration. If lakhs of data is to be exported, then this will be a huge load for front end server.
Let me tell you another option. The single query export approach:
  1. If the source table and destination table are both in same database.
            
              $query = db_query(
                "INSERT INTO `destination_table_name` (`title`, `uid`, `created`)
                  SELECT title, uid, created
                  FROM `source_table_name`
                  ORDER BY created DESC
              );
            
          
  2. If the source table and destination table are in different database, namely source table is in database_1 and destination table is in database_2.
            
              $query = db_query(
                "INSERT INTO `database_2.destination_table_name` (`title`, `uid`, `created`)
                  SELECT title, uid, created
                  FROM `database_1.source_table_name`
                  ORDER BY created DESC
              );
            
          
Normally this seems weird, but its the real trick. Avoiding of multiple compilation of query in MySQL server and looping in front-end server. Hope this helps!