set email opt_in when creating contact bean

Can anyone help with how you set a contacts primary email and set opt_out to 1 when a new contact bean is crated

        $contactbean                            = BeanFactory::newBean("Contacts");
        $contactbean->first_name                = $args['FirstName'];
        $contactbean->last_name                 = $args['LastName'];
        // $contactbean->email1                    = $args['Email'];
        $contactbean->portaluser_c              = $args['Email'];
        $contactbean->primary_address_country   = $args['MailingCountry'];
        $contactbean->primary_address_state     = $args['MailingState'];
        $contactbean->agreed_to_pp_c            = $args['Privacy_Policy'];
        $contactbean->save();

I read that i shouldn't be using email1 any more and instead use the email array

have tried 

        $contactbean->emailAddress->addAddress($args['Email']);
        //function addAddress($addr, $primary=false, $replyTo=false, $invalid=false, $optOut=false, $email_id = null, $validate = true)

        $bean = BeanFactory::newBean('EmailAddresses');
        $bean->getBeansByEmailAddress($args['Email']);
        $bean->emailAddress->addAddress($args['Email'], true, false, true, true);

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.0/Architecture/Email_Addresses/#Creat… 

this page says to pass it as a json object

  • I am not sure but looking at the above I can suggest this.

     $contactbean->emailAddress->addAddress($args['Email']);

    When adding an Email address  to Contact bean , you can make it opt_out .

    So above can be changed to 

            $bean->emailAddress->addAddress($args['Email'], true, false, true, true);

    Secondly.

    this line of code looks wrong

    $bean->getBeansByEmailAddress($args['Email']);

    it must be returning an array of objects as Email address can be associated with more than one Bean.

    Hope it helps.

  • Do you want all new email addresses to be set to opt_out = 1?

    Because you can do so by simply setting the flag from the Admin panel, I believe this was added with v8 and the GDPR Data Privacy Module.

    Admin->System Email Settings

    FrancescaS

  • hanks for this Francesca Shiekh we have set this now just also need a way to control by code

  • Hi Ashish Dwivedi, sorry i posted and didnt remove so test code, below was the newbean cotact script

            $contactbean                            = BeanFactory::newBean("Contacts");
            $contactbean->first_name                = $args['FirstName'];
            $contactbean->last_name                 = $args['LastName'];
            $contactbean->portaluser_c              = $args['Email'];
            $contactbean->primary_address_country   = $args['MailingCountry'];
            $contactbean->primary_address_state     = $args['MailingState'];
            $contactbean->agreed_to_pp_c            = $args['Privacy_Policy'];
            $contactbean->emailAddress->addAddress($args['Email'], true, true, true);
            $contactbean->save();

    the 

    $contactbean->emailAddress->addAddress($args['Email'], true, true, true);

    this isnt creating an email in the email_address table or creating an email linked to the contact even though the contact is being created success fully.

    Do you have to run this after $contactbean->save()?

  • I do the following for a lead w/ email address that is entered in code, see if it helps.

    I built this several YEARS ago, so it could probably use some improvements, but it works.

    $lead = BeanFactory::newBean('Leads');
    //assign all the various lead field values
    if(isset($input['first_name']) && !empty($input['first_name'])){
       $lead->first_name = $input['first_name'];
    }
    if(isset($input['last_name']) && !empty($input['last_name'])){
       $lead->last_name = $input['last_name'];
    }
    //and more...

    //save the lead and get the id
    $lead->save();
    $lead_id = $lead->id;

    //handle email address, note that we validate the address at the web form level, I add some extra checks to make sure there isn't a form out there that is passing an empty email address or no input for it. I also check lead_id out of an excess of caution

    if(isset($input['email_address']) && !empty($input['email_address'])&& !empty($lead_id)){
       $sea = new SugarEmailAddress();
       //set your email address properties when you create the email address here
       $sea->addAddress($input['email_address'],true);
       // Link the email to the lead bean
       if(isset($sea)){
          $mod = $lead->module_dir;
          $sea->save($lead_id, $mod);
       }
    }