+ Start a Discussion
SFineSFine 

Contact Page problems

Hey guys,

 

At the moment I'm working on a Contact Page. The good news is that it does send a message. The bad news is, while I know how to check to see if necessary fields are missing, I don't know how to to show that to the user.

 

Furthermore, I want when a email IS sent to link the user to a page saying that they have sent it. Or at least give them a message.

 

Here is my code and controller for both pages

 

 

<apex:page title="Configero - Company" showHeader="false" controller="sendEmail">
    <title>Configero - Contact Us</title>
    <apex:composition template="ConfigeroTemplate">
        <apex:define name="mainContent">

             <apex:outputPanel styleclass="block" style="float:left;display:inline;width:445px">
                <div class="blockTitle"><span class="blue">Contact</span> Us</div>
                <div class="blockBody">
                    <p>If you have any questions or would like a quote for one of our services, please call 404.229.2232 or contact us via email at <a href="mailto:info@configero.com">info@configero.com</a>.</p>
                    <apex:form >
                        Your Name <span class="red">*</span> <br/><apex:inputText id="name" value="{!conname}" size="50"/> <br/>
                        Email Address <span class="red">*</span><br/><apex:inputText id="email" value="{!remail}" size="50"/> <br/>
                        Subject <span class="red">*</span><br/><apex:inputtext id="subject" value="{!subject}" size="50"/><br/>
                        Message <span class="red">*</span><br/><apex:inputTextarea id="message" value="{!body}" cols="59" rows="5"/> <br/>
                        <apex:commandButton value="Send Email" action="{!send}"/>
                    </apex:form>
                </div>
            </apex:outputPanel> 
            
        </apex:define>
    </apex:composition>
</apex:page>

 

public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }
    public String REmail {get; set; }
    public String ConName {get; set;}

    private final Account account;

    // Create a constructor that populates the Account object  
    
    /*public sendEmail() {
        account = [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts) 
                from Account where id = :ApexPages.currentPage().getParameters().get('id')];
    }*/

    public Account getAccount() {
        return account;
    }

    public PageReference send() {
        // Define the email  
        if(REmail == '')
        {
           //Fields not entered
           return null;
        }
        else if(ConName == '')
        {
            return null;
        }
        else if(Subject == '')
        {
            return null;
        }
        else if(body == '')
        {
            return null;
        }
        else
        {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 



        String[] toAddresses = new String[] {'email@gmail.com'};

        // Sets the paramaters of the email  
    
        email.setReplyTo( REmail);
        email.setSubject( 'New Contact Message: '+subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( 'Message: ' + body +'\r Signed: '+ConName );
    
        // Sends the email  
    
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        
        return null;
        }
    }
}

 Any advice is appreciated. Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy_nJeremy_n

To return a message to the page when it fails your validation, create an ApexPages.Message with your warning text, and add it to the current page.

 

 

ApexPages.Message noEmailEntered = new ApexPages.Message( ApexPages.Severity.Warning, 'You must complete the Email Address field.');
ApexPages.addMessage(noEmailEntered);

You will also have to add a line to the top of your VF page to show this message:

<apex:messages />

 

To send the user to a different page on successful email send, you just need to return a meaningful PageReference, rather than null.

 

PageReference returnpage = Page.SuccessfulEmail;
return returnpage;

 

Good luck,

 

Jeremy

All Answers

Jeremy_nJeremy_n

To return a message to the page when it fails your validation, create an ApexPages.Message with your warning text, and add it to the current page.

 

 

ApexPages.Message noEmailEntered = new ApexPages.Message( ApexPages.Severity.Warning, 'You must complete the Email Address field.');
ApexPages.addMessage(noEmailEntered);

You will also have to add a line to the top of your VF page to show this message:

<apex:messages />

 

To send the user to a different page on successful email send, you just need to return a meaningful PageReference, rather than null.

 

PageReference returnpage = Page.SuccessfulEmail;
return returnpage;

 

Good luck,

 

Jeremy

This was selected as the best answer
SFineSFine

Much thanks. The site looks better and works delightfully. Just one more thing: The site crashes when there is something in the email field that doesn't resemble a email.

 

Any advice on how to check for that?

Jeremy_nJeremy_n

One thing is you can be a little more picky in your validation. For example, this is a quick email validation line I've used:

 

if ((!em.contains('@')) || (!em.contains('.')) || (em.length() < 8) || em.contains(' ')) {
   //handle bad email
}

 

Another thing you can do as a second protection against exceptions is to wrap your Messaging.SendEmail statement in a try {} catch{} construction, and handle the exception with an error message.

 

Jeremy

SFineSFine

Thank you very much that helped.

 

One last thing and it's something of a doozy. Is it possible to make a contact-like page where a user can upload a file and submit it?

 

I've seen the  page on creating a email attachment but it seems to be different than what I'm intended. You can see it here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_sending_attachments.htm

 

Any advice?