|
|
|
|
|
- Lesson 7 - Sending an email
- Overview
- New window to send emails
- Creating the window
- Creating the controls to send emails
- Creating the controls to enter the email characteristics
- WLanguage code for sending the email
- Window improvements
- Closing the window
- Formatting
- Opening the window from the drop-down menu
- To sum up
Tutorial - Managing data in a WINDEV application
Lesson 7 - Sending an email We will cover the following topics: - How to send an email from a WINDEV application.
- How to include a supercontrol in a window.
- How to define the tab order in a window.
- How to open a non-modal window.
 20 min Multiple WLanguage functions allow you to manage incoming and outgoing emails. You also have the ability to access the email characteristics: - sender, recipients,
- outgoing date, subject, message,
- attachments, etc.
In this lesson, we are going to create a window allowing the user to send an email from the "WD Full Application" application. This window is as follows:
We will use SMTP to send the email. First, we will create the window with the different controls for entering the characteristics of the email. Then, we will write the code to send the email.
Warning
This lesson is based on the example developed in lesson 1.
New window to send emails This window will contain all controls allowing the user to type the different email elements. A "Send" Button control will group all processes used to send the email. Creating the window Create a new window: - Click
in the quick access buttons. - Click "Window" then "Window". The window creation wizard opens.
- Click "Based on a template", select "Use: WINTPL_Template" and confirm.
The save window appears. Enter "Sending an email" in the title field, the name is automatically completed: "WIN_Sending_an_email". Validate the default information. Creating the controls to send emails First, we will create all controls for configuring the SMTP server used to send messages. Four edit controls are required: - SMTP server,
- SMTP server port,
- Username,
- User password.
To create the Edit control that corresponds to the name of the SMTP server: - On the "Creation" tab, in the "Usual controls" group, click
. - The control shape appears under the cursor.
- Click the upper-left corner of the window to create Edit control.
- Select the control and press Enter. The text switches to edit mode. Enter "SMTP server" and validate. The control name automatically changes to EDT_SMTP_Server.
| Your turn: Similarly, create two Edit controls: - An EDT_Port control to enter the port of the SMTP server. Enter "Port" for the caption of this control.
- An EDT_User control to enter the username.
|
We will create a new Edit control to allow users to enter their password. To do so, we will use one of the preset controls available in WINDEV: - On the "Creation" tab, in the "Usual controls" group, expand "Edit".
- Select "Password edit".
- The control shape appears under the cursor.
- Click below the "User" control to create the new control.
 The "Password" edit control appears in the editor with a bullet and an eye. At runtime, when users enter their passwords, the characters will be replaced with bullets. Users will be able to click the eye icon to toggle the visibility of the password.
Align the controls you have just created: - Select the four controls.
- On the "Alignment" tab, in the "Inside and outside" group, click "Justify (Ins. and Out.)".
- Save the window (
or Ctrl + S).
Creating the controls to enter the email characteristics To write an email, the user needs: - A control to enter the sender's address.
- A control to enter or select the recipient address.
- A control to enter the email subject.
- A control to enter the email text. The user must have the ability to format the email text.
- A control to add attachments.
We will create these controls in our window. 1. Sender's address: To create the Edit control for the sender's address, we will use a preset control: - On the "Creation" tab, in the "Usual controls" group, expand "Edit".
- Enter "Email" in the search box at the top of the list of preset controls. Select "eMail edit". The control shape appears under the cursor.
- Click below the "Password" control to create the new control.
- Select the control and press Enter. The text switches to edit mode. Enter "Sender" and confirm.
2. Recipient's address: The control used to type the recipient's address should list the addresses of the customers from the database, but should also allow the user to type another address. To do so, we will use an "Editable combo box" control linked to the Customer data file. - On the "Creation" tab, in the "Usual controls" group, click "Combo Box". The control shape appears under the cursor.
- Click below the "Sender" control. The Combo Box control creation wizard opens automatically.
- Select "Display data from a file or existing query". Go to the next step.
- Select the Customer data file. Go to the next step.
- The email addresses of the customers will be displayed in the control:
- Uncheck the "CustomerID" item.
- Check the "Email" item.
Go to the next step. - The sort item is the "Email" item. Go to the next step.
- The return value is the "Email" item. Go to the next step.
- Keep the default options. Go to the next step.
- In the "Additional settings" screen, check "Allow input". The user must have the possibility to enter a new email address. Go to the next step.
- Change the name and caption of the Combo Box control:
- Enter "COMBO_Recipient" in the name field.
- Enter "Recipient" in the caption field.
- Finish the wizard. The control is automatically created in the window.
3. Email subject: To create the Edit control that corresponds to the email subject: - On the "Creation" tab, in the "Usual controls" group, click
. The control shape appears under the cursor. - Click below the "Recipient" control to create the new control.
- Select the control and press Enter. The text switches to edit mode. Enter "Subject" and validate.
4. Message body: For the message body, we will use an HTML Email Editor control. This control will allow the user to easily format the email text via a specific ribbon. - On the "Creation" tab, in the "Other controls" group, expand "HTML" and select "HTML Email Editor".
- The control shape appears under the cursor.
- Click below the "Subject" control. The HTML Editor control will be automatically created.
- Enlarge the window using the sizing handles, then enlarge the control vertically and horizontally so that the ribbon of the control and some text lines are visible.
Let's change the control characteristics via its description window: - Select the HTML Editor control and open the control description window (Alt + Enter).
- Change the control name. Name the control "HTMEDT_Email".
- Select "Mini toolbar".
 The HTML Editor control lets you choose between two types of toolbars: a standard ribbon or a mini toolbar, which is more convenient for writing emails. - Confirm.
5. Attachments: We will create a file picker to allow users to add attachments. Instead of creating it from scratch, we will use a preset Edit control for files. The user will be able to select the file to attach by clicking "Browse" in the context menu of the control. - On the "Creation" tab, in the "Usual controls" group, expand "Edit". The list of preset controls is displayed.
- Select the "File path edit" control and place it below the "HTML Editor" control. The file picker is immediately created.
Align all controls in the window.
We will now create the Button control to send emails. WLanguage code for sending the email The email will be sent by clicking a button. We will create the button in the window and write the corresponding code in WLanguage. First of all, enlarge the window to have enough space to add a button below the HTML Editor control. To do so, use the bottom-right resizing handle of the window. To create the Button control to send emails: - On the "Creation" tab, in the "Usual controls" group, click
. - Click the location where you want to create the control (e.g., at the bottom of the window).
- Select the control and change its text (press Enter for example). Enter "Send".
Let's move on to the WLanguage code for this button: - Right-click the Button control to open the context menu, and select "Code".
- Write the following lines of code in the "Click" event:
- WLanguage code for opening the connection and starting the SMTP session:
MySession is emailSMTPSession
MySession.Name = EDT_User
MySession.Password = EDT_Password
MySession.ServerAddress = EDT_SMTP_Server
MySession.Port = EDT_Port
IF NOT MySession.StartSession() THEN
Error("Unable to connect to the SMTP server.", ErrorInfo())
RETURN
END
This code uses an advanced emailSMTPSession variable. The different properties of this variable define the characteristics of the SMTP session. Then, the <Session>.StartSession function is used to start the session. - WLanguage code for preparing the email:
MyMessage is Email
MyMessage.Sender = EDT_Sender
MyMessage.Subject = EDT_Subject
HTMEDT_Email.ToEmail(MyMessage)
MyMessage.Recipient.Add(COMBO_Recipient.DisplayedValue)
IF EDT_File <> "" THEN
MyMessage.LoadAttachment(EDT_File)
END
This code uses a variable of type Email. The different properties of this variable define the characteristics of the email to be sent. This code associates the content of the different window controls to the properties of the Email variable. In this code: - <HTML Editor control>.ToEmail directly associates the contents of the email with the corresponding properties of the Email variable. If images are included in the text of the email, they will be automatically added as attachments.
- <Email>.LoadAttachment integrates the attached file if it exists in the Email variable.
- WLanguage code for sending the email:
IF MySession.SendMessage(MyMessage) = False THEN
Error("Message not sent.", ErrorInfo())
ELSE
ToastDisplay("Message sent", toastShort, vaMiddle, haCenter)
END
The email is sent by <Email>.SendMessage. Simply pass as parameters: the variable that contains the characteristics of the SMTP session, and the variable that contains the characteristics of the email to send. If the email is sent, a Toast message is displayed, indicating that the email was sent. A Toast message is a small message that appears for a few seconds. - WLanguage code for closing the SMTP session:
This code closes the session with <Session>.CloseSession.
Save the window and its code (  or Ctrl + S). We are going to improve our window: - Add a Button control to close the window.
- Format the window by setting the anchors and the tab order.
- Open the window from the "WIN_Menu" window.
Closing the window To add a Button control to close the window: - Go to the "Creation" tab, "Usual controls" group, and expand "Button" to see the list of preset buttons.
- Click the "Close" button.
- Click the position in the window where the control must be created (for example at the bottom, to the right of the "Send" Button control).
Formatting To define the anchors: - Select the "Sender", "Recipient" and "Subject" controls and set their anchor mode to "Width".
- Select the "HTML Editor" control and set its anchor mode to "Width and height".
- Select the Button controls and set their anchor mode to "Right and bottom".
- Select the file picker and set its anchor mode to "Width and bottom".
Now let's see if the tab order of the controls in the page is consistent.  The tab order of the controls is the order in which the user will be able to enter the values in the different controls of the window. At runtime, the Tab key allows switching from one control to another.
The default tab order is the order in which the controls have been created. To define the tab order: - Display the tab order by pressing F5.
- Set the tab order to automatic. To do so, go to the "Window" tab, "Order" group, expand "Tab order" and select "Define automatically".
- The numbers change and appear in order.
- Press F5 again to hide the numbers.
- Save the window (
or Ctrl + S).
Opening the window from the drop-down menu The window will be opened directly from a drop-down menu option in "WIN_Menu". First, let's create the menu option: - Open "WIN_Menu" in the editor (double-click its name in the "Project explorer" pane, for example).
- In the editor, expand the "Menu" option and select "Exit".
- Right-click the "Exit" option to open the context menu and select "Add option before".
- Type the option caption ("Send an email") and confirm.
- Select the "Exit" option again.
- Right-click the "Exit" option to open the context menu and select "Insert a separator".
Let's look at the code for this option: - Right-click the "Send an email" option to open the context menu and select "Code".
- Write the following WLanguage code:
WIN_Sending_an_email.OpenChild()
In this WLanguage code, <Window>.OpenChild opens the window in "non-modal" mode. This means the user can both write an email and see the information in the main window.
Save the window and its code (  or Ctrl + S). Test the project (click  in the quick access buttons) and open the window for sending emails via "Menu .. Send an email".
 Completed project Do you want to check the end result of the steps described here? A completed project is available. This project contains the different windows created in this lesson. To open the completed project, go to the home page and click "Tutorial", then in "Tutorial - Full application with data", click "Open completed project". In this lesson, we discovered how to send an email from a WINDEV application. WINDEV applications can also allow users to read incoming emails. For more details, see Reading an email. This tutorial, which focuses on developing a management application, is now complete. We recommend you follow the Deploying the application tutorial to learn how to create and distribute the executable file of your application.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|