2

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?

2

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);
    }
}
  • what happens when your cron runs every 15 mins? It seems emails would be sent at every cron execution? Interesting I did the same thing but implemented with Elysia Cron – stefgosselin Aug 21 '12 at 3:54
0

this module will help you scheduled actions

you can set the cron trigger by rules

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.