Developer forum

Forum » Development » Dynamic Recipient e-mail for "Sending" the form data

Dynamic Recipient e-mail for "Sending" the form data

Maulik Kataria
Reply
Hi,

I am using DW7 forms module and we wish to send the form data to a dynamic email address (based on user choice). In the old form module it was possible to use javascript to change a hidden input field FM_ToEmail to do so. How so I do this in forms-dw7 module?

edit:
This is for option "Send the form via e-mail" and not for the "Send a receipt via e-mail" option.

Thanks.


Replies

 
Nicolai Høeg Pedersen
Reply
You can use "Send the form via e-mail" or "Send a receipt via e-mail" options on the paragraph. Choose from which field the e-mail address should be found.

http://manual.dynamicweb-cms.com/Dynamicweb-On-line-Manual/Modules/Data-Lists/Forms/Paragraph-settings.aspx

 
Maulik Kataria
Reply
Hi Nicolai. Thank you for you reply.
Under "Send the form via e-mail" option, I need "Sender e-mail" and "Recipient e-mail" both to be dynamic. The problem is that for "Send form" option recipient is fixed and for "Send receipt" option sender is fixed.
 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer
You can create you own provider that do that. Download the source code from that provider so you can change it to fit your need. You can find it here: http://developer.dynamicweb-cms.com/downloads/source-code.aspx. You need the Forms7SaveProviders.zip file.

Votes for this answer: 0
 
Maulik Kataria
Reply
I was wishing that I could avoid doing custom development for this. But I guess it will be better that way instead of a workaround. I will work on a custom module then.

Thank you very much for your help :)

 
Maulik Kataria
Reply
One more thing, could you please point me to the right documentation on how to deploy this custom provider?

 
Pavel Volgarev
Reply
Hi  Maulik,

Deployment phase is very simple - just copy the assembly (.dll file) to the \bin folder of your Dynamicweb installation. The system will pick it up when it starts.

-- Pavel
 
Maulik Kataria
Reply
Great! So I just need to make changes to MailFormSaveProvider.vb and compile it into dll.

Thanks for the help guys.

 
Morten Fink Eriksen
Reply

I'm having trouble deploying my mailformsave provider, i've made changes to the mailformsaveprovider and deployed it in a "custommodules" project, but the changes i have made don't appear in the paragraph module setup screen for my forms module.

 

What i did was add a new field for the recipient checkbox, so you can chose recipient as a field value.

 

Am i missing something here? do i need to change an edit form template somewhere for the field to be available?

 
Morten Fink Eriksen
Reply

Hey Pavel

 

Having some trouble deploying my mailformsaveprovider, ive added a new "get from form" field for the recipient email field. Either im deploying it wrong, because it doesn't show up on the paragraph forms module setup screen or i am missing some change somewhere other than the mailformsaveprovider module. Ive deployed the changes as a "custom module" project to the Bin folder of the application.

 

Do you have any idea what the problem could be?

 
Pavel Volgarev
Reply

Hi Morten,

 

Does the field you created have a "public" access modifier? Can we see some relevant code?

 

-- Pavel

 
Morten Fink Eriksen
Reply

This is the code for my field:

Private _MailToUseField As Boolean = False

 

....

< _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseFromFieldForRecipientEmail"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.RecipientEmail', this)") _
        > _
        Public Property MailToUseField() As Boolean
            Get
                Return _MailToUseField
            End Get
            Set(ByVal value As Boolean)
                _MailToUseField = value
            End Set
        End Property


 MailToEmail = MailTo
                If MailToUseField Then
                    For Each key As String In KeysAndValues.Keys
                        If key = MailTo Then
                            MailToEmail = Base.ChkString(KeysAndValues(key))
                        End If
                    Next
                End If
 
Pavel Volgarev
Reply

The code looks fine. Does the app recycle when you update the DLL?

 

-- Pavel

 
Morten Fink Eriksen
Reply

Tried recycling manually too.. didn't change anything..

 
Pavel Volgarev
Reply

My point is that if the app doesn't recycle when you update the DLL then you place it into the wrong /bin folder. Could you check this (in IIS make sure the /bin folder under the website matches the physical location where you copy your DLL)?

 

-- Pavel

 
Morten Fink Eriksen
Reply

Sorry.. it does recycle yes... but tried it manually too :)

 
Pavel Volgarev
Reply

Which version of Dynamicweb are you using?

 

-- Pavel

 
Morten Fink Eriksen
Reply

8.1.1.7 is the version.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Morten,

 

Does your class inherit from the correct base class? Can you post the full code?

Imar

 
Morten Fink Eriksen
Reply

 

 

Namespace Modules.DataManagement

    < _
    AddInName("DM.Form.SendMail"), _
    AddInLabel("Send the form over mail"), _
    AddInDescription("This will send an email with all form elements.") _
    > _
    Public Class MailFormSaveProvider
        Inherits FormSaveProvider
        Implements IDropDownOptions

        Private _MailTo As String = ""
        Private _MailToUseField As Boolean = False
        Private _MailFrom As String = ""
        Private _MailFromName As String = ""
        Private _MailFromUseField As Boolean = False
        Private _MailFromNameUseField As Boolean = False
        Private _MailCC As String = ""
        Private _MailBCC As String = ""
        Private _MailSubject As String = ""
        Private _MailTemplate As String = ""
        Private _MailEncoding As Text.Encoding = Text.Encoding.UTF8
        Private _MailLogging As Boolean = True

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.SenderName"), _
        AddInLabel("Sender name"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailFromName() As String
            Get
                Return _MailFromName
            End Get
            Set(ByVal value As String)
                _MailFromName = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseFormFieldForSenderName"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.SenderName', this)") _
        > _
        Public Property MailFromNameUseField() As Boolean
            Get
                Return _MailFromNameUseField
            End Get
            Set(ByVal value As Boolean)
                _MailFromNameUseField = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.SenderEmail"), _
        AddInLabel("Sender e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailFrom() As String
            Get
                Return _MailFrom
            End Get
            Set(ByVal value As String)
                _MailFrom = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseFormFieldForSenderEmail"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.SenderEmail', this)") _
        > _
        Public Property MailFromUseField() As Boolean
            Get
                Return _MailFromUseField
            End Get
            Set(ByVal value As Boolean)
                _MailFromUseField = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.RecipientEmail"), _
        AddInLabel("Recipient e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailTo() As String
            Get
                Return _MailTo
            End Get
            Set(ByVal value As String)
                _MailTo = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseFromFieldForRecipientEmail"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MailFormSaveProvider.RecipientEmail', this)") _
        > _
        Public Property MailToUseField() As Boolean
            Get
                Return _MailToUseField
            End Get
            Set(ByVal value As Boolean)
                _MailToUseField = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.RecipientCCEmail"), _
        AddInLabel("Recipient CC e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailCC() As String
            Get
                Return _MailCC
            End Get
            Set(ByVal value As String)
                _MailCC = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.RecipientBCCEmail"), _
        AddInLabel("Recipient BCC e-mail"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailBCC() As String
            Get
                Return _MailBCC
            End Get
            Set(ByVal value As String)
                _MailBCC = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.Subject"), _
        AddInLabel("Subject"), _
        AddInParameterEditor(GetType(Editors.TextParameterEditor), "NewGUI=true") _
        > _
        Public Property MailSubject() As String
            Get
                Return _MailSubject
            End Get
            Set(ByVal value As String)
                _MailSubject = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.Template"), _
        AddInLabel("Template"), _
        AddInParameterEditor(GetType(Editors.TemplateParameterEditor), "folder=/Templates/DataManagement/Forms/Email;FullPath=false;NewGUI=true") _
        > _
        Public Property MailTemplate() As String
            Get
                Return _MailTemplate
            End Get
            Set(ByVal value As String)
                _MailTemplate = value
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.Encoding"), _
        AddInLabel("Encoding"), _
        AddInParameterEditor(GetType(Editors.DropDownParameterEditor), "NewGUI=true;SortBy=Value") _
        > _
        Public Property MailEncodingCodePage() As Integer
            Get
                Return _MailEncoding.CodePage
            End Get
            Set(ByVal value As Integer)
                Dim enc As Text.Encoding = Text.Encoding.GetEncoding(value)
                _MailEncoding = enc
            End Set
        End Property

        < _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MailFormSaveProvider.UseLogging"), _
        AddInLabel("Logging"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "") _
        > _
        Public Property MailLogging() As Boolean
            Get
                Return _MailLogging
            End Get
            Set(ByVal value As Boolean)
                _MailLogging = value
            End Set
        End Property

        Public Function GetOptions(ByVal name As String) As Hashtable Implements IDropDownOptions.GetOptions
            Dim ht As New Hashtable()
            Dim encArray() As Text.EncodingInfo = Text.Encoding.GetEncodings
            'Array.Sort(encArray, AddressOf CompareDisplayName)

            For Each enc As Text.EncodingInfo In encArray
                If Not ht.ContainsKey(enc.CodePage) Then
                    ht.Add(enc.CodePage, enc.DisplayName)
                End If
            Next

            Return ht
        End Function

        Public Overrides Function Save(ByVal Form As FormSetting, ByVal KeysAndValues As Dictionary(Of String, Object)) As Boolean
            Dim sb As New Text.StringBuilder
            Dim isSaved As Boolean = False

            Dim hasError As Boolean = False

            Dim SendMail As Boolean = False
            Dim DWSmtpServer As String = ""

            Dim MailFromEmail As String = ""
            Dim MailFromNameValue As String = ""

            Dim MailTemplateContext As String = ""

            Try
                '############################
                'Building template context
                '############################
                Dim template As New Templatev2.Template("DataManagement/Forms/Email/" & MailTemplate)

                Dim fields As FormFieldCollection = Form.Fields

                Dim fieldsTmp As New FormFieldCollection
                fieldsTmp.AddRange(fields)

                For Each field As FormField In fieldsTmp
                    If KeysAndValues.ContainsKey(field.SystemName) Then
                        field.Value = Base.ChkString(KeysAndValues(field.SystemName))
                    Else
                        Form.Fields.Remove(field)
                    End If
                Next
                Dim render As New FormRenderer()

                MailTemplateContext = render.RenderForm(Form, template)

                '############################
                'Email settings
                '############################
                DWSmtpServer = Base.ChkString(Base.GetGs("/Globalsettings/System/MailServer/Server"))

                'Set values for MailFromNameValue and MailFromEmail. They may be overridden later.
                MailFromNameValue = MailFromName
                MailFromEmail = IIf(String.IsNullOrEmpty(MailFrom), Base.ChkString(Base.GetGs("/Globalsettings/Settings/CommonInformation/Email")), MailFrom).ToString
                If MailFromUseField OrElse MailFromNameUseField Then
                    For Each key As String In KeysAndValues.Keys
                        If key = MailFrom Then
                            MailFromEmail = Base.ChkString(KeysAndValues(key))
                        End If
                        If MailFromNameUseField AndAlso key = MailFromName Then
                            MailFromNameValue = Base.ChkString(KeysAndValues(key))
                        End If
                    Next
                End If
                If String.IsNullOrEmpty(MailFrom) Then MailFrom = "noreply@dynamicweb.dk"


                Dim objEmail As New System.Net.Mail.MailMessage

                If _MailEncoding IsNot Nothing Then
                    objEmail.BodyEncoding = _MailEncoding
                Else
                    objEmail.BodyEncoding = Text.Encoding.UTF8
                End If

                MailToEmail = MailTo
                If MailToUseField Then
                    For Each key As String In KeysAndValues.Keys
                        If key = MailTo Then
                            MailToEmail = Base.ChkString(KeysAndValues(key))
                        End If
                    Next
                End If

                If Not Base.ValidateEmail(MailToEmail) Then
                    SetInvalidEmailMessage(MailToEmail)
                End If
                objEmail.To.Add(MailToEmail)

                If Not String.IsNullOrEmpty(MailCC) Then
                    If MailCC.Contains(";") Then
                        Dim MailCCArray() As String = MailCC.Split(";"c)

                        For Each address As String In MailCCArray
                            Dim addr As String = address.Trim()
                            If Not Base.ValidateEmail(addr) Then
                                SetInvalidEmailMessage(addr)
                            End If
                            objEmail.CC.Add(addr)
                        Next
                    Else
                        If Not Base.ValidateEmail(MailCC) Then
                            SetInvalidEmailMessage(MailCC)
                        End If
                        objEmail.CC.Add(MailCC)
                    End If
                End If

                If Not String.IsNullOrEmpty(MailBCC) Then
                    If MailBCC.Contains(";") Then
                        Dim MailBCCArray() As String = MailBCC.Split(";"c)

                        For Each address As String In MailBCCArray
                            Dim addr As String = address.Trim()
                            If Not Base.ValidateEmail(addr) Then
                                SetInvalidEmailMessage(addr)
                            End If
                            objEmail.Bcc.Add(addr)
                        Next
                    Else
                        If Not Base.ValidateEmail(MailBCC) Then
                            SetInvalidEmailMessage(MailBCC)
                        End If
                        objEmail.Bcc.Add(MailBCC)
                    End If
                End If

                If Not String.IsNullOrEmpty(MailFromNameValue) Then
                    objEmail.Sender = New Net.Mail.MailAddress(MailFromEmail, MailFromNameValue)
                Else
                    objEmail.Sender = New Net.Mail.MailAddress(MailFromEmail)
                End If
                objEmail.From = objEmail.Sender
                objEmail.Subject = MailSubject

                objEmail.IsBodyHtml = True
                objEmail.Body = MailTemplateContext

                isSaved = EmailHandler.Send(objEmail, MailLogging)
                objEmail.Dispose()

            Catch ex As DataException
                isSaved = False
            End Try

            Return isSaved
        End Function

        Private Sub SetInvalidEmailMessage(ByVal mailAddress As String)
            HttpContext.Current.Response.Write(String.Format("<script>alert('Email address: \'{0}\' is invalid.');history.back(-1);</script>", mailAddress))
            HttpContext.Current.Response.End()
        End Sub

        Private Function CompareDisplayName(ByVal first As Text.EncodingInfo, ByVal second As Text.EncodingInfo) As Integer
            Return String.Compare(first.DisplayName, second.DisplayName, True)
        End Function
    End Class

End Namespace

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Morten

 

I think the issue is related to the fact that you've copied the entire code from our implementation of that provider. So unless you've built your own Dynamicweb based on the entire code, you now have two providers with the same AddInName and type signature. You should call your custom implementation something different from the standard provider as they can't--and shouldn't--exist side-by-side.

 

Hope this helps :)

 

- Jeppe

 
Morten Fink Eriksen
Reply

Yeah you are right.. but i have now tried renaming the class, addin name etc. and still no go. It doesn't show up as a provider which i can choose in the forms module setup screen (im guessing that is should happen as there is no documentation for this provider).

< _
    AddInName("MyExtendedFormSaveProvider"), _
    AddInLabel("Send the form over mail"), _
    AddInDescription("This will send an email with all form elements.") _
    > _
    Public Class MyExtendedFormSaveProvider
        Inherits FormSaveProvider
        Implements IDropDownOptions

        Private _MailTo As String = ""
        Private _MailToUseField As Boolean = False
        Private _MailFrom As String = ""
        Private _MailFromName As String = ""
        Private _MailFromUseField As Boolean = False
        Private _MailFromNameUseField As Boolean = False
        Private _MailCC As String = ""
        Private _MailBCC As String = ""
        Private _MailSubject As String = ""
        Private _MailTemplate As String = ""
        Private _MailEncoding As Text.Encoding = Text.Encoding.UTF8
        Private _MailLogging As Boolean = True

And the properties

< _
        AddInParameterGroup("Mail configuration"), _
        AddInParameter("MyExtendedFormSaveProvider.UseFromFieldForRecipientEmail"), _
        AddInLabel("Get from form"), _
        AddInParameterEditor(GetType(Editors.YesNoParameterEditor), "jsOnClick=ShowFieldSelector('MyExtendedFormSaveProvider.RecipientEmail', this)") _
        > _
        Public Property MailToUseField() As Boolean
            Get
                Return _MailToUseField
            End Get
            Set(ByVal value As Boolean)
                _MailToUseField = value
            End Set
        End Property

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Are you sure your code is compiling correctly? I tried your code as-is and it didn't compile because MailToMail is undefined.... I also got errors on the TemplateV2 namespace....

 

Imar    

Votes for this answer: 1
 
Morten Fink Eriksen
Reply

Thats odd... how are you compiling it?

 
Morten Fink Eriksen
Reply

Imar..  you are right.. somehow i got this mixed into an C# project (doh!), hence no errors when i compiled. :)

All is working now.. and the provider is shown.

 

Thanks Imar and support :)

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Yes, that's exactly what should happen. Documentation here: http://developer.dynamicweb-cms.com/Admin/Public/DWSDownload.aspx?File=Files%2fFiler%2fDocumentation%2fDevelopment%2fModules%2f(en-US)+Data+Management+Extensibility+API.pdf

 

I did the same thing as Imar and got the same issue. I fixed it by instanciating the MailToEmail:

Dim MailToEmail As String = MailTo

 

That solved the issue for me and I could see the provider in the paragraph setup.

 

- Jeppe

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

I see you solved it on your own also.

I'm just too slow I guess ;)

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Against Dynamicweb 8.1.1.5 (didn't have 1.1.7 here), against .NET 4 and with Option Explicit turned on at the project level.

 

Maybe you're compiling against an older version of DW locally and your code crashes at the server because TemplateV2.Template is obsolete?

 

Imar

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> I'm just too slow I guess ;)


Same here..... ;-)

 
Morten Fink Eriksen
Reply

Nope... 8.1.1.7 is the version im compiling against and is the right one :)

 

Problem was, as i stated above, that i didn't see any errors because i had the code compiling in a C# project by mistake. And thinking everything was alright when it compiled and said no errors.

 

But once again thanks for leading me on the right track :)

 

You must be logged in to post in the forum