0

Here is a method that fires when a guest enters their name and email address into a visualforce page and clicks submit. The email is being sent fine, but the primary contact on the org is also getting put in to the "To" field. A system.debug shows the the primary contact email address is not in the email list when I do the system.debug. Where, when, and why is this person's address getting added? I do not want them to get an email every time someone registers.

public pageReference makePendingUser(){
        List<EmailTemplate> templates = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Community_New_Member_Welcome_Email'];
        String templateId = templates[0].Id;
        List<Messaging.Email> emails = new List<Messaging.Email>();
        User randomUser = [SELECT Id FROM User LIMIT 1];
        OrgWideEmailAddress admissionsEmail = [SELECT Id FROM OrgWideEmailAddress WHERE DisplayName = 'Admissions' LIMIT 1];
        List<String> emailAddresses = new List<String>();

        newUser.Random_String__c = createRandomString();
        insert newUser;
        emailAddresses.add(newUser.Email__c);

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setTemplateId(templateId);
        email.setTargetObjectId(randomUser.Id);
        email.setToAddresses(emailAddresses);
        email.setOrgWideEmailAddressId(admissionsEmail.id);
        email.saveAsActivity = false;
        emails.add(email);
        Messaging.sendEmail(emails);

        return null;
    }

1 Answer 1

1

You're setting the target object Id to a User chosen at random:

    User randomUser = [SELECT Id FROM User LIMIT 1];

    email.setTargetObjectId(randomUser.Id);

The targetObjectId is defined as

Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

There's some puzzling code here (what is the random string for?) and newUser is not defined, so I'm not entirely sure what you're aiming at, but Apex is doing just what it thinks you are asking it to do.

If you don't want to send an email to a User, and you do want to render a template, you'll need to use a different value or call setTreatTargetObjectAsRecipient(false) to tell Apex not to send the email to the target object. Note that doing so exposes a potential data leak, as merge fields will still be populated from the target object.

1
  • Thanks for your response. I am trying to implement a workaround for the template that requires I provide a Contact, User, or Lead when the person I want to email is not in our system yet. I was told that if I call setTargetObjectId with any user and then call setToAddresses with the email I REALLY want, that would work. But I also needed to call setTreatTargetObjectAsRecipient(false) as you said. This worked perfectly and removed the email for the randomUser. Thank you so much!
    – Navigatrix
    Commented Jun 5, 2020 at 16:26

You must log in to answer this question.

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