Sending an Email with Visualforce | Techila
1121
single,single-post,postid-1121,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Sending an Email with Visualforce

Sending an Email with Visualforce

SENDING AN EMAIL WITH VISUALFORCE

Visualforce can be used to send email to any of your contacts, leads, or other recipients. It is also possible to create reusable email templates that take advantage of Visualforce’s ability to iterate over your Salesforce records. It is possible to send email using Visualforce by creating a custom controller to deliver the message. The Apex Messaging.SingleEmailMessage class handles the outbound email functionality available to Salesforce.

At minimum, a custom controller that uses the Apex Messaging namespace needs a subject, a body, and a recipient for the email. You will need a page that acts as a form to fill out the subject and body and deliver the email.

Create a new page called sendEmailPage and use the following code:

 

<apex:page controller=”sendEmail”>

 

    <apex:messages />

 

    <apex:pageBlock title=”Send an Email to Your    {!account.name} Representatives”>

 

        <p>Fill out the fields below to test how you might send an email to a user.</p>

 

        <br />

 

        <apex:dataTable value=”{!account.Contacts}” var=”contact” border=”1″>

            <apex:column >

 

                <apex:facet name=”header”>Name</apex:facet>

 

                {!contact.Name}

 

            </apex:column>

 

            <apex:column >

 

                <apex:facet name=”header”>Email</apex:facet>

 

                {!contact.Email}

 

            </apex:column>

 

        </apex:dataTable>

 

        <apex:form >

 

        <br /><br />

 

            <apex:outputLabel value=”Subject” for=”Subject”/>:<br />   

 

            <apex:inputText value=”{!subject}” id=”Subject” maxlength=”80″/>

 

            <br /><br />

 

            <apex:outputLabel value=”Body” for=”Body”/>:<br />   

 

            <apex:inputTextarea value=”{!body}” id=”Body” rows=”10″ cols=”80″/>         

 

            <br /><br /><br />

            <apex:commandButton value=”Send Email” action=”{!send}” />

 

        </apex:form>

 

    </apex:pageBlock>

 

</apex:page>

 

Notice in the page markup that the account ID is retrieved from the URL of the page. For this example to render properly, you must associate the Visualforce page with a valid account record in the URL. For example, if 001D000000IRt53 is the account ID, the resulting URL should be:

1

https://Salesforce_instance/apex/sendEmailPage?id=001D000000IRt53

 

The following code creates a controller named sendEmail that implements the Messaging.SingleEmailMessage class, and uses the contacts related to an account as recipients:

 Apex controller:

 public class sendEmail {

     public String subject { get; set; }

     public String body { 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

         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

   String addresses;

     if (account.Contacts[0].Email != null)

     {

         addresses = account.Contacts[0].Email;

         // Loop through the whole list of contacts and their emails

         for (Integer i = 1; i < account.Contacts.size(); i++)

         {

             if (account.Contacts[i].Email != null)

             {

                 addresses += ‘:’ + account.Contacts[i].Email;

             }

         }

     }

  String[] toAddresses = addresses.split(‘:’, 0);

   // Sets the paramaters of the email

         email.setSubject( subject );

         email.setToAddresses( toAddresses );

         email.setPlainTextBody( body );

     // Sends the email

         Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 

return null;

  }

}

 

The Visualforce preview will be:

Sending An Email With Visual force preview

That’s all.

Courtesyhttps://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_custom_controller.htm

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment