|
|
|
|
- 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 - WINDEV application: Managing data
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.
![Durée de la leçon Durée de la leçon](https://doc.windev.com/en-US/images/image.awp?langid=3&name=dur%E9e.png&0) 20 mn 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.
- In the "Based on a template" tab pane, select "Use: WINTPL_Template" and finish the wizard.
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 in the upper-left area of the window: the Edit control is automatically created.
- Select the control and press Enter. The text becomes editable. Enter "SMTP server" and validate. The control name automatically changes to EDT_SMTP_Server.
| It's 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 a preset "Password" edit control.
- The control shape appears under the cursor.
- Click below the "User" control: the Edit control is automatically created.
![](https://doc.windev.com/ext/images/us/note_v2.png?478445243) 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: this allows users to check their passwords.
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: the Edit control is automatically created.
- Select the control and press Enter. The text becomes editable. Enter "Sender" and validate.
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 starts 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: the Edit control is automatically created.
- Select the control and press Enter. The text becomes editable. Enter "Subject" and validate.
4. Message body: For the message body, we will use an HTML Email Editor control: the user will be able 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 is 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".
![](https://doc.windev.com/ext/images/us/note_v2.png?478445243) 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. - Validate.
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 drag it to the "WIN_Sending_an_email" window: drop the control 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: 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: - Edit the WLanguage code associated with the Button control: right-click and select "Code" in the context menu.
- 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 ( ![Save element Save element](https://doc.windev.com/en-US/images/image.awp?langid=3&name=ICO_Enregistrer_GAF.jpg&-650246955) 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: - On the "Creation" tab, in the "Usual controls" group, expand "Button": the list of preset buttons appears.
- 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: anchor these controls horizontally.
- Select the "HTML Editor" control: anchor this control vertically and horizontally.
- Select the Button controls: anchor these controls to the right and to the bottom.
- Select the file selection control: anchor this control horizontally and to the bottom.
Now let's see if the tab order of the controls in the page is consistent. ![](https://doc.windev.com/ext/images/us/note_v2.png?478445243) 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.
- Define the automatic tab order: on the "Window" tab, in the "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 to open the context menu of the "Exit" option and select "Add before".
- Type the option caption ("Send an email") and validate.
- Select the "Exit" option again.
- Right-click to open the context menu of the "Exit" option and select "Insert a separator".
Let's look at the code for this option: - Right-click to open the context menu of the "Send an email" option and select "Code".
- Write the following WLanguage code:
WIN_Sending_an_email.OpenChild()
In this WLanguage code, <Window>.OpenChild is used to open the window in "non-modal" mode: the user will be able to write an email and to see the information displayed in the main window at the same time.
Save the window and its code ( ![Save an element Save an element](https://doc.windev.com/en-US/images/image.awp?langid=3&name=ICO_Enregistrer_GAF.jpg&-650246955) or Ctrl + S). Test the project (click ![Test project Test project](https://doc.windev.com/en-US/images/image.awp?langid=3&name=ICO_GO_Projet_WD_GAF.jpg&618137699) in the quick access buttons) and open the window for sending emails via "Menu .. Send an email".
![](https://doc.windev.com/ext/images/us/note_v2.png?478445243) 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 - WINDEV application: Manage data", double-click "Full application - Answers". 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|