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
SV MSV M 

Email attachment is not able to read merge field values

Hi, I have a batch class which will send email to contact owner along with attachment with some merge fields. I was able to send the attachment but the merge fields are not taking values from Contact object. Can someone help me to resolve this? 

//Batch Class

global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
    public String Subject{get;set;}
    //Public String Body{get;set;}
    Public String attname{get;set;}
    Public blob attbody{get;set;}
    Map<Id,String> contactOwnerEmail = new Map<Id,String>();
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT FirstName, LastName, Email, MobilePhone FROM Contact';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> scope) {
        /*for(Contact con : scope) {
            contactOwnerEmail.add(con.Id, con.Owner.Email);
        }*/
    }
    global void Finish(Database.BatchableContext BC) {
        messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
        EmailTemplate ev=[SELECT Id, Name, Subject, Body, HtmlValue 
                          FROM EmailTemplate 
                          WHERE name = 'Test Template'];
        Blob b = Blob.valueOf(ev.Body);
        Subject = ev.Subject;
        //Body = ev.Body;
        email.setSubject(Subject);
        email.setPlainTextBody('Please Find the Attachment for Details about Contact');
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(attname);
        efa.setBody(b);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}

At the setToAddress, I need to use Contact Owner Email but I was unable to get the Contact Owner Email. I have attached the image for your reference.User-added image