send Mail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterj
Forum Newbie
Posts: 10
Joined: Fri Dec 05, 2008 5:19 pm

send Mail

Post by peterj »

Hi there,

I have a form that updates a database (inputs a new row with the form inputs).
I also want an email to be sent out when the form is submitted. I understand that the form can only have 1 action so I have done the following.

When the form is submitted, it runs the following function to update the database:

Code: Select all

public function productInsert(){
 
            if(isset($_GET['insert'])){
            include('classes/Security.class.php');
            $security = new Security();
            $security->escapeInput();
 
            //catch the vars
            $name = $_POST['name'];
            $email = $_POST['email'];
            $number = $_POST['number'];
            $idnum = $_POST['idnum'];
            $mincont = $_POST['mincont'];
            $govcont = $_POST['govcont'];
 
            //insert new data into db
            $query = "INSERT INTO products (name, email, number, idnum, mincont, govcont, code, price) VALUES ('$name', '$email', '$number', '$idnum', '$mincont', '$govcont', '$code', '$price')";
 
            $result = mysql_query($query);
            if (!$result) {
                echo "Could not insert values into the database. ". mysql_error();
            } else {
                $this->sendMail();
            }
        }
 
 
    }//productInsert
As you can see, the above code then runs the sendMail function below:

Code: Select all

public function sendMail(){
 
                    $recipient = "peterjunsworth@gmail.com";
                    $name = $_POST['name'];
                    $email = $_POST['email'];
                    $number = $_POST['number'];
                    $idnum = $_POST['idnum'];
                    $subject = "Assessment results";
 
                    $body = "
 
                    You have recieved Kiwisaver Assessment Results. \n\n
 
                    Personal Details: \n
                    Name: \t $name \n
                    Email:\t $email \n
                    Phone:\t $number \n
 
                ";
 
 
 
                    $headers = "From: $email \n";
 
                    $mailSent = mail($recipient, $subject, $body, $headers);
 
 
                    if (!$mailSent) {
                        header("Location:index.php?id=".$productID."&status= There was an issue sending your results. Sorry");
                    } else {
                        header("Location:index.php?status=Your assessment has been submitted to Huljich Wealth Management");
}
}
The problem is, I am getting 'There was an issue sending your results. Sorry' when I submit the form.
The database is getting updated which is great but I need the email to be sent.
The above code is part of a php class, can i have a $mailSent script within a class? or is there something else wring with my code?

Can anyone help?

Thanks
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: send Mail

Post by jaoudestudios »

Do you get any errors?

What server are you testing this on? Because there might not be mail server installed - if it is windows (WAMP) then most probably not.
Post Reply