1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public void sendCustomEmail(User selectedUser, Contact selectedContact, String templateName, Map<String, String> customVariables){
EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName=:templateName];
if (selectedContact==null){
selectedContact = selectedUser.Contact;
}
String customSubject = emailTemplate.Subject;
if (customVariables.containsKey('joinContactName')){
customSubject = customSubject.replace('{!JointContactName}', customVariables.get('joinContactName'));
}
if (customVariables.containsKey('ThreadId')) {
customSubject = customSubject.replace('{!ThreadId}', customVariables.get('ThreadId'));
}
String htmlBody = emailTemplate.HtmlValue;
htmlBody = htmlBody.replace('{!Contact.FirstName}',selectedContact.FirstName);
htmlBody = htmlBody.replace('{!Contact.Email}', selectedContact.Email);
if (customVariables.containsKey('Link')){
htmlBody = htmlBody.replace('{!CustomUrl}', customVariables.get('Link'));
} else if (customVariables.containsKey('link')){
htmlBody = htmlBody.replace('{!CustomUrl}', customVariables.get('link'));
} else if (customVariables.containsKey('joinContactName')){
htmlBody = htmlBody.replace('{!JointContactName}', customVariables.get('joinContactName'));
}
if (customVariables.containsKey('ThreadId')){
htmlBody = htmlBody.replace('{!ThreadId}', customVariables.get('ThreadId'));
}
String plainBody = emailTemplate.Body;
plainBody = plainBody.replace('{!Contact.FirstName}',selectedContact.FirstName);
plainBody = plainBody.replace('{!Contact.Email}', selectedContact.Email);
if (customVariables.containsKey('Link')){
plainBody = plainBody.replace('{!CustomUrl}', customVariables.get('Link'));
} else if (customVariables.containsKey('link')){
plainBody = plainBody.replace('{!CustomUrl}', customVariables.get('link'));
} else if (customVariables.containsKey('joinContactName')){
plainBody = plainBody.replace('{!JointContactName}', customVariables.get('joinContactName'));
}
if (customVariables.containsKey('ThreadId')){
plainBody = plainBody.replace('{!ThreadId}', customVariables.get('ThreadId'));
}
Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
List<OrgWideEmailAddress> orgWideEmailAddress = [Select Id, Address from OrgWideEmailAddress where DisplayName=:ORGWIDEEMAILADDRESSNAME];
if (customVariables.containsKey('ThreadId')){
List<EmailServicesAddress> emailServicesAddress = [SELECT Id,AuthorizedSenders,EmailDomainName,IsActive,LocalPart FROM EmailServicesAddress where IsActive=true and (LocalPart like '%service%' or LocalPart like '%support%')];
if (!emailServicesAddress.isEmpty()){
EmailServicesAddress emailToCase = emailServicesAddress.get(0);
email.setReplyTo(emailToCase.LocalPart +'@'+ emailToCase.EmailDomainName);
email.setSenderDisplayName('Client Support');
} else {
email.setReplyTo('customer-service@gmail.com');
email.setSenderDisplayName('Client Support');
}
}
email.setTargetObjectId(selectedContact.Id);
email.setSaveAsActivity(true);
email.setSubject(customSubject);
email.setHtmlBody(htmlBody);
email.setPlainTextBody(plainBody);
if (customVariables.containsKey('WhatId')){
email.setWhatId(customVariables.get('WhatId'));
}
Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});
}