B4J Tutorial [ABMaterial] Creating a Contact Us form that will send an email

Mashiane

Expert
Licensed User
Hi again..

I wanted to create a Contact Us form that will send an email to a specified recipient using stored SMTP details. After designing my form, I had something like this to go with.

Related Article: Email Account Settings (How you can set up your app to send emails using a database stored smtp settings)

ContactUsFormDesign.png


To be able for your app to send emails, you can use SMTP as explained in the link above. Based on that article, what this then does when the comment button is clicked is to read the form contents and send them to the specified administrator email addres..

B4X:
Private Sub ContactUsProcess()
    'define a map to hold the form contents
    Dim m As Map
    'read the file contents to a map
    m = GetContents
    'validate the form contents where required
    If Validate(m) = False Then
        Return
    End If
    Dim fullname As String = m.get("fullname")
    Dim email As String = m.get("email")
    Dim mobilephone As String = m.get("mobilephone")
    Dim comment As String = m.get("comment")
    Try
        'the form contents are ok, continue with the email send
        ' start smtp to send the emails
        'Get connection from current pool if MySQL/MSSQL
        Dim jSQL As SQL = ABMShared.SQLGet
        'get first available account, there should be 1
        Dim id As String = ABMShared.SQLSelectSingleResult(jSQL, "select id from EmailAccounts Limit 1", Null)
        'read the email settings first
        Dim es As Map
        es = ABMShared.SQLRecordRead(jSQL,"EmailAccounts", "id", id)
        Dim admin As String = es.GetDefault("administrator","")
        Dim sbody As String = "FullName: " & fullname & "||Email: " & email & "||MobilePhone: " & mobilephone & "||Comment: " & comment & "||"
        sbody = ABMShared.Replace(sbody,"|",CRLF)
        SendEmail(jSQL,smtp,admin, "DRPW: Contact Us", sbody)
        'Close the connection to the database
        ABMShared.SQLClose(jSQL)
        Return True
    Catch
        myToastId = myToastId + 1
        page.ShowToast("toast" & myToastId, "toastred", "Email could not be sent, please try again.", 3000)
    End Try
End Sub
I have attached a file here for reference..

NB: The link in this attachment is important to establishing the context of sending the email and the related code.
 

Attachments

Top