Best practice to send draft emails by scheduler

Dear community,

I am working with V13.0.0 and I have the following business case:

A custom module "Claim". This module is related to the Cases module (1:N). There can be several claims of different or the same type under the case. Each claim can be answered individually. The answers are created by the email module attached to the claim.

The Claim Module has a field "Due Date".

I want the related draft email to be sent only when this "Due Date" is reached. The "Send"-button in the "Compose Email View" of the email. is hidden. This way it is only possible to create a "Draft".

Scheduler is created at the Scheduler Module

Here is what I have done so far. 

Code: custom/Extension/modules/Schedulers/Ext/ScheduledTasks/sendClaimDraftEmails.php

function sendDraftEmails() {
    
    $emails = BeanFactory::newBean('Emails');

    $query = new SugarQuery();
    $query->select(array('id', 'mg_email_send_date_c'));
    $query->from($emails, array('team_security' => false));
    $query->where()->equals('status', 'draft');
    $query->where()->queryAnd()->equals('status', 'draft')->notNull('mg_email_send_date_c');
    $draft_emails = $query->execute();
    
    _ppl($draft_emails);
    
    $currentDate = strtotime(date('Y-m-d H:i:s'));

    _ppl($currentDate);

    if (!empty($draft_emails)) {
	    foreach($draft_emails as $email){

            $emailSendDate = strtotime($email['mg_email_send_date_c']);
            
            _ppl($emailSendDate);
            
            if ($currentDate >= $emailSendDate) {
                
                $email_bean = BeanFactory::retrieveBean('Emails', $email['id']);
        
                $email_bean->type = "out";
                $email_bean->state = Email::STATE_READY;

                if ($email_bean->send()) {
                    $email_bean->status = 'sent';
                    $email_bean->state = Email::STATE_ARCHIVED;
                } else {
                    $email_bean->status = 'send_error';
                    $email_bean->state = Email::STATE_DRAFT;
                }
		
                $email_bean->save();
            }
        }
    }
    
    return true;
}

I found the email send approach above on the www, without mention the Sugar version. It Looks like, this does not work with Sugar V13. I searched in Dev Club and Sugar Documents as well, without success.

Hope I have made myself clear..

Does anyone have an idea how I can achieve this? 

Thank you very much for any help

Rene

Parents
  • I have not done this, but if I needed to I would do as follows.

    By setting the execute_time on a scheduler job you can create a scheduler job and queue it for the Due Date.

    You could create a custom scheduler that given an email id will retrieve the draft with that id and send it (call that sendClaimEmail, for example)

    In your compose (wherever you create the email response) you could create the email record as a draft, then use the id of that EmailBean and create a custom scheduler.

    Something like:

          

     require_once('include/SugarQueue/SugarJobQueue.php');
    
           // Create the new job
           $job = new SchedulersJob();
           $job->name = "SendClaimEmail- {$ClaimBean->id} -  {$EmailBean->id}";
           $job->data = $EmailBean->id;
           $job->execute_time = $ClaimBean->due_date; //match this to your due date field, in the correct format
           $job->target = "function::sendClaimEmail"; //your custom job
           $job->requeue = true; //requeue in case it fails to execute on time (better late than never)
           $job->assigned_user_id = $current_user->id;
           $jq = new SugarJobQueue();
           $jobid = $jq->submitJob($job);

    That will schedule the sendClaimEmail for that particular ID to execute at that particular DueDate.

    FrancescaS

Reply
  • I have not done this, but if I needed to I would do as follows.

    By setting the execute_time on a scheduler job you can create a scheduler job and queue it for the Due Date.

    You could create a custom scheduler that given an email id will retrieve the draft with that id and send it (call that sendClaimEmail, for example)

    In your compose (wherever you create the email response) you could create the email record as a draft, then use the id of that EmailBean and create a custom scheduler.

    Something like:

          

     require_once('include/SugarQueue/SugarJobQueue.php');
    
           // Create the new job
           $job = new SchedulersJob();
           $job->name = "SendClaimEmail- {$ClaimBean->id} -  {$EmailBean->id}";
           $job->data = $EmailBean->id;
           $job->execute_time = $ClaimBean->due_date; //match this to your due date field, in the correct format
           $job->target = "function::sendClaimEmail"; //your custom job
           $job->requeue = true; //requeue in case it fails to execute on time (better late than never)
           $job->assigned_user_id = $current_user->id;
           $jq = new SugarJobQueue();
           $jobid = $jq->submitJob($job);

    That will schedule the sendClaimEmail for that particular ID to execute at that particular DueDate.

    FrancescaS

Children
  • Hi Francesca,

    Please excuse my late reply.... Vacation season...

    Thank you very much for taking the time to answer and this suggestion. This is indeed an approach I'll think about.

    What gives me more headaches is sending the drafted emails. I find many examples of how to create emails and send them using the Email class. What I do not find, and I have searched a lot, is how to send drafted emails. Do you have perhaps references to examples?

    Thank's again for your help
    Rene