Sending mail via APEX

Sending mail is an important feature in a web application. You can use it when you want to confirm something. Or when you want to send information to the user, or the user wants to send your company an e-mail with an inquiry. In this recipe, we will make a section on the homepage where users can request information via e-mail.

Getting ready

Your database should be able to send outbound e-mails. Therefore, check your database if it is configured for sending e-mails.

How to do it...

  1. In the application builder, go to page 1 of your application.
  2. In the Regions section, click the Add icon to create a new region.
  3. Select HTML.
  4. Select HTML again. This time it's for the type of HTML region.
  5. Enter a title for this region. For example, Contact. Click Next.
  6. In the text area, enter the following text: If you have any questions, please mail us.
  7. Click the Create region button.
  8. In the items section, click the add icon to create a new item.
  9. Select text field. Click Next.
  10. Enter a name for this item. In our case, this would be P1_NAME.
  11. In the region list box, select the region you just created. Click Next.
  12. Enter a name for the label. Click Next two times.
  13. Click the Create item button.
  14. The first item is now ready. You should also create the following items using the steps above:

    P1_EMAIL

    P1_SUBJECT

    P1_TEXT.

  15. But for this last item, select text area instead of text field as the item type.
  16. Next create a button. In the buttons section, click the Add icon.
  17. Select the region for this button. In our case, we select the region we just created. Click Next.
  18. Select Displayed among this region's items as the position.
  19. Enter a name for this button, for example P1_SENDMAIL.
  20. Check the Beginning on new line checkbox.
  21. In the label field, enter Send mail for this button.
  22. In the button style listbox, select HTML button.
    How to do it...
  23. Click the Create button.

We now have a region with the necessary text items and a button to send the mail. Now we must create the process.

  1. In the processes section in the middle of the screen, click the Add icon.
  2. Select Send E-mail and click Next.
    How to do it...
  3. Enter a name for this process. Click Next.
  4. In the From field, enter&P1_EMAIL. (with the dot at the end).
  5. In the To field, enter your company's e-mail address.
  6. In the Subject field, enter&P1_SUBJECT. (with the dot at the end).
  7. In the Body Plain Text field, enter&P1_TEXT. (with the dot at the end).
  8. Further, you can choose to have APEX send the mail immediately by selecting Yes in the Send immediately list box at the bottom.
    How to do it...
  9. Click Next.
  10. Enter a success message and a failure message. Click Next.
  11. In the "when button pressed" list box, select the button you just created.
  12. Click the Create process button.

The contact region with items and a send button is now ready. Run the form and try to send an e-mail.

How to do it...

How it works...

The actual work is done by the apex_mail.send function and this function is called in the page process. This page process is of type Email and all you have to do is enter all details in the text fields. However, you can make it dynamic by using the substitution variables like&P1_NAME. Don't forget the dot at the end.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.216.75