SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast weblover1984's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP mail to, help?

    Hi,
    I'm trying to find a solution to my php contact form. It seems I cannot get the form to send to my email address and I need some help please.

    This is my contact form:-
    <h3>Drop a line</h3>

    <form action="#" id="contact_form">

    <input type="text" id="name" value="Your name" />

    <input type="email" id="email" value="Your e-mail" />

    <div class="clearfix"></div>

    <div class="textarea"><textarea cols="30" id="comment" rows="10"></textarea></div>

    <input type="submit" value="Submit" />

    <span class="status"></span>

    </form>
    <script src="js/script.js"></script>

    and this is the php within script.js:-
    /* Contact form */
    $('#contact_form').submit(function () {
    $.ajax({
    type: 'POST',
    url: 'contact.php',
    data: {
    name: $('#contact_form input[type=text]').val(),
    email: $("#contact_form input[type=email]").val(),
    text: $("#contact_form textarea").val()
    },
    success: function(data) {
    if ( data == 'sent' ) {
    $('#contact_form .status').html('Your message is sent!');
    } else if ( data == 'invalid' ) {
    $('#contact_form .status').html('Your name, email or message is invalid.');
    } else {
    $('#contact_form .status').html('Your message could not be sent.');
    }
    },
    error: function () {
    $('#contact_form .status').html('Your message could not be sent.');
    }
    });
    return false;
    });
    What do I need in contact.php in order to have the form information sent to my email address?

  2. #2
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://www.w3schools.com/php/func_mail_mail.asp


    refer this for mail function. just get your email address and pass that to the mail function

  3. #3
    SitePoint Enthusiast weblover1984's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Contact.php:-
    <?php
    $to = "MY EMAIL ADDRESS HERE";
    ?>
    It does not work, it displays "Your message could not be sent."
    Sorry but PHP isn't my forte, any other ideas?

  4. #4
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    did you use the same example like in w3 schools ?

  5. #5
    SitePoint Enthusiast weblover1984's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea, but I don't need the rest such as:

    subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: webmaster@exam ple.com" . "\r\n" .
    "CC: somebodyelse@exam ple.com";

    etc, I only need a php with my email address.

  6. #6
    SitePoint Addict goldensona's Avatar
    Join Date
    Jun 2011
    Posts
    226
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    can you tell what you want to do? post your code so better to guide.

    use the required field and try with that.

  7. #7
    SitePoint Enthusiast weblover1984's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This seems to work:

    <?php

    // Enter your e-mail address.
    $to = 'example@exam ple.com';

    // Go to your-site.com/contact.php?test to send a testing email.
    if ( isset($_GET['test']) ) {
    mail($to, 'Message from contact form', 'It\'s working!', 'From: ' . $to . "\r\n");
    die('Testing e-mail has been sent.');
    }

    function isValidEmail( $email = null )
    {
    return preg_match( "/^
    [\d\w\/+!=#|$?%{^&}*`'~-]
    [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
    [A-Z0-9]
    [A-Z0-9.-]{0,61}
    [A-Z0-9]\.
    [A-Z]{2,6}$/ix", $email );
    }

    if ( !empty($_POST['name']) && isValidEmail($_POST['email']) && !empty($_POST['text'])) {

    $message = $_POST['text'];
    $headers = 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>' . "\r\n" . 'Reply-To: ' . $_POST['email'];

    if ( mail($to, 'Message from contact form', $message, $headers) ) echo 'sent';

    } else {
    echo 'invalid';
    }

  8. #8
    SitePoint Enthusiast weblover1984's Avatar
    Join Date
    Jul 2010
    Location
    UK
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need a action.php file for this:


    <form action="action.php" method="post">
    <input type="text" name="name" value="Name" class="placeholder" />
    <input type="text" name="email" value="E-mail" class="placeholder" />
    <textarea name="message" class="placeholder">Message</textarea>
    <div class="status">email was sent<br/>successfully</div>
    <div class="button mouse-events"><div>Send email</div></div>
    </form>
    Any ideas?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •