You need to sign in to do that
Don't have an account?
Setting the To address in an email from a form
In the To part of my send email page in VF below I want to be able to populate the input field with an email address that's recorded at {!Member__c.Email__c} that someone has entered when creating a new record in my Member object.
Right now I have to manually type in an address. Everything works right and email gets sent but it's important that when sending email users don't need to type in this address.
When I change the value for the inputText field in VF from value={!emailTo} to value={!Member__c.Email__c} the email address is shown but there is no valid To address to be able to send an email in mail.setToAddress so it fails.
Any help or ideas on this would be much appreciated!!
Apex
public class SendEmailPage { public SendEmailPage(ApexPages.StandardController controller) { } public String emailTo {get; set;} public String emailSubject {get; set;} public String emailBody {get; set;} public String response {get; set;} public PageReference sendEmail(){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] {emailTo});
mail.setOrgWideEmailAddressId('0D2A0000000PBAC'); mail.setReplyTo('no-reply@company.com'); mail.setSubject(emailSubject); mail.setHtmlBody(emailBody); try{ Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); if(resultMail[0].isSuccess()) { PageReference page = System.Page.messagesent; page.setRedirect(true); return page; } else{ response = resultMail[0].getErrors().get(0).getMessage(); } }catch(System.EmailException ex){ response = ex.getMessage(); } return null; } }
VF Code
<apex:form > To: <apex:outputField value="{!Member__c.Name}" /><br/> <apex:inputText value="{!emailTo}" style="width: 200px"/><br/> Subject:<br/> <apex:inputtext value="{!emailSubject}" style="width: 400px"></apex:inputtext><br/><br/> Body:<br/> <apex:inputtextarea value="{!emailBody}" style="width: 400px; height: 100px"></apex:inputtextarea><br/> <apex:commandbutton value="Send Message" action="{!sendEmail}" rerender="statusMail"></apex:commandbutton> <apex:outputpanel id="statusMail" layout="block"> <strong><apex:outputtext value="{!response}"></apex:outputtext></strong> </apex:outputpanel> </apex:form>
I guess Memeber__c is known only to your standard controller, but not to your extension. You need to reference the Member__c record in your constructor. Check the VF guide for controller extensions (this one is for Accounts, just replace with Member__c, and you will have a "handle" for the record in your extension.):
http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_controller_extension.htm