You need to sign in to do that
Don't have an account?
lakshman.matti
Sending Vf page as PDF as attachment.
Hi Everyone,
I have create a vf page and custom controller .page contains 2 input to take user inputs.when user gives values and saves the record using custom button.once it is saved i need to send that page vf page as attachment in pdf format to the mail what we have speciefied in vf page.
here i'm pasting my code.please corrrect my code.
VF PAGE:
---------------------
<apex:page controller="sendingMailAttachment"> <apex:form > <apex:messages /> <apex:outputLabel >Name</apex:outputLabel> <apex:inputText value="{!name}"/><br/> <apex:outputLabel >Mail</apex:outputLabel> <apex:inputText value="{!cmail}"/><br/> <apex:commandButton action="{!Submit}" value="Submit"/> </apex:form> </apex:page>
controller:
-----------
public class sendingMailAttachment {
public String cmail { get; set; }
public String name { get; set; }
public sendingMailAttachment (){}
public pagereference Submit(){
contact Conval=new contact();
conval.lastname=name ;
conval.Email=cmail ;
insert conval;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info, 'contact inserted');
ApexPages.addMessage(myMsg);
// Reference the attachment page and pass in the ID
PageReference pdf = Page.emailAttachmentEx1;
pdf.getParameters().put('id',conval);
// Take the PDF content
Blob b = pdf.getContentAsPDF();
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setContentType('application/pdf');
efa.setFileName('attachment.pdf');
efa.setInline(false);
efa.setBody(b);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {conval.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('New contact is created');
mail.plainTextBody = 'Name : '+conval.lastname+'\n'+'Mail Id : '+conval.email;
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
return null;
}
}
---
above code is sending attachment with just labels. i want the inputs user give along with labels.
Thanks
Lakshman
I have create a vf page and custom controller .page contains 2 input to take user inputs.when user gives values and saves the record using custom button.once it is saved i need to send that page vf page as attachment in pdf format to the mail what we have speciefied in vf page.
here i'm pasting my code.please corrrect my code.
VF PAGE:
---------------------
<apex:page controller="sendingMailAttachment"> <apex:form > <apex:messages /> <apex:outputLabel >Name</apex:outputLabel> <apex:inputText value="{!name}"/><br/> <apex:outputLabel >Mail</apex:outputLabel> <apex:inputText value="{!cmail}"/><br/> <apex:commandButton action="{!Submit}" value="Submit"/> </apex:form> </apex:page>
controller:
-----------
public class sendingMailAttachment {
public String cmail { get; set; }
public String name { get; set; }
public sendingMailAttachment (){}
public pagereference Submit(){
contact Conval=new contact();
conval.lastname=name ;
conval.Email=cmail ;
insert conval;
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.info, 'contact inserted');
ApexPages.addMessage(myMsg);
// Reference the attachment page and pass in the ID
PageReference pdf = Page.emailAttachmentEx1;
pdf.getParameters().put('id',conval);
// Take the PDF content
Blob b = pdf.getContentAsPDF();
// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setContentType('application/pdf');
efa.setFileName('attachment.pdf');
efa.setInline(false);
efa.setBody(b);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {conval.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('New contact is created');
mail.plainTextBody = 'Name : '+conval.lastname+'\n'+'Mail Id : '+conval.email;
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
return null;
}
}
---
above code is sending attachment with just labels. i want the inputs user give along with labels.
Thanks
Lakshman
be:
Can you elaborate your problem:
----
above code is sending attachment with just labels. i want the inputs user give along with labels.
----
You may creata a new VF page and controller. Query the record using ID and just display the data on page. Use this page as a refernce of PDF in above controller "sendingMailAttachment".
Also, as Rahul mentioned it should be ID that you have to pass instead of object itsself.