Avatar of glepiza
glepiza
 asked on

why this php script is not working?

Hi Experts,

I've been digging around this script to send emails, but I really don't know why it is not working. Everything from my point of view looks correct. Well, after couple hours trying I decided it was time to ask an expert to help me to figure why is wrong with it.  My page reads php.

Thanks in advance.

<?php  
 
if(isset($_POST['submit'])) {  
  
 
    if(trim($_POST['fstname']) == '') {  
        $hasError = true;  
    } else {  
        $name = trim($_POST['fstname']);  
    }  
      
   if(trim($_POST['sport']) == '') {  
        $hasError = true;  
    } else {  
        $subject = trim($_POST['sport']);  
    }
	
    if(trim($_POST['subject']) == '') {  
        $hasError = true;  
    } else {  
        $subject = trim($_POST['subject']);  
    }  
      
 
    if(trim($_POST['email']) == '')  {  
        $hasError = true;  
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {  
        $hasError = true;  
    } else {  
        $email = trim($_POST['email']);  
    }  
      
 
    if(trim($_POST['message']) == '') {  
        $hasError = true;  
    } else {  
        if(function_exists('stripslashes')) {  
            $comments = stripslashes(trim($_POST['message']));  
        } else {  
            $comments = trim($_POST['message']);  
        }  
    }  
  
 
    if(!isset($hasError)) {  
        $to = 'xxx@email.com'; 
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";  
        $headers = 'From: My Site <'.$to.'>' . "\r\n" . 'Reply-To: ' . $email;  
  
        mail($to, $subject, $body, $headers);  
        $emailSent = true;  
      
}  
?>

Open in new window

PHPWeb Servers

Avatar of undefined
Last Comment
glepiza

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

I would start the script with:
$hasError = false;  

and change:
if(!isset($hasError))
into:
if(!$hasError)

glepiza

ASKER
Thanks for your answer angellll,

Still not working, I wonder if it was to do with how Im calling that script from my index.php

this is what I have at the top of the page before the html tag:
<?php
      require_once("includes/processing_contact_info.php");
?>

And this is what I have on my form:
      <form method="post" action="includes/processing_contact_info.php" id="contactform">  

Is that written fine? what do you think?

Thanks



Cornelia Yoder

When you say 'not working', what exactly is not working?   Do you simply not get the emails you try to send?  Have you checked that they are not going out, or is it possible they are being killed by a spam/junk mail filter?   Have you ensured that your server allows emails to be sent from scripts?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
shaunak

Possible Causes
1: $_POST['sport'] and $_POST['subject'] both assigned to $subject
------------------------------------------------
if(trim($_POST['sport']) == '') {  
        $hasError = true;  
    } else {  
        $subject = trim($_POST['sport']);  
    }
       
    if(trim($_POST['subject']) == '') {  
        $hasError = true;  
    } else {  
        $subject = trim($_POST['subject']);  
    }  

2: Use of Deprecated eregi function
---------------------------------------------------
eregi is deprecated in latest PHP stable release. Instead use
preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i"

note the syntax carefully preg_match ("/................................/ i ") i for case sensitivity.


3: Missing Header or incomplete headers
---------------------------------------------------
Add these extra and modified headers and try
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: My Site <".$to.">\r\n";
$headers .= "Reply-To: ".$name." <".$email.">\r\n";


4: Get the actual error
-----------------------------------------------------
Add this to get the error message if some goes wrong

$mail_sent = @mail($to, $subject, $body, $headers)
                              or die;
if ($mail_sent)
        $emailSent = true;  
------------------------------------------------------
glepiza

ASKER
Hello guys, thanks so much for taking your time answering to my question.

yodercm:
Yes, it is not working because Im not getting the emails Im trying to send. I have checked they are not going out and also checked the span folder etc, but nothing is in there. My serves does allow emails to be sent from scripts. thanks

shaunak:

Thanks so the corrections you sent me. I've not tried yet since I left my computer at home, but I will  try it this afternoon and I will let you know if it works.

Thanks everyone!!

glepiza
ASKER CERTIFIED SOLUTION
Cornelia Yoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
allen-davis

i would be careful with require_once if it is also in your form action.  Try require instead of require_once as you are debugging.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
glepiza

ASKER
Hi yoderm,

It seems you were right,  sent the script to a friend and it worked perfectly. So I called my hosting provider and they told me I needed to use one of their form scripts since I was under a windows environment. I really did not that it could be an issue of email system server.
Thanks everyone for taking the time to answer my question.

glepiza