SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HTML Form and Sendmail.php sending blank emails

    Hi all,

    I am very new to html and ran into problems with my html and php code for form sending. It is just a simple form, but I have been stuck here for hours and finally decided to ask some experts for advice so I can learn more.

    Below is the php code (sendemail.php):

    PHP Code:

    <?php
        header
    ('Content-type: application/json');
        
    $status = array(
            
    'type'=>'success',
            
    'message'=>'Email sent!'
    );


    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $contact = @trim(stripslashes($_POST['contact'])); 
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from $email;
    $email_to 'myemail@gmail.com';

    $body 'Name: ' $name "\n\n" 'Email: ' $email "\n\n" 'Contact: ' $contact "\n\n" 'Subject: ' $subject "\n\n" 'Message: ' $message;

    $success = @mail($email_to$subject$body'From: <'.$email_from.'>');


    echo 
    json_encode($status);
    die;
    and below is the form code:

    Code:
    <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
    <div class="row-fluid">
    <div class="span5">
    
    <label>Name</label>
    <input type="text" class="name" required="required" placeholder="Your name">
    
    <label>Email Address</label>
    <input type="text" class="email" required="required" placeholder="Your email address">
    
    <label>Contact</label>
    <input type="text" class="contact" required="required" placeholder="Your contact number">
    
    <label>Subject</label>
    <input type="text" class="subject" required="required" placeholder="The subject of your inquiry">
    
    </div>
    
    <div class="span7">
    
    <label>Message</label>
    <textarea name="message" id="message" required="required" class="message" rows="8" placeholder="Your inquiry"></textarea>
    
    </div>
    
    </div>
    
    <button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
    
    <p></p>
    
    </form>
    Thanks in advance and sorry for the inconvenience caused!

  2. #2
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remove the @ sign before your mail() function call so that you aren't suppressing errors (they will then be output to the browser).

    Also try moving your header() call to just before your echo() at the bottom.

  3. #3
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the help, unfortunately it still did not work. I have removed the @ and shifted the header() to before echo(). This is what i got on my email (with no subject title as well):

    Name:

    Email:

    Contact:

    Subject:

    Message:

    Any more advises?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    All those nasty error suppressors (the leading @'s) aren't helping you debug any.

    $_POST variables come from a form's (whose method = POST) inputs based on their name values.
    I don't see any "name=" in your form code except for message.

    And it would be a good idea to be more strict with the user supplied input.

  5. #5
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I was thinking of getting it working first before inserting restrictions so I can learn step by step. Thanks a bunch!

    I have removed all @ at the php and also replaced "class=" with "name=" at the form, unfortunately it is still sending in blank emails. Below is the updated codes:

    sendemail.php:

    PHP Code:
    <?php
    header
    ('Content-type: application/json');
        
    $status = array(
            
    'type'=>'success',
            
    'message'=>'Email sent!'
    );


    $name $_POST['name']; 
    $email $_POST['email']; 
    $contact $_POST['contact']; 
    $subject $_POST['subject']; 
    $message $_POST['message']; 

    $email_from $email;
    $email_to 'myemail@email.com';

    $body 'Name: ' $name "\n\n" 'Email: ' $email "\n\n" 'Contact: ' $contact "\n\n" 'Subject: ' $subject "\n\n" 'Message: ' $message;

    $success mail($email_to$subject$body'From: <'.$email_from.'>');


    echo 
    json_encode($status);
    die;
    form code:

    Code:
    <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
    <div class="row-fluid">
    <div class="span5">
    
    <label>Name</label>
    <input type="text" name="name" required="required" placeholder="Your name">
    
    <label>Email Address</label>
    <input type="text" name="email" required="required" placeholder="Your email address">
    
    <label>Contact</label>
    <input type="text" name="contact" required="required" placeholder="Your contact number">
    
    <label>Subject</label>
    <input type="text" name="subject" required="required" placeholder="The subject of your inquiry">
    
    </div>
    
    <div class="span7">
    
    <label>Message</label>
    <textarea name="message" id="message" required="required"  rows="8" placeholder="Your inquiry"></textarea>
    
    </div>
    
    </div>
    
    <button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
    
    <p></p>
    
    </form>

    Thanks in advance and sorry for any inconvenience caused!

  6. #6
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add:

    Code PHP:
    print_r($_POST); exit;

    At the top of sendemail.php to see what values/variables are being passed via POST. Then we can investigate further.


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
  •