Send Workflow Emails with From as user email id instead of system email id

Hi,

I have set a workflow for Opportunity to send an email. The email is sent using the system email settings in the from field.

Can we somehow send the emails using the email id of the assigned user for opportunity?

If not, is there a way I can modify somewhere in the code? I tried searching in the modules, but could not find the file. Can somebody please point me to the right file?

Thanks and Regards,
Madhur

This would be incredibly helpful. I’ve been trying to figure out a way to do this myself and can’t make any headway.

Hi cjmNY,

I did this using Logic Hooks. Use the After_Save event from the logic hooks and write the code to send emails. I am giving you an example of the code below. My code was more complex as it involved getting information from associated leads, etc… I have tried to make it simple. So there might be some errors. Please debug if it gives errors.

Open custom/modules/Opportunities/logic_hooks.php and insert this line:


$hook_array['after_save'][] = Array(76, 'sendAutomatedEmail', 'custom/modules/Opportunities/OpportunitiesAutomatedEmail.php','OpportunitiesAutomatedEmail', 'sendAutomatedEmail');

Create custom/modules/Opportunities/OpportunitiesAutomatedEmail.php and enter the following code:


<?php

if (!defined('sugarEntry') || !sugarEntry)
    die('Not A Valid Entry Point');

class OpportunitiesAutomatedEmail
{
    function sendAutomatedEmail(&$bean, $event, $arguments)
    {
        //get the related acccount record
        $accounts = $bean->get_linked_beans('accounts','Account');
        $opportunityid = $bean->id;
        //get the assigned user record
        $user = BeanFactory::getBean("Users", $bean->assigned_user_id);
        //get the assigned user's email id and name
        $fromid = $user->email1;
        $fromname = $user->name;
        
        foreach($accounts as $account)
        {
            //get the email id to whom the email should be sent
            $accountemailid = $account->email1;
            $accountname = $account->name;
            //send the email
            $this->sendEmailAccount('49faf8b7-6497-0e07-0c65-57d91a9fcd4c', $accountname, $accountemailid, $fromid, $fromname, $opportunityid);
        }
    }

    private function sendEmailAccount($templateid, $accountname, $toemailid, $fromemailid, $fromemailname, $opportunityid)
    {
        require_once("include/SugarPHPMailer.php");
        //create new email object
        $mailer=new SugarPHPMailer();

        //create new emailtemplate object
        $email_template = new EmailTemplate();
        $email_template->disable_row_level_security = true;

        //retrieve the email template
        $email_template->retrieve($templateid);
        //replace the entity merge codes with proper values in the email template
        $email_template->body_html = str_replace('$contact_first_name', $accountname, $email_template->body_html);
        $mailer->Body = wordwrap($email_template->body_html, 900);

        //set the from id, from name, subject and to
        $mailer->From     = $fromemailid;
        $mailer->FromName = $fromemailname;
        $mailer->Subject = $email_template->subject;
        $mailer->AddAddress($toemailid, $accountname);

        $mailer->IsHTML(true);
        $mailer->prepForOutbound();
        $mailer->setMailerForSystem();
        
        //send the email
        if (!$mailer->Send())
        {
            $GLOBALS['log']->fatal("OpportunityUpdatesHook: Could not send email:  " . $mailer->ErrorInfo);
            return false;
        }
        else
        {
            //if the send email succeeds then log the email object with the opportunity record
            $this->logEmail($toemailid, $mailer, $opportunityid, $bean->assigned_user_id);
            return true;
        }

    }

    private function logEmail($email, $mailer, $entityid = null, $ownerid)
    {
        require_once('modules/Emails/Email.php');
        $emailObj = new Email();
        $emailObj->to_addrs = $email;
        $emailObj->type= 'out';
        $emailObj->deleted = '0';
        $emailObj->name = $mailer->Subject;
        $emailObj->description = $mailer->AltBody;
        $emailObj->description_html = $mailer->Body;
        $emailObj->from_addr = $mailer->From;

        if ($entityid)
        {
            $emailObj->parent_type = "Opportunities";
            $emailObj->parent_id = $entityid;
        }

        $emailObj->date_sent = TimeDate::getInstance()->nowDb();
        $emailObj->modified_user_id = $ownerid;
        $emailObj->created_by = $ownerid;
        $emailObj->assigned_user_id = $ownerid;
        $emailObj->status = 'sent';
        $emailObj->save();
    }
}

?>


1 Like

Same question years after.

I love workflow for automated emails.

But how to send workflow emails from another email than the system email?

Thanks

has this still not been addressed ? i mean dynamic CRM has had that since 2013s version . if this is the case i maybe going back to dynamic even if it cost me a upgrade on our on prem server.