public void sendClientEmail() {
Set<String> repSet = new set<String>();
map<String, Map<string, Bottling_Lot__c>> repMap = new map<String, Map<string, Bottling_Lot__c>>(); // email <account Id, bottling lot>
Map<string, Bottling_Lot__c> acctMap;
// build a set of all the owner Id that will have to be emailed
if (botLotList.size() > 0) {
for (Bottling_Lot__c bot:botLotList) {
repSet.add(bot.client_lookup__r.owner.email);
}
for (String s:repSet) {
acctMap = new map<String, Bottling_lot__c>();
for (Bottling_Lot__c bot:botLotList) {
if (s == bot.client_lookup__r.owner.email) {
acctMap.put(bot.client_lookup__c, bot);
}
}
repMap.put(s, acctMap);
}
system.debug('repMap = ' + repMap);
}
// Define the email
for (String s:repSet) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {};
String[] ccAddresses = new String[] {};
toAddresses.add('mathew@terravant.com');
blob body;
BottlingEmail_Class botClass = new BottlingEmail_Class(repMap);
PageReference htmlPage = page.bottlingEmail_Page;
body = htmlPage.getContent();
String htmlBody = body.toString();
email.setToAddresses(toAddresses);
email.setSubject('test');
email.setHtmlBody(htmlBody);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
} // end the send email method
<apex:page controller="BottlingEmail_Class" >
<p>
Hello, the following items were bottled for your clients
</p>
<table>
<tr>
<th>Date</th>
<th>Account</th>
<th>Cases</th>
</tr>
<apex:repeat value="{!botLotList}" var="v">
<tr>
<td>{!v.Bottling_Shift__r.Bottling_Day__r.Bottling_Date__c}</td>
<td>{!v.client_lookup__r.name}</td>
<td>{!v.Actual_Bottled__c}</td>
<td>testing</td>
</tr>
</apex:repeat>
</table>
<p>
end of page
</p>
</apex:page>
public class BottlingEmail_Class {
public map<String, Map<string, Bottling_Lot__c>> repMap {get; set;} // email <account Id, bottling lot>
public List<Bottling_Lot__c> botLotList {get; set; }
public BottlingEmail_Class(map<String, Map<string, Bottling_Lot__c>> repMap) {
botLotList = new List<bottling_Lot__c>();
this.repMap = repMap;
for (string key :repMap.keySet()) {
Map<String, Bottling_lot__c> acctMap = new Map<String, Bottling_Lot__c>();
acctMap = repMap.get(key);
for (Bottling_Lot__c lot :acctMap.values() ) {
botLotList.add(lot);
}
}
} // end the constructor
}
Then you need to go for visualforce template and associate a controller where you can pass the list just we pass in visualforce page.Here is an example how to do it https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm