How to create a task which run at cron time and send emails to users?
Question
Is there a module (based on cron) which can be used to check a date value form a field and to send email to users 2 days before the date?
If there is not, how can I implement this to work with cron?
Solution
Here is how I did without using any custom modules.
I implemented hook_cron
like that:
function HOOK_cron() {
// search for expiration date interval
$result = db_query("SELECT my_date, uid, company_name, nid
FROM my_table
LEFT JOIN ...
LEFT JOIN ...
LEFT JOIN ...
LEFT JOIN ...
WHERE my_date BETWEEN DATE(NOW( ) + INTERVAL 6 DAY ) AND DATE( NOW( ) + INTERVAL 20 DAY)")->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $product) {
$module = 'MY_MODULE';
$key = 'MY_KEY';
$from = 'my_email@test.com';
$user_account = user_load($product['uid']);
$product_detail = node_load($product['nid']);
$to_user_mail = $user_account->mail;
// build the email
$message = drupal_mail($module, $key, $to_user_mail, LANGUAGE_NONE, array(), $from, FALSE);
//$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
$message['headers']['Cc'] = 'cc_email@test.com';
$message['headers']['Reply-To'] = 'reply_to_email@test.com';
$message['subject'] = 'THE SUBJECT';
$message['body'] = array();
$message['body'][] = 'Dear ' . $user_account->name;
$message['body'][] = '...';
$message['body'][] = 'Regards,';
$message['body'][] = 'YOUR NAME';
// retrieve the responsible implementation for this message.
$system = drupal_mail_system($module, $key);
// format the message body.
$message = $system->format($message);
// send e-mail
$message['result'] = $system->mail($message);
}
}
OTHER TIPS
this module will help you scheduled actions
you can set the cron trigger by rules
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange