Micah
Micah

Reputation: 23

How do I send email with multiple form fields in php

I want to send email from the contact form on my site that has multiple form fields. But I can't seem to get that right. Here is my form HTML code:

  <form class="form-horizontal" action="mail.php" method="post">
  <h2>Quick Contact</h2>
    <div class="form-group">
      <label class="control-label col-sm-2" for="email">Full Name:</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" id="fname" placeholder="Enter Full Name" name="fname">
      </div>
    </div><br>
    <div class="form-group">
      <label class="control-label col-sm-2" for="num">ID Number:</label>
      <div class="col-sm-6">
        <input type="text" class="form-control" id="num" placeholder="Enter ID Number" name="id">
      </div>
    </div><br>
    <div class="form-group">
      <label class="control-label col-sm-2" for="email">Email:</label>
      <div class="col-sm-6">
        <input type="email" class="form-control" id="email" placeholder="Enter Email Address" name="email">
      </div>
    </div><br>
    <div class="form-group">
      <label class="control-label col-sm-2" for="sal">Monthly Salary:</label>
      <div class="col-sm-6">          
        <input type="number" class="form-control" id="sal" placeholder="Enter Salary" name="salary">
        <div class="label label-warning" >Numbers only. Any other characters are not permitted.</div>
      </div>
      
    </div><br>
    <div class="form-group">        
      <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-danger">Submit</button>
      </div>
    </div>
  </form>

This is my PHP code, I am using PHP mail() to send the email:

<?php

$to = '[email protected]';
$subject = 'Loan Request';
    $from = $_POST['email']; // required
    $name = $_POST['fname']; // required
    $id = $_POST['id']; // required
    $telephone = $_POST['mobile']; // not required
    $salary = $_POST['salary']; // required

$message = $name." ".$telephone." ".$salary;

$headers = 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" ;

// Sending email
if(mail($to, $subject, $from, $message)){
header('Location: http://example.com');
} else{
    echo 'Unable to send email. Please try again.';
}

?>

The mail delivers quite alright, but does not display the entire form details when it enters my email. I will like to know how I can capture all the form fields cos I intend to add more form fields. It's a really long form.

Upvotes: 1

Views: 301

Answers (2)

Shubham Narvekar
Shubham Narvekar

Reputation: 77

First ensure that your localhost/server is configured to send email. Use the following function to send email (works for me):

public function email($to, $cc, $from, $subject, $message) {
    $config['protocol']='mail';
    $config['smtp_host']='ssl://smtp.gmail.com';
    $config['smtp_port']='465';
    $config['smtp_timeout']='30';
    $config['smtp_user']='[email protected]';
    $config['smtp_pass']='urPassword';
    $config['charset']='utf-8';
    $config['newline']="\r\n";
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $this->load->library('email',$config);
    // $this->email->initialize($config);
    $this->email->from($from, 'Limecar.in');
    $this->email->to($to);
    $this->email->cc($cc);
    $this->email->subject($subject);
    $this->email->message($message);
    return $this->email->send();
}

Upvotes: 0

suspectus
suspectus

Reputation: 17258

If you are using PHP's mail function then the parameters should be in this order. In your example the $headers parameter was not passed at all.

if (mail($to, $subject, $message, $headers)) {
    header('Location: http://example.com');
} else {
    echo 'Unable to send email. Please try again.';
}

Upvotes: 1

Related Questions