[SalesForce] Case trigger send email to external user/contacts

i have a custom text field which allow user to enter email address, separate by ':' if multiple email address and a trigger on case to send an email to those email addresses entered by customer. but at the end the trigger not only send email to those email address in the text field, it also send email to the case's contact.

here is my trigger, how can i exclude the case's contact from the recipient?

trigger EscalateEmail on Case (after Insert,after update){
            case[] c = trigger.new;

            if(c[0].Status == 'Escalated')
            {

                if ( c[0].Escalate_Email__c !=null)
                 {
                     string templateName = 'Case Email Escalation';
                     String[] emailAddr = c[0].Escalate_Email__c.split(';');
                     EmailTemplate e = [select Id,Name,Subject,body from EmailTemplate where name like :templateName+'%'];
                     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                     List<Messaging.SendEmailResult> results = new list<Messaging.SendEmailResult>();
                     if(emailAddr.size() > 0){

                             System.Debug('Split emailAddr>>>>'+ emailAddr );

                             mail.setToAddresses(emailAddr );

                             mail.setTargetObjectId( c[0].ContactId);
                             mail.setWhatId(c[0].Id);

                             mail.setTemplateId(e.Id);

                             try {
                                     results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

                                     if (!results.get(0).isSuccess())                      
                                        System.debug('>>>>>>>>>>>>>> Error generated when sending mail - ' + results.get(0).getErrors()[0].getMessage());

                              } catch(Exception ex) {       

                              }

                     }

                }     

        }

}

Best Answer

The email is also sent to the Case contact because of this

mail.setTargetObjectId( c[0].ContactId);

The question is: Do you really need to use the template specified ? If yes, you may need to consider creating a fake contact or user (e.g a free chatter user) in your system which will be spammed with all emails sent via this functionality (not nice...)

If you don't need the template, you can very easily skip the above command and only specify the toAddresses.