function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aaron2010Aaron2010 

Emails to "TO" and "CC"

Hello All,

                 We have a registration system where the user receives Registration email upon registration.  Here we used Visualforce email Template. Is it possible, if the registrant do not want to receive email after the registration and he wants email to be sent only to CC address.

 

Here is code that is used to send emails.. I m posting only the part of code that is used for emails...

 

Here before calling sendEventConfirmation i am changing the email of the contact to cc emailaddress and after email is sent i am resetting back to old email.  But still the email is reaching both To and CC addresses...

 

 

 private void sendConfirmation() {
                   
         if (this.contact.IsPersonAccount || this.registrantIsDoctor) {
             this.oldemail = this.email;
             this.account.PersonEmail =  this.reg.CC_Email__c;
             this.contactId = RegistrantPersister.persistAccount(this.account);
         } else{
                 this.oldemail = this.email; 
                 this.contact.Email = this.email;
                 this.contactId = RegistrantPersister.persistContact(this.contact);
         }
        RegistrationEmailSender.sendEventConfirmation(this.eventId, this.reg.Id, this.contactId, this.reg.CC_Email__c, this.event.Planner_Email__c, testingEmail);
        
         if (this.contact.IsPersonAccount || this.registrantIsDoctor) {
                 this.account.PersonEmail =  this.oldemail;
                 this.contactId = RegistrantPersister.persistAccount(this.account);
         } else{
                 this.contact.Email = this.oldemail;
                 this.contactId = RegistrantPersister.persistContact(this.contact);
         }
     
        
        }
}

 

 

 

 

 

 webservice static Boolean sendEventConfirmation(Id eventId, Id registrantId, Id contactId, String ccEmailAddress, String plannerEmailAddress, Boolean testing) {
        Boolean emailSent = false;
        // Have to query to get the Id of the template and From address
        EmailTemplate et = [select Id from EmailTemplate where DeveloperName = 'Event_Registration_Confirmed' limit 1];
        OrgWideEmailAddress oe = [Select Id From OrgWideEmailAddress where DisplayName = 'Events' and Address = 'gem.meetings@events.com' limit 1];
        
        // Load attachments
        Integer totalAttachmentSize = 0;
        Attachment[] atts = [select id, name, body, bodylength from attachment where parentid = :eventId and name not in ('header', 'footer')];
        List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
        
        for(Attachment a : atts) {
            totalAttachmentSize += a.bodylength;
            Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
            fileAttachment.setFileName(a.name);
            fileAttachment.setBody(a.body);
            fileAttachments.add(fileAttachment);
        }
        
        string email1 = [select id,email from contact where id = :contactId][0].email;
        system.debug('this is email id that is changed------------------>>>>>>>'+email1);
                
        // Build mail sending object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        // Sender
        //mail.setSenderDisplayName('Events');           
        //mail.setReplyTo(plannerEmailAddress);
        mail.setOrgWideEmailAddressId(oe.id);
        
        // Recipient(s) and Content
        mail.setTargetObjectId(contactId);
        mail.setWhatId(registrantId);
        mail.setTemplateId(et.id);
              
        if (ccEmailAddress != null && ccEmailAddress != '') {
            List<String> ccAddresses = new List<String>();
            ccAddresses.add(ccEmailAddress);
            mail.setCcAddresses(ccAddresses);
        }
             
        if (fileAttachments.size() > 0 && totalAttachmentSize < 10485760) {
            mail.setFileAttachments(fileAttachments);
        }
        
        // Other misc options 
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        
         
        if (!testing) {
            List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            if (results.get(0).isSuccess())
                emailSent = true;
        }
            
        return emailSent;
    }
}

 So is there any other way to stop emails to "TO" address and send only to cc address.

 

Any help would be appreciated.

 

Thanks

 

Walter@AdicioWalter@Adicio

i dont think so , targetObjectId or To is requried at least. i guess if you sent "to" a bogus addres like no-reply@your company, and CC the user.

Aaron2010Aaron2010

Thanks for your response. Here targetobjectId is must as we are using visualforce email template and for the merge fields to display properly in email's. 

What I want is, is there a way to change the "TO" email address before the email is sent, and set it back to the old email once the email is sent.

 

I tried to change the contact's email to cc email address before calling sendEventConfirmation and set it back to original email. But this is not working.

 

Thanks