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
Uttpal chandraUttpal chandra 

How to send email on custom button

Hi all,
I want to send an email as well as pdf which contain the fields of contacts whenever user click on custom send email button.
Best Answer chosen by Uttpal chandra
Khan AnasKhan Anas (Salesforce Developers) 
Hi Uttpal,

Please try the below code:
 
public class ConPdfEmailC{
    Contact con;
    
    public ConPdfEmailC(ApexPages.StandardController stdController) 	{
        
        //con = [select Id, FirstName, LastName, Phone, Email from Contact where Id = :ApexPages.currentPage().getParameters().get('id')];
        con = (Contact)stdController.getRecord();
    }
    
    public pagereference sendAttach() {
        for(Contact cc : [SELECT Id, Name, Email__c FROM Contact WHERE Id =: con.Id]){
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.ConPdfEmail; // Visualforce Page Name
            pref.getParameters().put('id',(String)con.id);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('ContactDetails.pdf');
            attach.setBody(b);
            semail.setSubject('Contact details');
            List<String> sendTo = new List<String>();
            sendTo.add(cc.Email__c);
            semail.setToAddresses(sendTo);
            semail.setPlainTextBody('Please find the attached contact details');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        }
        return null;
    }
}

Regards,
Khan Anas

All Answers

Uttpal chandraUttpal chandra
No Chris, not like that it should automatically send email with pdf  when user click on "Send Email" button
Khan AnasKhan Anas (Salesforce Developers) 
Hi Uttpal,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce: ConPdfEmail
<apex:page standardController="Contact" renderAs="pdf">
    <apex:pageBlock >
        <apex:pageBlockSection columns="1">
            <apex:outputField Value="{!Contact.FirstName}"/>
            <apex:outputField Value="{!Contact.LastName}"/>
            <apex:outputField Value="{!Contact.Email}"/>
            <apex:outputField Value="{!Contact.Phone}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Controller:
public class ConPdfEmailC{
    Contact con;
    
    public ConPdfEmailC(ApexPages.StandardController stdController) 	{
        
        //con = [select Id, FirstName, LastName, Phone, Email from Contact where Id = :ApexPages.currentPage().getParameters().get('id')];
        con = (Contact)stdController.getRecord();
    }
    
    public pagereference sendAttach() {
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        PageReference pref = page.ConPdfEmail; // Visualforce Page Name
        pref.getParameters().put('id',(String)con.id);
        pref.setRedirect(true);
        Blob b;
        b = pref.getContent();
        attach.setFileName('ContactDetails.pdf');
        attach.setBody(b);
        semail.setSubject('Contact details');
        String[] sendTo = new String[]{'khananasrehmani@gmail.com'};
            semail.setToAddresses(sendTo);
        semail.setPlainTextBody('Please find the attached contact details');
        semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        return null;
    }
}

Visualforce: ConPdfEmail1
<apex:page standardController="Contact" extensions="ConPdfEmailC" action="{!sendAttach}">
	<apex:outputLabel value="Email Sent Successfully"/>
</apex:page>

Create a custom detail page button using ConPdfEmail1 visualforce page and add it to page layout.

Please refer to the below link which might help you further with the above requirement.

https://developer.salesforce.com/forums/?id=9060G000000Xj73QAC

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Raj VakatiRaj Vakati
If you are on lightning here is the the link

http://sfdcmonkey.com/2017/01/03/send-email-lightning-component/
Uttpal chandraUttpal chandra
Hi Anas,

Your code is working correctly but can you help me on this part.

It should fetch email address from the custom field and send email to that email address whenever i click on the "send email" button.

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Uttpal,

Please try the below code:
 
public class ConPdfEmailC{
    Contact con;
    
    public ConPdfEmailC(ApexPages.StandardController stdController) 	{
        
        //con = [select Id, FirstName, LastName, Phone, Email from Contact where Id = :ApexPages.currentPage().getParameters().get('id')];
        con = (Contact)stdController.getRecord();
    }
    
    public pagereference sendAttach() {
        for(Contact cc : [SELECT Id, Name, Email__c FROM Contact WHERE Id =: con.Id]){
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.ConPdfEmail; // Visualforce Page Name
            pref.getParameters().put('id',(String)con.id);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('ContactDetails.pdf');
            attach.setBody(b);
            semail.setSubject('Contact details');
            List<String> sendTo = new List<String>();
            sendTo.add(cc.Email__c);
            semail.setToAddresses(sendTo);
            semail.setPlainTextBody('Please find the attached contact details');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        }
        return null;
    }
}

Regards,
Khan Anas
This was selected as the best answer
Uttpal chandraUttpal chandra
Hi Anas,

The code is not working and also it is not giving any error.
Right now when i Click on the button it is moved to the ConPDFEmail1 page without giving any error.
Khan AnasKhan Anas (Salesforce Developers) 
It is working fine for me. If you are not getting an email in the inbox, I suggest you check the spam folder of your email. See screenshot:


User-added image

Regards,
Khan Anas
Uttpal chandraUttpal chandra
Hey Anas,

Can you help me same thing for lightning side also?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Uttpal,

I have replied with sample code to your new question (send an email with attached PDF in lightning) which you have posted on forums:

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IF7BQAW

Regards,
Khan Anas