SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Empty email from contact form ?!

    I have a problem i get empty emails from my contact form. Should there have been something in these emails ? What could be wrong.

    Here is the code.
    HTML Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1" />
    <meta name="viewport" content="width=device-width" />
    <title>EMAIL</title>
    </head>
    </head>
    <body>
    <?php
    	$email_to = "mail@mymail.com";/* YOUR EMAIL HERE! */
    	$name = $_POST["name"];
    	$email = $_POST["email"];
    	$url = $_POST["url"];
    	$message = $_POST["message"];
    	$text = "USERS NAME: $name<br>
    			 USERS EMAIL: $email<br>
    			 USERES TLF: $url<br>		 
    			 USERES MESSAGE: $message";
    	$headers = "MIME-Version: 1.0" . "\r\n"; 
    	$headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
    	$headers .= "FRON: <$email>" . "\r\n";
    	mail($email_to, "Message", $text, $headers);
    ?>
    
    </body>
    </html>
    				
                    
                    
                    
                    
                    <form action="send.php" id="contact-form" class="simple-form " method="post">
    					<div class="one-fourth">
    						<fieldset>
    							<label><span>* </span>YOUR NAME </label>
    							<input type="text" class="text requiredField" name="name"/>
    						</fieldset>
    					</div>
    					<div class="one-fourth">
    						<fieldset>
    							<label><span>* </span>YOUR Email </label>
    							<input type="text" class="requiredField email" name="email"/>
    						</fieldset>
    					</div>
    					<div class="one-fourth last">
    						<fieldset>
    							<label>Telephone</label>
    							<input type="text" class="url" name="url"/>
    						</fieldset>
    					</div>
    					<div class="three-fourth last">
    						<fieldset>
    							<label><span>* </span>Your MESSAGE </label>
    							<textarea cols="30" rows="6" class="text requiredField" name="message"></textarea>
    						</fieldset>
    						<fieldset>
    							<input type="submit" value="Send message" class="button small black"/>
    						</fieldset>
    					</div>
    				</form>

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi ingenting,

    You've got a typo in your mail headers:
    PHP Code:
    $headers .= "FRON: <$email>" "\r\n"
    Also, does this code come from two separate files? If not, your form HTML should be before the closing </body> tag. Also, you might want to wrap your php code in an IF statement so it doesn't get run when you first open the page.. something like:
    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
    // send your email here


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Hi ingenting,

    You've got a typo in your mail headers:
    PHP Code:
    $headers .= "FRON: <$email>" "\r\n"
    Also, does this code come from two separate files? If not, your form HTML should be before the closing </body> tag. Also, you might want to wrap your php code in an IF statement so it doesn't get run when you first open the page.. something like:
    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
    // send your email here

    I dont think that is the mistake - also i just changed it quick to english from my language.

    It is in to seperate files - send.php and contact.html

    If there is no coding mistake, could it then be spam ? or something with my email ?

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    I think the headers have to be in english to work - according to wikipedia they are translated by the email client (http://en.wikipedia.org/wiki/MIME). When I tested using 'FRON' instead of 'From' the email doesn't send.

    Another thing that might also prevent your email from being sent though is that your email server might require the FROM header to be set to a particular email address, in which case you can set the Reply-To header with the user's submitted email address, to allow you to reply to the emails.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay i will try to change it to FROM: What about USERS EMAIL, USERS TLF, USERS MESSAGE then:
    HTML Code:
    $text = "USERS NAME: $name
    USERS EMAIL: $email
    USERES TLF: $url		 
    USERES MESSAGE: $message";
    Should it also be in english ?

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    No, only the header names.. the subject line and the text of your message can be in any language.

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't know if the headers are case specific but it's best to use the standard usage:
    Code:
    From: 
    To: 
    Cc: 
    Bcc: 
    Return-path: 
    Reply-To: 
    Subject:
    followed by a space. You can use additional custom headers if you wish:
    Code:
    X-FRON: Joe
    X-My-other-custom-header: hi there
    Denny Schlesinger
    web services

  8. #8
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,200
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Rather than this:

    Code php:
    $text = "USERS NAME: $name<br>
        USERS EMAIL: $email<br>
        USERES TLF: $url<br>		 
        USERES MESSAGE: $message";

    try this

    Code php:
    $text = "User's Name: $name\n" .
        "User's Email: $email\n" .
        "User's TLF: $url\n" .
        "User's Message:\n\n" .
        "$message";

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, for reply

    HTML Code:
    $text = "User's Name: $name\n" .
        "User's Email: $email\n" .
        "User's TLF: $url\n" .
        "User's Message:\n\n" .
        "$message";
    Why should i use this ? And what does the /n do ?

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have read on other forums that empty emails good come from google crawlers - true ?

  11. #11
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ingenting View Post
    HTML Code:
    $text = "User's Name: $name\n" .
        "User's Email: $email\n" .
        "User's TLF: $url\n" .
        "User's Message:\n\n" .
        "$message";
    Why should i use this ? And what does the /n do ?
    The \n inserts a line break.. it's what you'd want to do if you're sending a plain text email, rather than a HTML one.

    Quote Originally Posted by ingenting View Post
    I have read on other forums that empty emails good come from google crawlers - true ?
    I'm not aware of GoogleBots triggering email forms.. I always thought they didn't touch forms. In any case, you really should be checking for valid form fields before sending the email.

  12. #12
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "In any case, you really should be checking for valid form fields before sending the email."

    What do you mean by valid form fields ?

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay is this then correct ? I have changed "ulr" to "telephone (Tlf)" as i dont need a url from user. Is this also okay ?

    HTML Code:
    <?php
    	$email_to = "my@email.com";
    	$name = $_POST["name"];
    	$email = $_POST["email"];
    	$url = $_POST["url"];
    	$message = $_POST["message"];
    	$text = "User's Name: $name\n" .
        "User's Email: $email\n" .
        "User's TLF: $url\n" .
        "User's Message:\n\n" .
        "$message";
    	$headers = "MIME-Version: 1.0" . "\r\n"; 
    	$headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
    	$headers .= "FROM: <$email>" . "\r\n";
    	mail($email_to, "Message", $text, $headers);
    ?>
    
    <form action="send.php" id="contact-form" class="simple-form " method="post">
    					<div class="one-fourth">
    						<fieldset>
    							<label><span>* </span>Your name </label>
    							<input type="text" class="text requiredField" name="name"/>
    						</fieldset>
    					</div>
    					<div class="one-fourth">
    						<fieldset>
    							<label><span>* </span>Email </label>
    							<input type="text" class="requiredField email" name="email"/>
    						</fieldset>
    					</div>
    					<div class="one-fourth last">
    						<fieldset>
    							<label>Telephone</label>
    							<input type="text" class="url" name="url"/>
    						</fieldset>
    					</div>
    					<div class="three-fourth last">
    						<fieldset>
    							<label><span>* </span>Your message </label>
    							<textarea cols="30" rows="6" class="text requiredField" name="message"></textarea>
    						</fieldset>
    						<fieldset>
    							<input type="submit" value="Send message" class="button small black"/>
    						</fieldset>
    					</div>
    				</form>

  14. #14
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ingenting View Post
    What do you mean by valid form fields ?
    Well, checking that fields like name, email and message aren't empty, making it impossible to be sent an empty email. Also you probably want to check that the email address is a valid one (i.e. name@domain, and not just random text or something).

  15. #15
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you check if the above code is correct ? I would like to check that the email is valid, what could i add to do that ?

  16. #16
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ingenting View Post
    I have read on other forums that empty emails good come from google crawlers - true ?
    False, provided the email form is properly done. I say this based on ten year's evidence.

    First you must exclude bots with a robots.txt file in your document root, for example:
    Code:
    User-agent: *
    Disallow: /cgi-bin/
    Disallow: /errors/
    Disallow: /images/
    Disallow: /contact.php
    http://www.robotstxt.org/

    Good robots will not touch your contact page. For added security you can include a robot meta tag:
    HTML Code:
    <meta name="robot" content="noindex, nofollow">
    Bad and evil robots will try to spam your email form and if you don't take precautions they will. They could cause empty emails.

    I have an email script that has never sent an email from a bad or evil crawler in ten years. It does not send the email directly. Instead of a Submit button it has a Review button which displays the email for preview and editing using Sessions. Bad and evil robots have not yet mastered the fine art of reviewing their SPAM. Since I also record all the accesses to the email form I have their IP addresses and if they are persistent I exclude them from the server altogether.

    Please don't blame good old Googlebot for the harm created by Evilbots.
    Denny Schlesinger
    web services

  17. #17
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    False, provided the email form is properly done. I say this based on ten year's evidence.

    First you must exclude bots with a robots.txt file in your document root, for example:
    Code:
    User-agent: *
    Disallow: /cgi-bin/
    Disallow: /errors/
    Disallow: /images/
    Disallow: /contact.php
    http://www.robotstxt.org/

    Good robots will not touch your contact page. For added security you can include a robot meta tag:
    HTML Code:
    <meta name="robot" content="noindex, nofollow">
    Bad and evil robots will try to spam your email form and if you don't take precautions they will. They could cause empty emails.

    I have an email script that has never sent an email from a bad or evil crawler in ten years. It does not send the email directly. Instead of a Submit button it has a Review button which displays the email for preview and editing using Sessions. Bad and evil robots have not yet mastered the fine art of reviewing their SPAM. Since I also record all the accesses to the email form I have their IP addresses and if they are persistent I exclude them from the server altogether.

    Please don't blame good old Googlebot for the harm created by Evilbots.
    Thanks, so i add this to my robots.txt

    User-agent: *
    Disallow: send.php

    send.php is in root. Should it be :

    Disallow: /send.php or Disallow: send.php ?

    Could you check if my send.php above is okay ?

  18. #18
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ingenting View Post
    Can you check if the above code is correct ? I would like to check that the email is valid, what could i add to do that ?
    The code seems OK.. although I'd put a check around the PHP code so that it only runs once the form has been submitted, not when you first load the page:
    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        
    // your emailing code here

    As far as validating the email goes, as a start you could use a simple regex pattern - here's an example function which will return true for valid addresses:
    PHP Code:
    function valid_email($email) {
        
    regex "/^[\S]+@[\S]+\.[\S]+$/"
        
    return (bool) preg_match($regex, (string) $email);

    This is just a basic check, it won't screen out all invalid addresses, but it gets you about 90% of the way there.

  19. #19
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ingenting View Post
    Disallow: /send.php or Disallow: send.php ?
    Disallow: /send.php

  20. #20
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ingenting View Post
    Thanks, so i add this to my robots.txt

    User-agent: *
    Disallow: send.php

    send.php is in root. Should it be :

    Disallow: /send.php or Disallow: send.php ?

    Could you check if my send.php above is okay ?
    Don't "disallow" the send.php page but the one with the html form. If it is in the document root it would be:
    Code:
    User-agent: *
    Disallow: /form.php
    Denny Schlesinger
    web services

  21. #21
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    The code seems OK.. although I'd put a check around the PHP code so that it only runs once the form has been submitted, not when you first load the page:
    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        
    // your emailing code here

    As far as validating the email goes, as a start you could use a simple regex pattern - here's an example function which will return true for valid addresses:
    PHP Code:
    function valid_email($email) {
        
    regex "/^[\S]+@[\S]+\.[\S]+$/"
        
    return (bool) preg_match($regex, (string) $email);

    This is just a basic check, it won't screen out all invalid addresses, but it gets you about 90% of the way there.
    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        
        
    $email_to "mail@email.com";
        
    $name $_POST["name"];
        
    $email $_POST["email"];
        
    $url $_POST["url"];
        
    $message $_POST["message"];
        
    $text "User's Name: $name\n" .
        
    "User's Email: $email\n" .
        
    "User's TLF: $url\n" .
        
    "User's Message:\n\n" .
        
    "$message";
        
    $headers "MIME-Version: 1.0" "\r\n"
        
    $headers .= "Content-type:text/html; charset=utf-8" "\r\n"
        
    $headers .= "FROM: <$email>" "\r\n";
        
    mail($email_to"Message"$text$headers);

    ?>
    Above is okay ?

    Sorry not sure how to use/place it:
    PHP Code:
    function valid_email($email) {
        
    regex "/^[\S]+@[\S]+\.[\S]+$/"
        
    return (bool) preg_match($regex, (string) $email);


  22. #22
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    You could do something like this:
    PHP Code:
    function valid_email($email) { 
        
    $regex "/^[\S]+@[\S]+\.[\S]+$/" 
        
    return (bool) preg_match($regex, (string) $email); 


    if (
    $_SERVER['REQUEST_METHOD'] === 'POST') {      
        
    $email_to "mail@email.com"
        
    $name $_POST["name"]; 
        
    $email $_POST["email"]; 
        
    $url $_POST["url"]; 
        
    $message $_POST["message"];

        
    // Check for valid form data
        
    if (!empty($email) AND valid_email($email) AND !empty($name) AND !empty($message)) {
             
    $text "User's Name: $name\n" 
            
    "User's Email: $email\n" 
            
    "User's TLF: $url\n" 
            
    "User's Message:\n\n" 
            
    "$message"
            
    $headers "MIME-Version: 1.0" "\r\n";  
            
    $headers .= "Content-type:text/html; charset=utf-8" "\r\n";  
            
    $headers .= "FROM: <$email>" "\r\n"
            
    mail($email_to"Message"$text$headers); 
        }  


  23. #23
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    You could do something like this:
    PHP Code:
    function valid_email($email) { 
        
    $regex "/^[\S]+@[\S]+\.[\S]+$/" 
        
    return (bool) preg_match($regex, (string) $email); 


    if (
    $_SERVER['REQUEST_METHOD'] === 'POST') {      
        
    $email_to "mail@email.com"
        
    $name $_POST["name"]; 
        
    $email $_POST["email"]; 
        
    $url $_POST["url"]; 
        
    $message $_POST["message"];

        
    // Check for valid form data
        
    if (!empty($email) AND valid_email($email) AND !empty($name) AND !empty($message)) {
             
    $text "User's Name: $name\n" 
            
    "User's Email: $email\n" 
            
    "User's TLF: $url\n" 
            
    "User's Message:\n\n" 
            
    "$message"
            
    $headers "MIME-Version: 1.0" "\r\n";  
            
    $headers .= "Content-type:text/html; charset=utf-8" "\r\n";  
            
    $headers .= "FROM: <$email>" "\r\n"
            
    mail($email_to"Message"$text$headers); 
        }  

    I have just copy/pasted above code
    I get an error on this line :
    PHP Code:
    return (bool) preg_match($regex, (string) $email); 

  24. #24
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,408
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ingenting View Post
    I get an error on this line :
    PHP Code:
    return (bool) preg_match($regex, (string) $email); 
    Sorry, looks like I missed a semicolon off the end of this line $regex = "/^[\S]+@[\S]+\.[\S]+$/"

  25. #25
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great that helped.
    Thanks alot for all your help, cant wait to see if this also makes the empty emails disappear.
    Thanks again


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
  •