Click here to Skip to main content
15,891,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to create a contact form so that users can send emails directly from the website to my email address. The problem iam having is that it doesn't work no matter what script or guide i used.

I hope someone can take a look and tell me what iam doing wrong here.

What I have tried:

Now i cleaned my code so that it is easier to see if something is wrong so i don't have anything to validate the input text.

what i currently have are 3 files in my server which are contact.php, contact.html and app.js and i created an email account on the same server because it is part of the package when i hosted it.

My html code:
HTML
<div class=" container formcontainer">
        
      <form method="post">
            
            <input type="text" name="subject" class="form-control" placeholder="Subject..." ng-model="formData.subject">
            
            <input type="email" name="email" class="form-control" placeholder="Your email..." ng-model="formData.email">
         
            <textarea  rows="5" cols="50" class="form-control" placeholder="You message" ng-model="formData.message"></textarea>
           
            <button type="submit" ng-click="processForm()" class="btn btn-success btn-lg btn-block">Submit!</button>
      </form>
   </div>


My controller:
JavaScript
app.controller("contactController", ['$scope', '$http', function ($scope, $http) {


        $scope.formData = {

            subject: '',
            email: '',
            message: ''
        };
        console.log(formData);
        // process the form
        $scope.processForm = function () {
            $http({
                method: 'POST',
                url: 'contact.php',
                data: $scope.formData,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            })


        };


    } ]);


My php script:
PHP
<?php


$post_data = file_get_contents("php://input");
$data = json_decode($post_data);



//email information
$to = "lurtze@hotmail.com";

$subject = $data['subject'];
$userEmail=$data['email'];
$message=$data['message'];



$headers = 'From: ' . $userEmail . "\r\n" .
        'Reply-To:'.$userEmail . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

//php mail function to send email on your email address
mail($to, $subject, $message, $headers);

//Email response
  echo "Thank you for contacting us!";
  }

?>
Posted
Updated 4-Jun-18 3:39am
Comments
Richard MacCutchan 4-Jun-18 3:59am    
Which SMTP host are you using?
Member 13857265 4-Jun-18 4:52am    
Well do you mean the SMTP server? because i thought that if i use a php script then i don't use an SMTP, the server is send.one.com
Richard MacCutchan 4-Jun-18 5:04am    
You cannot send mail without an SMTP server to handle the transport. It should be configured in your php.ini file.
Member 13857265 4-Jun-18 5:27am    
Yeah but that is the weird thing, none of guides i used had mentioned anything about this, the question is when you say php.ini do you mean my php script or something else and how should i do it exactly? because i did try SMTP approach only and it didn't work either.
Richard MacCutchan 4-Jun-18 5:43am    
as I said, you cannot send mail without access to a SMTP server. And the mail system needs to know the address of that server. See PHP 5 Mail Functions[^] for details.

1 solution

As already mentioned in the comments you have to setup PHP (see PHP: Runtime Configuration - Manual[^]) for using PHP: mail - Manual[^].

But the mail() function is a relict from old times and sending mails requires nowadays secure connections and authentication. The solution is to use one of the PHP mail frameworks like GitHub - PHPMailer/PHPMailer: The classic email sending library for PHP[^] or Powerful component based mailing library for PHP – Swift Mailer[^].

In any case you need to specify an SMTP server (address, port, and protocol) and the required credentials (user name and password of a mail account known by that server).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900