0

I am attempting to use an after save logic hook to scrub bogus email addresses that are entered during testing, etc. However, the email address does not update. Below is the code I am using. I am currently on 6.5 enterprise. Does anyone have an idea of what I may be doing wrong? Or, how to correctly update / remove an email address through an after save logic hook?

Thanks!

    $sea = new SugarEmailAddress;
    $sea = $bean->emailAddress;

    foreach ($bean->emailAddress->addresses as $k=>$emailaddress ) {
        if( $ema = $emailaddress['email_address'] ) {
            if( 
                stripos($ema,'@none.com') !== FALSE || 
                stripos($ema,'@test.com') !== FALSE ||
                stripos($ema,'@nunya.com') !== FALSE ||
                stripos($ema,'@testing.com') !== FALSE
            ) {

                $sea->addresses[$k]['emailaddress'] = '' ;
                //sugar_die(print_r($sea->addresses));

                $sea->save($bean->id,$bean->module_dir);

            }
        }
    }
zxlg
  • 1

1 Answers1

0

To update user's email address in logic hook or custom import you can use below logic:

if($bean->email1) {
    $sea = new SugarEmailAddress();
    // Add a primary email address
    $sea->addAddress($bean->email1, true); 
    // Associate the email address with the given module and record
    $sea->save($user_id, "Users");
}
Sachin I
  • 1,500
  • 3
  • 10
  • 29
  • Thanks for your reply. This has helped me get started in the right direction. By using: `$sea->addAddress($ema, true, null, true);` where $ema is the bogus email I wish to remove, I am able to mark this address as invalid. However, I wish to completely remove this address (or update it with an empty string), so the user is prompted to enter a new address upon editing the record (since it is a required field). Any ideas on how to set the email address for removal? Thanks again! – zxlg Apr 19 '16 at 20:18