Copied to clipboard

Flag this post as spam?

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


  • Evan 344 posts 99 karma points
    Jun 08, 2009 @ 18:50
    Evan
    0

    Trying to send/store IP addresses through contact form using CWS...

    SOLVED I am getting stuck, I am trying to use the contact from that comes with cws (doc2form maybe I am not sure) and I can not seem to get the IP address to send through. In the settings section of my content section for the contact form I am trying to use [IP] to show where the IP will be displayed in the email. What could I be doing wrong?

    [code]
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    // Extra Namespaces we have imported
    using System.Net;
    using System.Net.Mail;
    using System.Net.Configuration;
    using System.Text.RegularExpressions;
    using System.Configuration;
    using System.Web.Configuration;
    using System.Xml;
    using System.Text;
    using System.IO;

    using umbraco;
    using umbraco.presentation.nodeFactory;

    namespace umbracowebsitewizardsite.Usercontrols
    {
    public partial class ContactForm : System.Web.UI.UserControl
    {

    #region Properties from Umbraco

    private string _EmailTo, _EmailSubject, _EmailBody, _EmailReplyFrom, _EmailReplySubject, _EmailReplyBody, _FormHeader, _FormText, _ThankYouHeaderText, _ThankYouMessageText;
    private bool _EnableSSL;

    public string EmailTo
    {
    get
    {
    return _EmailTo;
    }
    set
    {
    _EmailTo = value;
    }
    }

    public string EmailSubject
    {
    get
    {
    return _EmailSubject;
    }
    set
    {
    _EmailSubject = value;
    }
    }

    public string EmailBody
    {
    get
    {
    return _EmailBody;
    }
    set
    {
    _EmailBody = value;
    }
    }

    public string EmailReplyFrom
    {
    get
    {
    return _EmailReplyFrom;
    }
    set
    {
    _EmailReplyFrom = value;
    }
    }

    public string EmailReplySubject
    {
    get
    {
    return _EmailReplySubject;
    }
    set
    {
    _EmailReplySubject = value;
    }
    }

    public string EmailReplyBody
    {
    get
    {
    return _EmailReplyBody;
    }
    set
    {
    _EmailReplyBody = value;
    }
    }

    public string FormHeader
    {
    get
    {
    return _FormHeader;
    }
    set
    {
    _FormHeader = value;
    }
    }

    public string FormText
    {
    get
    {
    return _FormText;
    }
    set
    {
    _FormText = value;
    }
    }

    public string ThankYouHeaderText
    {
    get
    {
    return _ThankYouHeaderText;
    }
    set
    {
    _ThankYouHeaderText = value;
    }
    }

    public string ThankYouMessageText
    {
    get
    {
    return _ThankYouMessageText;
    }
    set
    {
    _ThankYouMessageText = value;
    }
    }

    public bool EnableSSL
    {
    get
    {
    return _EnableSSL;
    }
    set
    {
    _EnableSSL = value;
    }
    }

    #endregion

    #region PageEvents

    protected void Page
    Load(object sender, EventArgs e)
    {
    /
    ========================================================
    Get the variables from the form and populate the
    literal controls on the usercontrol .ascx file
    ========================================================
    */

    // Get the currentNode and call it currentNode
    Node currentNode = Node.GetCurrent();

    /

    ========================================================
    Build up the formHeader text as follows.
    The node name in strong to make it pink in sIFR and
    then our FormHeaderText from our node we are passing in
    ========================================================
    /
    formHeader.Text = "" + currentNode.Name + ". ";

    /

    ========================================================
    We are just settig formText to be equal to our property
    from our macro to FormText
    ========================================================

    umbraco.library.RemoveFirstParagraphTag(formText.Text = FormText);/
    }

    public void btnSubmit_Click(object sender, EventArgs e)
    {

    //Proceed only if the vaidation is successfull
    if (!Page.IsValid)
    {
    //Stop everything we are doing and exit.
    return;
    }


    /

    ========================================================
    Get the variables from the form and set them in strings
    ========================================================
    /

    string strName, strAddressLine1, strAddressLine2, strMessage, ipAddr;

    strName = name.Text;
    strAddressLine1 = addressLine1.Text;
    strAddressLine2 = addressLine2.Text;
    strMessage = message.Text;
    ipAddr = HttpContext.Current.Request.UserHostAddress;

    /

    ========================================================
    Lets set the values passed in from the Macro
    ========================================================
    /

    string strEmailFrom, strEmailTo, strEmailSubject, strEmailBody, strTime, strDate;

    strEmailFrom = emailAddress.Text; //The email will be sent from the address the user typed in the form.
    strEmailTo = EmailTo;
    strEmailSubject = EmailSubject;
    strTime = String.Format("{0:HH:mm:ss}", DateTime.Now);
    strDate = String.Format("{0:dd/MM/yyyy}", DateTime.Now);

    /

    ========================================================
    Lets Replace the placeholders in the email message body
    ========================================================
    /
    strEmailBody = EmailBody;


    /

    ========================================================
    Find and Replace [Name]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[Name]", strName);


    /

    ========================================================
    Find and Replace [AddressLine1]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[AddressLine1]", strAddressLine1);

    /

    ========================================================
    Find and Replace [AddressLine2]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[AddressLine2]", strAddressLine2);

    /

    ========================================================
    Find and Replace [Email]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[Email]", strEmailFrom);

    /

    ========================================================
    Find and Replace [Message]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[Message]", strMessage);

    /

    ========================================================
    Find and Replace [Time]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[Time]", strTime);

    /

    ========================================================
    Find and Replace [Date]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[Date]", strDate);

    /

    ========================================================
    Find and Replace [IP]
    ========================================================
    /
    strEmailBody = strEmailBody.Replace("[IP]", ipAddr);


    /

    ===========================================================
    Lets setup the SMTP client using settings from web.config
    ===========================================================
    /
    SmtpClient mySMTPClient = new SmtpClient();

    //If EnableSSL set to true lets enable it on our SMTP object.
    if (EnableSSL)
    {
    mySMTPClient.EnableSsl = true;
    }


    /

    ===============================================================
    TRY to send email using the web.config settings to site owner
    ===============================================================
    /
    try
    {
    // Send email (From email address, Recipients email address, Email Subject, Email Message)
    mySMTPClient.Send(strEmailFrom, strEmailTo, strEmailSubject, strEmailBody);

    }
    catch (SmtpException ex)
    {
    //If mail fails to send for any reason
    errorMessage.Text = ex.Message;
    ErrorMailSettings.Visible = true;
    FormFields.Visible = true;

    //If we fail to send email immediately stop runnning code
    return;
    }

    /

    ====================================================================================
    Now the email is sent out to the owner, lets send out an email
    to let the user know we have recieved their email & will respond shortly
    ====================================================================================
    /
    string strEmailReplyFrom, strEmailReplySubject, strEmailReplyBody;

    strEmailReplyFrom = EmailReplyFrom;
    strEmailReplySubject = EmailReplySubject;
    strEmailReplyBody = EmailReplyBody;



    /

    ========================================================
    Find and Replace [Name]
    ========================================================
    /
    strEmailReplyBody = strEmailReplyBody.Replace("[Name]", strName);



    try
    {
    // Send email (From email address, Recipients email address, Email Subject, Email Message)
    mySMTPClient.Send(strEmailReplyFrom, strEmailFrom, strEmailReplySubject, strEmailReplyBody);

    }

    catch (SmtpException ex)
    {
    //If mail fails to send for any reason
    errorMessage.Text = ex.Message;
    ErrorMailSettings.Visible = true;
    FormFields.Visible = true;

    //If we fail to send email immediately stop runnning code
    return;
    }

    /

    ========================================================
    Log the item to the XML file
    ========================================================
    /
    logItem(strName, strAddressLine1, strAddressLine2, strEmailFrom, strMessage);


    /

    ========================================================
    Set Thankyou text from our contact node in our
    hidden thankyou DIV
    ========================================================
    /
    thankyouHeader.Text = ThankYouHeaderText;
    thankyouMessage.Text = ThankYouMessageText;

    /

    ========================================================
    Hide & Show div's to show our thankyou message
    ========================================================
    /
    FormFields.Visible = false; //Hide the FormFields DIV
    ErrorMailSettings.Visible = false; //Hide the errormessage DIV
    ThankYou.Visible = true; //SHOW our thankyou message DIV
    }


    //Call this to log the contact form to XML file.
    private void logItem(string strName, string strAddressLine1, string strAddressLine2, string strEmail, string strMessage)
    {
    string strTime = String.Format("{0:HH:mm:ss}", DateTime.Now);
    string strDate = String.Format("{0:dd/MM/yyyy}", DateTime.Now);

    string filePath = Server.MapPath("~/data/contact_form_log.xml");


    //Check if XML file exists
    if (!File.Exists(filePath))
    {
    //XML file DOES not exist yet.

    // Create a new XmlTextWriter instance
    XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);

    //Set the XML writer to indent the XML as opposed to all one line.
    writer.Formatting = Formatting.Indented;

    // start writing XML!
    writer.WriteStartDocument();

    // Create the

    ========================================================
    Add the

  • Evan 344 posts 99 karma points
    Jun 08, 2009 @ 19:21
    Evan
    0

    Solved it if anyone is interested the correct code is posted above...

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies