www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: problem with PHP email script

  1. #1
    Join Date
    Apr 2006
    Posts
    120

    problem with PHP email script

    I've been using this script for the last couple months with no problems. It would generate an email within seconds every time. About a week ago, for reasons I have not be able to discover, it stopped sending the emails.

    I'm hoping someone here can shed some light on what's wrong.

    Here is my HTML:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
    	ul, li, p, body {
    		margin: 0;
    		padding: 0;
    	}
    	
    	body {
    		color: #333;
    		background: url(images/bkgGradient.jpg) repeat-x #fff4a0;
    		font-family: arial, helvetica, sans-serif;
    		font-size: 12px;
    	}
    	
    	.error_strings { 
    		font-size: 12px; 
    		color: #900;
    		margin: 2px 0px 12px;
    	}
    
    	.error_strings ul { 
    		list-style-type: none;
    	}	
    	
    	.formLeft {
    		width: 105px;
    		font-size: 12px;
    		font-weight: bold;
    		margin-top: 6px;
    		float: left;
    	}
    	
    	.formRight {
    		width: 335px;
    		font-size: 12px;
    		margin-top: 3px;
    		float: left;
    	}
    
    </style>
    <script language="javascript" type="text/javascript" src="gen_validatorv5.js"></script>
    </head>
    
    <body>
    
        <form name="request" id="request" method="post" action="thank-you.php">
            <div style="width: 440px">
                <div class="formLeft">Name:</div>
                <div class="formRight"><input type="text" name="name" id="name" style="width: 320px" /></div>
                <div class="formLeft">Address:</div>
                <div class="formRight"><input type="text" name="address" id="address" style="width: 320px" /></div>
                <div class="formLeft">City:</div>
                <div class="formRight"><input type="text" name="city" id="city" style="width: 320px" /></div>
                <div class="formLeft">State:</div>
                <div class="formRight"><input type="text" name="state" id="state" style="width: 320px" /></div>
                <div class="formLeft">Zip:</div>
                <div class="formRight"><input type="text" name="zip" id="zip" style="width: 320px" /></div>
                <div class="formLeft">Email:</div>
                <div class="formRight"><input type="text" name="email" id="email" style="width: 320px" /></div>
                <div class="formLeft">Phone:</div>
                <div class="formRight"><input type="text" name="phone" id="phone" style="width: 320px" /></div>
                <div class="formLeft">Best Time to Call:</div>
                <div class="formRight"><input type="text" name="best" id="best" style="width: 320px" /></div>
                <div class="formLeft">Brief Description of Job:</div>
                <div class="formRight"><textarea name="description" id="description" cols="1" rows="5" style="width: 320px"></textarea></div>
            </div>
            <div style="clear: both"></div>
            <input type="submit" value="Submit" name="submit" style="margin: 6px 0 10px 367px" />
        </form>
    
        <div id="request_errorloc" class="error_strings" style="height: 80px; margin-left: 105px"></div>
        <script language="JavaScript" type="text/javascript">
        var frmvalidator  = new Validator("request");
            frmvalidator.EnableOnPageErrorDisplaySingleBox();
            frmvalidator.EnableMsgsTogether();
            frmvalidator.addValidation("name","req","Please enter your first name");
            frmvalidator.addValidation("email","req","Please enter an email address");
            frmvalidator.addValidation("email","email","Please enter a valid email address");
            frmvalidator.addValidation("phone","req","Please enter your phone number");
            frmvalidator.addValidation("best","req","Please enter the best time for us to call");
            frmvalidator.addValidation("description","req","Please enter a brief description of the job");
        </script>
                    
    
    </body>
    </html>
    and here is the PHP I'm using:
    Code:
    <?php 
    
    // -------------------- EMAIL CONFIGURATION ---------------------
    $mailto = 'smarshall@stltoday.com' ;
    $subject = "Feedback Form Test" ;
    $thankyouurl = "http://dmsgroup.co/templates/thank-you.html" ; // <----- THIS LINE CANE BE REMOVED IF HTML FOLLOWS THE PHP
    // -------------------- END OF EMAIL CONFIGURATION ---------------
    
    // -------------------- FORM FIELDS ---------------------
    $name = $_POST['name'] ;
    $address = $_POST['address'] ;
    $city = $_POST['city'] ;
    $state = $_POST['state'] ;
    $zip = $_POST['zip'] ;
    $email = $_POST['email'] ;
    $phone = $_POST['phone'] ;
    $best = $_POST['best'] ;
    $description = $_POST['description'] ;
    // -------------------- END OF FORM FIELDS ---------------------
    
    // ----- DON'T CHANGE ANYTHING IN THE SECTION BELOW EXCEPT THE NAME OF THE EMAIL FIELD (IF NECESSARY)
    $headersep = $uself ? "\n" : "\r\n" ;
    $nl = "<br />";
    $http_referrer = getenv( "HTTP_REFERER" );
    $headers = "From: \"$name\" <$email>" . $headersep ; // ----- THE NAME OF THE EMAIL FIELD MUST MATCH HERE  ***
    $headers .= "Reply-To: \"$name\" <$email>" . $headersep ; // ----- THE NAME OF THE EMAIL FIELD MUST MATCH HERE  ***
    $headers .= "X-Mailer: chfeedback.php 2.16.2" . $headersep ;
    $headers .= "MIME-Version: 1.0" . $headersep ;
    $headers .= 'Content-Type: text/html; charset="iso-8859-1"' ;
    // ----- DON'T CHANGE ANYTHING IN THE SECTION ABOVE EXCEPT THE NAME OF THE EMAIL FIELD (IF NECESSARY)
    
    // ----- THIS IS WHERE THE EMAIL BODY IS CREATED
    $body = "<span style='font-family: Verdana, sans-serif; font-size: 12px; line-height: 16px'>
    <b>Sent from:</b> &nbsp; $http_referrer by $name (" . "$email" . ")" . // ----- THE NAME OF THE EMAIL FIELD MUST MATCH HERE  ***
    $nl . $nl . 
    "<b>Name:</b> " . $name . $nl .
    "<b>Address:</b> " . $address . $nl .
    "<b>City:</b> " . $city . $nl .
    "<b>State:</b> " . $state . $nl .
    "<b>Zip:</b> " . $zip . $nl .
    "<b>Email:</b> " . $email . $nl .
    "<b>Phone:</b> " . $phone . $nl .
    "<b>Best time to call:</b> " . $best . $nl .
    "<b>Brief description of job:</b> " . $description . $nl . "</span>" ;
    
    mail($mailto, $subject, $body, $headers );
    
    // ----- THE NEXT TWO LINES CAN BE REMOVED IF HTML FOR A THANK YOU PAGE FOLLOWS THE PHP
    header( "Location: $thankyouurl" );
    exit ;                             
    
    ?>
    The actual page is here: http://dmsgroup.co/glass_block_new/fb-app.html

    Thanks in advance to anyone who is able to help!

    SteveMTNO

  2. #2
    Join Date
    May 2012
    Posts
    17
    Is this on a shared hosting? Can you check the apache log file for errors? Or postfix to see if the message is actually being sent?

    Make sure there's no output at all in the file, so no blank lines at the top of the script. Other than that there's not much I can see this being.

  3. #3
    Join Date
    Apr 2006
    Posts
    120
    No, I don't have access to the apache log.

    What's postfix?

    No blank lines at all, or just between these two:
    Code:
    <?php 
    
    // -------------------- EMAIL CONFIGURATION ---------------------

  4. #4
    Join Date
    Apr 2006
    Posts
    120
    I tried taking out the blank line between the two lines above and that didn't make any difference.

  5. #5
    Join Date
    May 2012
    Posts
    17
    No blank linkes before the <?php . The reason for this is leaving a blank line will output headers which could affect your script.

    Posftix is an email program which runs on linux which most shared hosts use for sending email, looking at this would tell you if the email was sent, or what reason it wasn't sent.

    Do you have access to the apache error log? You might find some useful information on there too.

  6. #6
    Join Date
    Apr 2006
    Posts
    120
    Quote Originally Posted by Alan01252 View Post
    No blank linkes before the <?php . The reason for this is leaving a blank line will output headers which could affect your script.
    There are no blank lines before the <?php.

    Quote Originally Posted by Alan01252 View Post
    Posftix is an email program which runs on linux which most shared hosts use for sending email, looking at this would tell you if the email was sent, or what reason it wasn't sent.

    Do you have access to the apache error log? You might find some useful information on there too.
    I'm gonna check with our admin on this. I have no idea on either of these.

  7. #7
    Join Date
    Apr 2006
    Posts
    120
    Do I need to have a "$send = " before the mail statement?

  8. #8
    Join Date
    May 2012
    Posts
    17
    no the mail function will still execute the $send variable will just be assigned the return value of that function. In this case (assuming your emails still aren't being sent) $send will be (boolean) false.

  9. #9
    Join Date
    Apr 2006
    Posts
    120
    I got an older script from our admin. Emails are working now, altho, I'm not sure what the problem was.

    Right now, I'm going thru the old script and gradually updating it with parts of the new code.

    Here's what I've got so far:
    Code:
    <?PHP  putenv("TZ=US/Central"); $time = date("M j, Y, g:i a");
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
    
    $name = $_POST["name"];
    $email = $_POST["email"];
    $comments = $_POST["comments"];
    
    $recipient = "smarshall@stltoday.com";
    $subject = "testing email form";
    
    // ----- THIS IS WHERE THE EMAIL BODY IS CREATED
    $headersep = $uself ? "\n" : "\r\n" ;
    $nl = "<br />";
    $body = "<span style='font-family: Verdana, sans-serif; font-size: 12px; line-height: 16px'>
    <b>Sent from:</b> &nbsp; $http_referrer by $name (" . "$email" . ")" . // ----- THE NAME OF THE EMAIL FIELD MUST MATCH HERE  ***
    $nl . $nl . 
    "<b>Name:</b> " . $name . $nl .
    "<b>Email:</b> " . $email . $nl .
    "<b>Comments:</b> " . $comments . $nl . "</span>" ;
    
    //$body = "<p><b>Name:</b> $name<p><b>Email:</b> $email<p><b>Questions or comments:</b> $comments";
    
    // ----- DON'T CHANGE ANYTHING IN THE SECTION BELOW EXCEPT THE NAME OF THE EMAIL FIELD (IF NECESSARY)
    //$headersep = $uself ? "\n" : "\r\n" ;
    //$nl = "<br />";
    $http_referrer = getenv( "HTTP_REFERER" );
    $headers = "From: \"$name\" <$email>" . $headersep ; // ----- THE NAME OF THE EMAIL FIELD MUST MATCH HERE  ***
    $headers .= "Reply-To: \"$name\" <$email>" . $headersep ; // ----- THE NAME OF THE EMAIL FIELD MUST MATCH HERE  ***
    $headers .= "X-Mailer: chfeedback.php 2.16.2" . $headersep ;
    $headers .= "MIME-Version: 1.0" . $headersep ;
    $headers .= 'Content-Type: text/html; charset="iso-8859-1"' ;
    // ----- DON'T CHANGE ANYTHING IN THE SECTION ABOVE EXCEPT THE NAME OF THE EMAIL FIELD (IF NECESSARY)
    
    $send = mail($recipient,$subject,$body,$headers);
    header( "Location: thank-you.php" ) ;
    }
    else
    {
    	echo '<p><a href="catering.html">Please fill in the form</a></p>';
    }
    ?>

  10. #10
    Join Date
    Apr 2006
    Posts
    120
    I've been using the following to generate the URL that the email is being sent from and place it into the email (scroll up to see the full script):

    Code:
    $http_referrer = getenv( "HTTP_REFERER" );
    
    ... snip...
    
    $body = "<span style='font-family: Verdana, sans-serif; font-size: 12px; line-height: 16px'>
    <b>Sent from:</b> &nbsp; $http_referrer by $name (" . "$email" . ")" .
    
    ... snip ...
    I've determined that the HTTP_REFERER variable is what's been causing the emails to not send. If I remove all instances of HTTP_REFERER, the email sends in a matter of seconds.

    Any ideas?

  11. #11
    Join Date
    May 2012
    Posts
    17
    If you comment out the sending stuff and just echo out $http_referrer what do you get? I'm guessing there's some special character in there which is causing this to fail.

    To prove this I guess you could do $http_referrer = htmlspecialchars(getenv( "HTTP_REFERER" ));

  12. #12
    Join Date
    Jan 2011
    Location
    United Kingdom, London
    Posts
    117
    does $_SERVER['HTTP_REFERER'] cause same problem?

  13. #13
    Join Date
    Apr 2006
    Posts
    120
    Quote Originally Posted by Alan01252 View Post
    If you comment out the sending stuff and just echo out $http_referrer what do you get?
    Nothing at all.

    Quote Originally Posted by Alan01252 View Post
    To prove this I guess you could do $http_referrer = htmlspecialchars(getenv( "HTTP_REFERER" ));
    Tried this too. Any mention of "HTTP_REFERER" is causing the email to not send.

  14. #14
    Join Date
    Apr 2006
    Posts
    120
    Quote Originally Posted by maurycy View Post
    does $_SERVER['HTTP_REFERER'] cause same problem?
    Yes

  15. #15
    Join Date
    Jan 2011
    Location
    United Kingdom, London
    Posts
    117
    It has to be server related issue, I've copied your code to my server and it worked, no change at all.

    BTW mail() is BOOL so you can use it like
    PHP Code:
    if (mail($mailto$subject$body$headers )){
        
    //success action
    }else{
        
    //fail


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles