Sending Email to Multiple Leads at Once

Hey All, 

Is there functionality built into SugarCRM that allows for an email template to be sent to multiple leads at once? 

I know we can use the checkbox next to the lead to select multiple leads at once to email as shown below: 

the problem there is it will list out every email in the "to" address bar when sent. 


I would like to be able to send a templated email to say 15 leads at once, without listing out every contact the email was sent to, to each customer. 

Any insight or suggestions for this would be appreciated. 

Parents
  • One way you could do this, if you want to go down the rabbit hole of programming it yourself is to leverage the MailerFactory.

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_10.0/Architecture/Email/Mailer_Factory/

    For example I use this to send ONE email to one or more To and Cc addresses from various logic hooks, schedulers etc.
    These are generally for internal emails,  things like  notifications to an entire Team or department, or the assigned user cc the manager etc..

    function sendGroupEmail($ToEmailAdd, $FromEmailAdd, $FromEmailName, $EmailSubject, $EmailBody, $CcEmailAdd = array(), $attach='') {
      include_once('modules/Mailer/MailerFactory.php');
      global $sugar_config;
      $To = array();
      $Cc = array();
    
      if(is_array($ToEmailAdd)){
        $To = $ToEmailAdd;
      }else{
        $To = explode(';',$ToEmailAdd);
      }
    
      if(is_array($CcEmailAdd)){
        $Cc = $CcEmailAdd;
      }else{
        $Cc = explode(';',$CcEmailAdd);
      }
      try{
        $phpMailer = MailerFactory::getSystemDefaultMailer();
        $mailTransmissionProtocol = $phpMailer->getMailTransmissionProtocol();
        $FromEmailIdentity = new EmailIdentity($FromEmailAdd, $FromEmailName);
        $headers = new EmailHeaders();
        $header_array = array(
          'From'=>$FromEmailIdentity,
          'ReplyTo'=>'',
          'Sender'=>$FromEmailIdentity, //mandatory
          'Subject'=>$EmailSubject,
        );
        $headers->buildFromArray($header_array);
        $phpMailer->setHeaders($headers);
        //$phpMailer->setHtmlBody($EmailBody);
        $phpMailer->setTextBody($EmailBody);
    
        foreach($To as $to_addr){
          //note should be new EmailIdentity(<email_address>, <name>)
          //we don't have a name for these
          $phpMailer->addRecipientsTo(new EmailIdentity($to_addr, $to_addr));
        }
        foreach($Cc as $cc_addr){
          //note should be new EmailIdentity(<email_address>, <name>)
          //we don't have a name for these
          $phpMailer->addRecipientsCc(new EmailIdentity($cc_addr, $cc_addr));
        }
        if(!empty($attach)){
          $phpMailer->addAttachment(new Attachment($attach));
        }
        $phpMailer->send();
      }catch(MailerException $me) {
        $message = $me->getMessage();
        $GLOBALS["log"]->fatal(
          "SendGroupEmail: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"
        );
      }
      return;
    }
    

    And this to send individual emails to one or more recipients

    <?php
      //Function to send Email message
      //one email per recipient
    
    function sendEmail($ToEmailAdd, $FromEmailAdd, $FromEmailName, $EmailSubject, $EmailBody) {
      global $sugar_config;
      require_once ('modules/Emails/Email.php');
      if(is_array($ToEmailAdd)){
        $To = $ToEmailAdd;
      }else{
        $To = explode(';',$ToEmailAdd);
      }
      foreach ($To as $to_addr){
        if (filter_var($to_addr, FILTER_VALIDATE_EMAIL)){
          try{
            $phpMailer = MailerFactory::getSystemDefaultMailer();
            $mailTransmissionProtocol = $phpMailer->getMailTransmissionProtocol();
            $FromEmailIdentity = new EmailIdentity($FromEmailAdd, $FromEmailName);
            $header_array = array(
              'From'=>$FromEmailIdentity,
              'ReplyTo'=>'',
              'Sender'=>$FromEmailIdentity, //mandatory
              'Subject'=>$EmailSubject,
            );
            $phpMailer->constructHeaders($header_array);
            $phpMailer->addRecipientsTo(new EmailIdentity($to_addr, $to_addr));
            $phpMailer->setHtmlBody($EmailBody);
            $phpMailer->send();
          }catch(MailerException $me) {
            $message = $me->getMessage();
            $GLOBALS["log"]->fatal(
              "SendEmail: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"
            );
            return($message);
          }
        }else{
          $message = 'BAD EMAIL TO:' . $to_addr;
          return($message);
        }
      }
      return;
    }
    ?>
    

    You could even build something that leverages Sugar's APIs to send and log emails...

     
    But I have been on Professional for over a decade, and I've been building my own BPMs for years). 

    If you can use BMP as  suggested or Campaigns as  suggested,

    I would advise you do so.

    PS. This is OLD code and could use some revision for sure Slight smile ...the downside of coding everything is it gets pretty hard to maintain...

Reply
  • One way you could do this, if you want to go down the rabbit hole of programming it yourself is to leverage the MailerFactory.

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_10.0/Architecture/Email/Mailer_Factory/

    For example I use this to send ONE email to one or more To and Cc addresses from various logic hooks, schedulers etc.
    These are generally for internal emails,  things like  notifications to an entire Team or department, or the assigned user cc the manager etc..

    function sendGroupEmail($ToEmailAdd, $FromEmailAdd, $FromEmailName, $EmailSubject, $EmailBody, $CcEmailAdd = array(), $attach='') {
      include_once('modules/Mailer/MailerFactory.php');
      global $sugar_config;
      $To = array();
      $Cc = array();
    
      if(is_array($ToEmailAdd)){
        $To = $ToEmailAdd;
      }else{
        $To = explode(';',$ToEmailAdd);
      }
    
      if(is_array($CcEmailAdd)){
        $Cc = $CcEmailAdd;
      }else{
        $Cc = explode(';',$CcEmailAdd);
      }
      try{
        $phpMailer = MailerFactory::getSystemDefaultMailer();
        $mailTransmissionProtocol = $phpMailer->getMailTransmissionProtocol();
        $FromEmailIdentity = new EmailIdentity($FromEmailAdd, $FromEmailName);
        $headers = new EmailHeaders();
        $header_array = array(
          'From'=>$FromEmailIdentity,
          'ReplyTo'=>'',
          'Sender'=>$FromEmailIdentity, //mandatory
          'Subject'=>$EmailSubject,
        );
        $headers->buildFromArray($header_array);
        $phpMailer->setHeaders($headers);
        //$phpMailer->setHtmlBody($EmailBody);
        $phpMailer->setTextBody($EmailBody);
    
        foreach($To as $to_addr){
          //note should be new EmailIdentity(<email_address>, <name>)
          //we don't have a name for these
          $phpMailer->addRecipientsTo(new EmailIdentity($to_addr, $to_addr));
        }
        foreach($Cc as $cc_addr){
          //note should be new EmailIdentity(<email_address>, <name>)
          //we don't have a name for these
          $phpMailer->addRecipientsCc(new EmailIdentity($cc_addr, $cc_addr));
        }
        if(!empty($attach)){
          $phpMailer->addAttachment(new Attachment($attach));
        }
        $phpMailer->send();
      }catch(MailerException $me) {
        $message = $me->getMessage();
        $GLOBALS["log"]->fatal(
          "SendGroupEmail: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"
        );
      }
      return;
    }
    

    And this to send individual emails to one or more recipients

    <?php
      //Function to send Email message
      //one email per recipient
    
    function sendEmail($ToEmailAdd, $FromEmailAdd, $FromEmailName, $EmailSubject, $EmailBody) {
      global $sugar_config;
      require_once ('modules/Emails/Email.php');
      if(is_array($ToEmailAdd)){
        $To = $ToEmailAdd;
      }else{
        $To = explode(';',$ToEmailAdd);
      }
      foreach ($To as $to_addr){
        if (filter_var($to_addr, FILTER_VALIDATE_EMAIL)){
          try{
            $phpMailer = MailerFactory::getSystemDefaultMailer();
            $mailTransmissionProtocol = $phpMailer->getMailTransmissionProtocol();
            $FromEmailIdentity = new EmailIdentity($FromEmailAdd, $FromEmailName);
            $header_array = array(
              'From'=>$FromEmailIdentity,
              'ReplyTo'=>'',
              'Sender'=>$FromEmailIdentity, //mandatory
              'Subject'=>$EmailSubject,
            );
            $phpMailer->constructHeaders($header_array);
            $phpMailer->addRecipientsTo(new EmailIdentity($to_addr, $to_addr));
            $phpMailer->setHtmlBody($EmailBody);
            $phpMailer->send();
          }catch(MailerException $me) {
            $message = $me->getMessage();
            $GLOBALS["log"]->fatal(
              "SendEmail: error sending e-mail (method: {$mailTransmissionProtocol}), (error: {$message})"
            );
            return($message);
          }
        }else{
          $message = 'BAD EMAIL TO:' . $to_addr;
          return($message);
        }
      }
      return;
    }
    ?>
    

    You could even build something that leverages Sugar's APIs to send and log emails...

     
    But I have been on Professional for over a decade, and I've been building my own BPMs for years). 

    If you can use BMP as  suggested or Campaigns as  suggested,

    I would advise you do so.

    PS. This is OLD code and could use some revision for sure Slight smile ...the downside of coding everything is it gets pretty hard to maintain...

Children
No Data