Search code examples
salesforceapex-codevisualforceforce.com

VISUALFORCE /APEX : Simple email feedback form


I am developing a site in Visualforce and would like to offer user a simple form to send me feedback via email. There would be 3-4 fields like name, user's email, reason and feedback and "send" button. Clicking the send button should automatically send that message to my email address.

I do not want to store the form data in salesforce at least for now...All the stuff I found online about visualforce/apex and email is about saving that data to salesforce too.

Can I just make use of apex's email capabilities and send out email without storing that data anywhere in salesforce?

Thanks, Calvin


Solution

  • It's not required to insert/update/delete any records in the database when executing an action on a VisualForce page. You can leverage the Outbound Email functionality to send out your notification. For something like this, you will probably want to familiarize yourself with the SingleEmailMessage methods.

    A simple example to get you going:

    public PageReference actionSend() {
        String[] recipients = new String[]{'[email protected]'};
        Messaging.reserveSingleEmailCapacity(recipients.size());
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setToAddresses(recipients);
        msg.setSubject('Test Email Subject');
        msg.setHtmlBody('Test body including HTML markup');
        msg.setPlainTextBody('Test body excluding HTML markup');
        msg.setSaveAsActivity(false);
        msg.setUseSignature(false);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {msg}, false);
        return null;
    }
    

    If you are interested in sending these outbound messages from a dedicated email address (something like [email protected]), you can set these up through the Setup -> Administration Setup -> Email Administration -> Organization-Wide Addresses menu. Once you have created an org-wide address, grab the Id from the URL and use the setOrgWideEmailAddressId(Id) method on your instance of Messaging.SingleEmailMessage.