1

Is there a way to set a single email address to which all outgoing emails will be sent from Sandbox? Also, ensuring that no emails are sent to customers during testing?

I found this idea on IdeaExchange, but it's 7 years old and it's not yet delivered.

I tried setting a trigger on EmailMessage object that overrides the ToAddress field, however that doesn't seem to solve the problem.

4
  • One way is to update email address of all the users you will use in Sandbox to the email address you want the emails to be delivered.
    – jagmohan
    Commented Aug 29, 2016 at 7:19
  • Can you share your code Commented Aug 29, 2016 at 7:22
  • @krishsfdc I don't really have any code to share - the trigger I mentioned was just a quick test I performed and it didn't even triggered when I sent an email.
    – smukov
    Commented Aug 29, 2016 at 7:29
  • @jagmohan Updating all email addresses for all contacts is a possible solution, however, that would cause our other validation rules to be triggered, since we don't allow duplicate email addresses in a system. I admit that this validation could be disabled, however, I was hoping for another solution.
    – smukov
    Commented Aug 29, 2016 at 7:30

1 Answer 1

0
 if([SELECT Id, IsSandbox FROM Organization LIMIT 1].IsSandbox)

    {
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    // Set recipients to two contact IDs.
    // Replace IDs with valid record IDs in your org.
    //Create a contact with the email Id to which all the emails should be sent
    message.toAddresses = new String[] { '003D000000QDexS'};
    message.optOutPolicy = 'FILTER';
    message.subject = 'Opt Out Test Message';
    message.plainTextBody = 'This is the message body.';
    Messaging.SingleEmailMessage[] messages = 
        new List<Messaging.SingleEmailMessage> {message};
             Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
    if (results[0].success) {
        System.debug('The email was sent successfully.');
    } else {
        System.debug('The email failed to send: '
              + results[0].errors[0].message);
    }

    }

Try this.

2
  • That's not what I'm looking for. I need a way to intercept outgoing emails and send them to a single, predefined email address. How does your code intercepts outgoing emails sent from a WF, for example?
    – smukov
    Commented Aug 29, 2016 at 7:51
  • I don't there is any way to do so.. Commented Aug 29, 2016 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .