Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Maarten Boer 61 posts 82 karma points
    May 23, 2011 @ 11:53
    Maarten Boer
    0

    Sending mail from a contactform

    Hi,

    Im not so familiar with contactforms in Umbraco.
    I've seen a few packages like Cultivcontact and doc2form but i'm not actually looking for a formbuilder but the code to send the mail or <form> items.

    The provider of my site uses the component: JMail.NET

    Here is an example of the code:

    Dim JMail As New Message
    Dim s As String = "emailaddress"
    Dim Address As Address = Address.Parse(s)
    Dim List As AddressList = New AddressList()
    List.Add(Address)
    'List.Add(New Address("emailaddress")) for more addresses
    JMail.To = List
    JMail.From = "emailaddress"
    JMail.Subject = "subject"
    JMail.BodyText = "Text"
    JMail.Charset = Encoding.GetEncoding("iso-8859-1")
    Smtp.Send(JMail, "smtp.mijnhostingpartner.nl", 25, "mijnhostingpartner.nl", SmtpAuthentication.Login, "username", "password")
    JMail = Nothing

    Before using umbraco i would simply create a SENDMAIL.ASP with this code and address to this page in a form submit.
    But what is the proper way in Umbraco.???

    Maarten

  • Steven Newstead 62 posts 103 karma points
    May 23, 2011 @ 14:12
    Steven Newstead
    0

    This sort of thing is a fairly standard way of sending emails in asp.net, is this what you mean?

    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
       
    {
           
    // Instantiate a new instance of MailMessage
           
    MailMessage mMailMessage = new MailMessage();

           
    // Set the sender address of the mail message
            mMailMessage
    .From = new MailAddress(from);
           
    // Set the recepient address of the mail message
       
                    mMailMessage
    .To.Add(new MailAddress(to));
           
           
    // Check if the bcc value is null or an empty string
           
    if ((bcc != null) && (bcc != string.Empty))
           
    {
               
    // Set the Bcc address of the mail message
                mMailMessage
    .Bcc.Add(new MailAddress(bcc));
           
    }

           
    // Check if the cc value is null or an empty value
           
    if ((cc != null) && (cc != string.Empty))
           
    {
               
    // Set the CC address of the mail message
                mMailMessage
    .CC.Add(new MailAddress(cc));
           
    }       // Set the subject of the mail message
            mMailMessage
    .Subject = subject;
           
    // Set the body of the mail message
            mMailMessage
    .Body = body;
           
    // Set the format of the mail message body as HTML
            mMailMessage
    .IsBodyHtml = true;
           
    // Set the priority of the mail message to normal
            mMailMessage
    .Priority = MailPriority.Normal;

           
    // Instantiate a new instance of SmtpClient
           
    SmtpClient mSmtpClient = new SmtpClient();
            mSmtpClient
    .EnableSsl = true;
           
    // Send the mail message
            mSmtpClient
    .Send(mMailMessage);
       
    }

     

  • Maarten Boer 61 posts 82 karma points
    May 23, 2011 @ 15:23
    Maarten Boer
    0

    Hi Steven,

    This part i get.....

    But i'v saved the vb script in 'sendmail.asp' and placed it in de root of the site.

    In my page contact.aspx i have placed the following form:

        <form action="/sendmail.asp" method="post"> 
    <div id="div_contact_fr">
    <div id="div_contact_titel">contactform</div>

    <div id="div_contact_item" style="top:57px">name</div>
    <div id="div_contact_invul" style="top:52px">
    <input name="kname" type="text" class="contact_data" value="" size="79" maxlength="70" />
    </div>

    <div id="div_contact_item" style="top:90px">company</div>
    <div id="div_contact_invul1" style="top:85px">
    <input name="kcompany" type="text" class="contact_data" value="" size="79" maxlength="70" />
    </div>

    <div id="div_contact_item" style="top:123px">e-mail</div>
    <div id="div_contact_invul1" style="top:118px">
    <input name="kemail" type="text" class="contact_data" value="" size="79" maxlength="70" />
    </div>
    <div id="div_contact_item" style="top:156px">phone</div>
    <div id="div_contact_invul1" style="top:151px">
    <input name="kphone" type="text" class="contact_data" value="" size="79" maxlength="70" />
    </div>

    <div id="div_contact_item" style="top:189px">question</div>
    <div id="div_contact_invul2" style="top:184px">
    <textarea name="kquestion" rows="4" class="contact_data"></textarea>
    </div>
    <div id="div_nextpage"><input name="Send" type="submit" class="but_send" id="Send" value=""></div>
    </div>
    </form>

    Now when i press the submit-button i get an error

    Server Error in '/' Application.

    Runtime Error

    Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

    Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

    <!-- Web.Config Configuration File -->
    
    <configuration>
        <system.web>
            <customErrors mode="Off"/>
        </system.web>
    </configuration>


    Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.

    <!-- Web.Config Configuration File -->
    
    <configuration>
        <system.web>
            <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
        </system.web>
    </configuration>
  • Steven Newstead 62 posts 103 karma points
    May 23, 2011 @ 15:52
    Steven Newstead
    0

    I think that Umbraco is getting confused as you are posting back to an asp file, it is trying to process the asp file as a normal umbraco page. Try opening the web.config and finding this line

    <add key="umbracoReservedUrls" value="~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx" />

    If you change this to:

    <add key="umbracoReservedUrls" value="~/config/splashes/booting.aspx,~/install/default.aspx,~/config/splashes/noNodes.aspx,sendmail.asp" />

    This will make umbraco ignore the page so it should hopefully then just work.

     

  • Maarten Boer 61 posts 82 karma points
    May 23, 2011 @ 17:23
    Maarten Boer
    0

    Hi Steven,

     

    i altered the web.config added as you suggested, but still getting an error although it is a different error.

    Now receiving the following error:

    500 - Internal server error.

    There is a problem with the resource you are looking for, and it cannot be displayed.

    Maarten

  • Jesper Hauge 298 posts 487 karma points c-trib
    May 23, 2011 @ 23:11
    Jesper Hauge
    1

    Hi Maarten,

    It's not possible to use classic asp pages in a standard Umbraco site, if you really need to do this you will need to change settings in IIS, and solve some session sharing issues that are not trivial to solve.

    If you'd like to solve your problem using only the Umbraco backend (No Visual Studio Solution, precompiled dll's and stuff like that as shown in this video) You could implement a simple user control with this code, and upload it to the /usercontrols folder in your site calling it sendmail.ascx

    <%@ Control Language="C#" ClassName="SendmailControl" %>
    <script runat="server">
        protected void SendMail(object sender, EventArgs e)
      {
       try
       {
        umbraco.library.SendMail(SenderEmail.Text, "[email protected]", SubjectText.Text, MessageText.Text, false);
        ResponseLiteral.Text = @"<p>Your Email has been sent</p>";
       }
       catch (Exception)
       {
        ResponseLiteral.Text = @"<p>There was a problem sending your mail</p>";
       }
       FormPanel.Visible = false;
       ResponsePanel.Visible = true;
      }
    </script>
    <form runat="server">
    <asp:Panel ID="FormPanel" runat="server">
     <label>Email:</label><br />
     <asp:TextBox ID="SenderEmail" runat="server" CssClass="inputBoxClass" /><br />
     <label>Subject:</label><br />
     <asp:TextBox ID="SubjectText" runat="server" CssClass="inputBoxClass" /><br />
     <label>Message:</label><br />
     <asp:TextBox ID="MessageText" runat="server" CssClass="textareaBoxClass" /><br />
     <asp:Button ID="SendButton" runat="server" Text="Send" OnClick="SendMail" />
    </asp:Panel>
     
    <asp:Panel ID="ResponsePanel" runat="server" Visible="false">
     <div class="responseDivClass">
      <asp:Literal ID="ResponseLiteral" runat="server" />
     </div>
    </asp:Panel>
    </form>

    After doing that you need to create a macro ponting to this usercontrol.
    Developers section -> Right Click "Macros" -> name "SendMail"
    Then select your usercontrol in the ".NET user controle" dropdown to the right and save your macro. Now you should be able to insert this macro in the templates (or in the Richtext editor if you checked "Use in editor" in the macro configuration)

    Be aware that the code above relies on the SMTP server settings in your web.config file:

     <system.net>
      <mailSettings>
       <smtp>
        <network host="127.0.0.1" userName="username" password="password" />
       </smtp>
      </mailSettings>
     </system.net>

    You need to set the host attribute value to an IP-addres or DNS server name of a SMTP server that will accept and forward Emails for your server. Your website or webserver hoster should be able to provide you with the necessary information there.

    Regards
    Jesper Hauge

  • Steven Newstead 62 posts 103 karma points
    May 24, 2011 @ 11:48
    Steven Newstead
    0

    Although it's not possible to use classic ASP as Jesper correctly states you can call the asp page from umbraco as long as you exclude it from the Umbraco processing (by making that change to umbracoReservedUrls).

    Your server 500 error is probably because there is a code issue with the ASP page, possibly you are not grabbing the post data from your form, try hardcoding some email values in the asp page to see if an email gets sent at all, if that works then ensure you are getting all the post information from the umbraco page.

    Saying all that Jesper is correct, if possible I would set up something within Umbraco like he suggests, this would be the simplest solution!

  • Maarten Boer 61 posts 82 karma points
    May 24, 2011 @ 12:07
    Maarten Boer
    0

    Thanks guy's for all the help and suggestions...

    Ofcourse i want to drop the 'old school' solutions and learn all about the Umbraco way.

    I've been analyzing some contact forms like Cultvic en Koiak and starting to understand how things work. I got the both versions working now. Problem is that you need Visual Studio to make adjustments to the aspx.cv part which is responsible for the actual sending i believe. This information is compiled in the DLL's that come with these forms. Correct me if i'm wrong.

    So i'm gonna try Jesper's suggestion bij writing a new aspx file with any limitation....

    When i got things working i will post my solution for you.

    Maarten

  • Maarten Boer 61 posts 82 karma points
    May 25, 2011 @ 11:06
    Maarten Boer
    0

    Hi Jesper,

    Your script is working perfect..

    So i promised to keep you posted once i altered the script.

    My new script is not working. I'm not receiving any mails. But also receive no errors. Here is the code:

    <%@ Control Language="C#" ClassName="SendmailControl" %>
    <script runat="server">
    protected void SendMail(object sender, EventArgs e)
    {
    string emptyfield = "";
    if (kname.Text == "")
    emptyfield += "Name is required" + Environment.NewLine;
    if (kemail.Text == "")
    emptyfield += "Email address is required" + Environment.NewLine;
    if (kmessage.Text == "")
    emptyfield += "Message is required" + Environment.NewLine;
    if (emptyfield == "")
    {
    ResponseLiteral.Text = @"<p>" + emptyfield + "</p>";
    }
    else
    {
    string body = "Items send from the contactform:" + Environment.NewLine + Environment.NewLine;

    body += "name :" + kname.Text;
    body += "company :" + kcompany.Text;
    body += "email :" + kemail.Text;
    body += "telephone:" + ktelephone.Text + Environment.NewLine + Environment.NewLine;
    body += kmessage.Text + Environment.NewLine + Environment.NewLine;
    body += Environment.NewLine + "Regards," + Environment.NewLine;
    body += kname.Text;

    try
    {
    umbraco.library.SendMail(kemail.Text, "my@address", "Contactform from the website", body, false);
    ResponseLiteral.Text = @"<p>The form has been send...</p>";
    }
    catch (Exception)
    {
    ResponseLiteral.Text = @"<p>There is a problem sending the form</p>";
    }
    }
    FormPanel.Visible = false;
    ResponsePanel.Visible = true;
    }
    </script>
    <form runat="server">
    <asp:Panel ID="FormPanel" runat="server">
    <label class="div_contact_item">name</label><br />
    <asp:TextBox ID="kname" runat="server" CssClass="contact_data" /><br />

    <label class="div_contact_item">company</label><br />
    <asp:TextBox ID="kbedrijf" runat="server" CssClass="contact_data" /><br />

    <label class="div_contact_item">email</label><br />
    <asp:TextBox ID="kemail" runat="server" CssClass="contact_data" /><br />

    <label class="div_contact_item">telefoon</label><br />
    <asp:TextBox ID="ktelefoon" runat="server" CssClass="contact_data" /><br />

    <label class="div_contact_item">vraag</label><br />
    <asp:TextBox ID="kvraag" TextMode="multiline" runat="server" CssClass="contact_data1" /><br />

    <asp:Button ID="SendButton" runat="server" Text="send" OnClick="SendMail" />
    </asp:Panel>
    <asp:Panel ID="ResponsePanel" runat="server" Visible="false">
    <div class="responseDivClass">
    <asp:Literal ID="ResponseLiteral" runat="server" />
    </div>
    </asp:Panel>
    </form>

    Any idea ?

     

    Maarten

  • Maarten Boer 61 posts 82 karma points
    May 25, 2011 @ 20:43
    Maarten Boer
    0

    Thanks Jesper...

    I got it solved

     

    Maarten

  • Jesper Hauge 298 posts 487 karma points c-trib
    May 29, 2011 @ 14:39
    Jesper Hauge
    0

    Hi Maarten, 

    And sorry for my late reply, I've been offline since Wednesday morning.

    A little tip for you: When concatenating strings in .net it's much more efficient (as in using fewer cpu cycles) using a stringbuilder than concatenating with +=. It's probably negligable in this case, but hey good habits are invaluable right? Try:

    using System.Text;
    
    protected void SendMail(object sender, EventArgs e)
    {
      // Other code
    
      var body = new StringBuilder();
      body.AppendLine("Items sent from contact form:")
      body.AppendLine();
      body.AppendFormat("name   : {0}", kname.Text);
      body.AppendLine();
      // More concatenations
      umbraco.library.SendMail(kemail.Text, "my@address", "Contactform", body.ToString(), false);
    
      // etc.
    }

    Glad you succeeded in getting you contact form working.

    Regards
    Jesper Hauge

  • Maarten Boer 61 posts 82 karma points
    May 30, 2011 @ 08:19
    Maarten Boer
    0

    Thanks Jesper,

    I changed the script according your adjustments...

    One small question.. for some reason i'm unable to send the mail to more then 1 recipient. Something like this:

    umbraco.library.SendMail(kemail.Text, "my@address; colleage@addres", "Contactform", body.ToString(), false);

    Both recipients have the same domain mailaccounts; [email protected] and [email protected]
    For some reason only the last recipient receives the mail. 

    Any Idea?

    And last...i would like the recipient(s) to be variables that can be inserted with the macro. I've never done this before.

    Maarten

  • Jesper Hauge 298 posts 487 karma points c-trib
    May 30, 2011 @ 09:07
    Jesper Hauge
    1

    Hi Maarten,

    Inside the framework umbraco.library.SendMail calls new MailMessage(string, string) which accepts multiple adresses, separated by commas try sending a string looking like this: "[email protected],[email protected]", and it should be okay.

    Regarding getting variables from the macro into the usercontrol I think you can do it using macro parameters and a public property on the usercontrol.

    There's a guide on how to do that in this PDF: http://umbraco.com/media/42a0202c-b0ba-4f84-bcf1-64dfd5230322-usingcontrolswithumbraco.pdf

    Regards
    Jesper Hauge

     

  • Maarten Boer 61 posts 82 karma points
    May 30, 2011 @ 23:31
    Maarten Boer
    0

    Hi Jesper, Thanks for all your help.
    I got everything working now and learned a few things.

    kind regards,
    Maarten Boer

Please Sign in or register to post replies

Write your reply to:

Draft