2

When I refresh a sandbox I have a class that runs to fix some things like email addresses and custom settings. I've also been asked to email the developer the new org ID so that their testing environments can stay connected.

The problem is when I refresh the sandbox, email deliverability defaults to "system email only", and I cannot switch it to "All Emails" until after logging in, which is well after the refreshpostcopy has run. This means I can't email as part of the refreshpostcopy.

Is there a way to set email deliverability to "all emails" in apex? If not is there another way to get the developer that information?

    String sysemail = [SELECT Email FROM User WHERE Email like 'myusername' LIMIT 1].email;

    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    List<String> sendTo = new List<String>();
    sendTo.add('myemail@test.com');
    mail.setToAddresses(sendTo);
    mail.setSubject('Subject');
    mail.setPlainTextBody('New Sandbox ID is '+context.organizationId());
    mails.add(mail);
    Messaging.sendEmail(mails);

The error I get is System.EmailException: SendEmail failed. First exception on row 0; first error: NO_MASS_MAIL_PERMISSION, Single email is not enabled for your organization or profile.: []

  • 1
    you could do a callout to something like slack or twilio (sms) if you use tools like these – cropredy May 3 '19 at 22:15
  • I'll look into that thanks. We use slack. No idea what that entails though – denvergreene May 3 '19 at 23:20
  • It is pretty easy. I did a trivial slack integration when Community user asked a question. Worked almost immediately. – cropredy May 3 '19 at 23:51
1

When you create/refresh sandbox, Email Deliverability default to System Email Only. System Email Only allows us to send emails to users or reset passwords.You can run below class, when you create/refresh sandbox. enter image description here

When you run below class then it will update all System Admin's user's email address and that will eventually send email to user.

global class SandboxPostRefresh_AC implements SandboxPostCopy { 

    global void runApexClass(SandboxContext context) { 

        System.debug(context.organizationId()); 

        System.debug(context.sandboxId()); 

        System.debug(context.sandboxName()); 

        run(); 

    } 

   global static void run() { 

   //List of all emails from the User object  

    List<User> userEmailList = [select Email from User where profile.name = ‘System Administrator’]; 

    for(User uc : userEmailList) 

    { 

        uc.Email = uc.Email.replace('=','@'); 

        //to remove appended domain 

        String addedPhrase = '@example.com'; 

        uc.Email = uc.Email.remove(addedPhrase); 

        userEmailList.add(uc); 

    } 

    if(userEmailList.size() > 0) 

    { 

         Update userEmailList; 

    } 

    } 
} 
  • Hi sfdc. I've just clarified my question a little bit more. I have a version of that class that runs when my sandbox is refreshed and it works. But I can't send an email as part of the post refresh because the default deliverability settings. I need to change the deliverability settings in apex – denvergreene Apr 25 '19 at 18:16
  • 'System Email Only' should allow emails to Users.You don't need to set it to 'All Emails'.Please check this link help.salesforce.com/… – sfdc Apr 25 '19 at 18:19
  • System email only: Allows only automatically generated emails, such as new user and password reset emails. Especially useful for controlling email sent from sandboxes so that testing and development work doesn’t send test emails to your users. Newly created sandboxes default to System email only. – sfdc Apr 25 '19 at 18:19
  • 1
    OOH okay I understand. I was currently hardcoding the email address, but I should just make it email the user I'm looking for directly. I understand now, thanks! – denvergreene Apr 25 '19 at 18:22
  • I'm still not able to send emails when referencing a user ID directly. I've added code to the question regarding the email construction. – denvergreene May 3 '19 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.