I have a custom visual force page with several contact fields . User will enter data and hit save button. Once Save button is hit , it should send email to the emailid specified on emailid field on the page. I have written a controller to send email. As of now, I hard coded emailid for Toaddress field.This is working fine. How can I use !contact.email in controller so that the email will go to the !contact.email
Here is the part of the controller
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] { 'abcd@gmail.com' };
// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setReplyTo('xyz@yahoo.com');
mail.setSenderDisplayName('KFC');
mail.setSubject('Your upcoming shift with KFC');
mail.setBccSender(false);
mail.setPlainTextBody('Hi,I am sending a email');
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Here is the visual force page
<apex:inputField value="{! contact.firstname}"/>
<apex:inputField value="{! contact.lastname}"/>
<apex:inputField value="{! contact.Title}"/>
<apex:inputField value="{! contact.email}"/>
<apex:inputField value="{! contact.phone}"/>
<apex:inputField value="{!contact.MailingStreet}"/>
<apex:pageBlockButtons location="bottom" >
<apex:commandbutton value="Submit" action="{!save}"/>
</apex:pageBlockButtons>
Thank you all
Best Answer
So, since you're code is incomplete I'm going to make a quick assumption:
Your extension will NOT see the contact directly. You have to create a new variable in your extension that holds the contact provided by the standard controller:
Once you do that, you should be able to fill your email address by referencing the new variable within your method:
If you are not using the standard set controller with an extension; rather, you are using a completely custom controller, that means you have something like this in your controller:
Shame on you...
Regardless, the answer is much easier: