How do I send email with multiple form fields in php - 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 = 'mail#gmail.com';
$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.

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.';
}

first ensure ur 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']='urEmail#gmail.com';
$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);
if ($this->email->send()) {return true;}
else {return false;}
}

Related

Some issue in sending email using php email function

I am trying to send an email from a enquiry form available on a catalogue website on which I am working and found a strange issue.
On the same domain I have a file to test the email function with following code in it:
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
and its working fine. But the same code is not working on the index.php page where I have the enquiry form and also not showing any error at all (I have checked the error log. I have also tried by putting this code in a separate file and by including it on index.php but the same result).
<form name="frm_enquiry" id="frm_enquiry" method="post" autocomplete="off">
<div class="row">
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="fullname">Name:</label>
<input type="text" class="form-control" id="fullname" name="fullname" placeholder="Enter your name">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label for="mobile">Mobile No:</label>
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Enter your mobile number">
</div>
</div>
<div class="col-lg-3 col-md-3">
<div class="form-group">
<label style="width: 100%;" for="mobile"> </label>
<button type="submit" class="btn btn-block btn-primary" name="submit">Submit</button>
</div>
</div>
</div>
</form>
php:
if( isset( $_REQUEST['submit'] ) )
{
$name = $_REQUEST['fullname'];
$email = $_REQUEST['email'];
$mobile = $_REQUEST['mobile'];
echo $name . ' : ' . $email . ' : ' . $mobile . '<br>';
$sender = 'someone#somedomain.tld';
$recipient = 'name#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
}
Someone please guide me, what I am doing wrong here, I am stuck on this from last 2 days.
Thanks in advance.

I am receiving empty emails on my website

I am building a html5 based contact form with php mail script.
I do not know why i am getting empty emails on submission as well as If i want to print any message like "Thank you for contacting us", How would I do that.
<?php
$name = (isset($_POST['name']) ? $_POST['name'] : null);
$phone = (isset($_POST['phone']) ? $_POST['phone'] : null);
$address = (isset($_POST['address']) ? $_POST['address'] : null);
$appointment_datepicker =(isset($_POST['appointment-datepicker']) ? $_POST['appointment-datepicker'] : null);
$message = (isset($_POST['textarea']) ? $_POST['textarea'] : null);
$email_address = (isset($_POST['email']) ? $_POST['email'] : null);
// Create the email and send the message
$to = 'dipen2512#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nPhone: $phone\n\nEmail: $email_address\n\nAppointment: $appointment_datepicker\n\nAddress: $address\n\nMessage:\n$message";
$headers = "From: noreply#dev-designers.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
$mail_status = mail($to,$email_subject,$email_body,$headers);
/** Check for empty fields
if(!empty($_POST['name']) || !empty($_POST['phone']) || !empty($_POST['address']) || !empty($_POST['appointment-datepicker']) || !empty($_POST['textarea']) || filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{**/
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
print( "Thank you for the message. We will contact you shortly.");
window.location = 'http://thankyou.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
print( 'Message failed. Please, send an email to gordon#template-help.com');
window.location = 'http://thankyou.html';
</script>
<?php
}
?>
<form id="appointment-form" action="contact_me.php" method="post" class="appointment-form">
<div class="col-md-6">
<div class="form-group form-md-line-input form-md-floating-label">
<input id="name" type="text" class="form-control" required="required">
<label for="name">Name</label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<input id="phone" type="text" class="form-control" required="required" pattern="/[1-9][01][0-9]-?[0-9]{3}-=[0-9]{4}">
<label for="phone">Phone Number</label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<input id="address" type="text" class="form-control" required="required">
<label for="address">Address </label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<input id="appointment-datepicker" type="text" class="form-control form-line-input" required="required">
<label for="appointment-datepicker">Book a date</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-md-line-input form-md-floating-label">
<input id="email" type="text" class="form-control" required="required" pattern="[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~.-]+#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*">
<label for="email">Email Address</label>
</div>
<div class="form-group form-md-line-input form-md-floating-label">
<textarea id="textarea" rows="4" class="form-control form-textarea"></textarea>
<label for="textarea">Message</label>
</div>
<div class="btn-wrapper">
<button type="submit" class="btn btn-make-app">Send to us</button>
</div>
</div>
<div class="clearfix"> </div>
</form>
Your form inputs are missing "name" attributes. IDs don't get passed along in a form request; only the name-value pairings.
v---------v
<input id="name" name="name" type="text" class="form-control" required="required">

Bootstrap PHP Form Not Sending

I am trying to create a very simple contact form for my personal website. This form worked perfect for another website I did so I'm not sure what the problem is here. I've spent the last few hours trying to figure it out and dipping into other form tutorials so its strayed from the original code. I guess the biggest difference between this and the other website is that this is in bootstrap.
I have three problems with this.
1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.
2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.
3) The biggest problem is that while the form runs, I have not received any emails from the form.
The php before html on same doc:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 't*************#gmail.com';
$subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'],
FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in
touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error
sending your message. Please try again later.</div>';
}
}
}
?>
The html/form:
<form class="form-horizontal" role="form" method="post"
action="pauline.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name"
placeholder="First & Last Name" value="<?php echo
htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email"
name="email" placeholder="example#domain.com" value="<?php echo
htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-
label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject"
name="subject" placeholder="Message Subject" value="<?php echo
htmlspecialchars($_POST['subject']); ?>">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-
label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php
echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human"
name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send"
class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
I have your issues resolved with example code. See code comments for exact edits
This is just a matter of setting an anchor to jump the back page
down on load
Just set the subject to a blank, and not
have any html for the subject
This was fixed on its own by cleaning up your undefined errors. Note
the issets() I used with shorthand if statements $if?$then:$else to define the variables as blank if there is nothing coming
from $_POST
I recommend you take care of your format needs on top to keep your code as organized as you can. Also use the variables you defined as opposed to calling $_POST repeatedly like your example.
<?
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ""; ### shorthand if statements
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : "";
$message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : "";
$human = isset($_POST['human']) ? intval($_POST['human']) : -1;
$from = 'Contact Form';
$to = 't*************#gmail.com';
$subject = ""; ### send a blank subject. issue 2 solved
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$errName = $errEmail = $errMessage = $errHuman = $result = ""; ### defining these all as blank to stop the undefined error.
if (isset($_POST["submit"])) {
// Check if name has been entered
if (!$name) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$message) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $from)) {
$result = '<div class="alert alert-success">Thank You! I will be in
touch</div>';
}
else {
$result = '<div class="alert alert-danger">Sorry there was an error
sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form class="form-horizontal" role="form" method="post"
action="pauline.php#submit"> <!-- // this solves issue 1 -->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name"
placeholder="First & Last Name" value="<?php echo $name; ?>">
<?php echo "<p class='text-danger'>$errName</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email"
name="email" placeholder="example#domain.com" value="<?php echo $email; ?>">
<?php echo "<p class='text-danger'>$errEmail</p>"; ?>
</div>
</div>
<!--// BYE BYE SUBJECT!
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject"
name="subject" placeholder="Message Subject" value="<?php # echo htmlspecialchars($_POST['subject']); ?>">
<?php # echo "<p class='text-danger'>$errSubject</p>"; ?>
</div>
</div>
-->
<div class="form-group">
<label for="message" class="col-sm-2 control-
label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message">
<?php echo $message; ?>
</textarea>
<?php echo "<p class='text-danger'>$errMessage</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?
</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human"
name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>"; ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send"
class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</body>
</html>
1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.
Because you're posting to the page and not providing a redirect, you'll always refresh the page, and you'll always reload the page at the top first. You could provide a Javascript function to scroll to a certain point, or redirect to an anchor tag on the page (though this would involve an unnecessary reload the way you're running it).
2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.
Sure! You can hardcode a subject into your PHP. Just replace your $subject = $_POST['subject']; with $subject = 'Message From Site'. Then remove the input from your form. You've already done this with your $from variable.
3) The biggest problem is that while the form runs, I have not received any emails from the form.
This is a more difficult issue. Emails from PHP scripts can be difficult to get through email provider spam filters. You may need to set some configuration or set specific headers in order to get emails sent to you.The mail() function accepts a set of headers as the fourth parameter, not just a "From" value - tweaking that may help. You can find TONS more info on debugging options aon the PHP docs, located here: http://php.net/manual/en/function.mail.php

FILTER_VALIDATE_EMAIL saying a valid email is invalid

I have a problem with checking if a email is valid. but the weird is that i have the same form on to different pages/urls, and on one of the forms it keeps saying that the email is invalid and on the form its valid.
The form on this page works - http://night.sendme.to/about
the form on this page doesnt - http://night.sendme.to/book/jokeren
The HTML on the forms is the same
<form action="" method="post" id="myform">
<div class="form-group">
<label for="name">Navn *</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Navn" required="required">
</div>
<div class="form-group">
<label for="corp">Virksomhed</label>
<input type="text" class="form-control" id="corp" name="corp" placeholder="Virksomhed">
</div>
<div class="form-group">
<label for="email">Email adresse *</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email adresse" required="required">
</div>
<div class="form-group">
<label for="tel">Telefon *</label>
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefon" required="required">
</div>
<div class="form-group">
<label for="message">Kommentar</label>
<textarea class="form-control" id="message" name="message" rows="10" required="required"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit">Send</button>
</form>
<div id="success" style="color:red;"></div>
The PHP is this
<?php // Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOURMAIL';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:".$_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $header); //This method sends the mail.
echo "Your email was sent!";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
} else {
echo "Invalid Email, please provide an correct email.";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
The javascript is this
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", $("#myform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
Hope some one can help, why the form only works on the http://night.sendme.to/about and the others
So, to not leave this question answer-less:
In your HTML code, you actually had <form action="" method="post" id="myform"> in both pages – but in your second page, you had another <form> tag right before it … and because of this invalid HTML, the browser ignored the second form tag, and that made $("#myform").serialize() not return any data at all, because it could not find the form element with that id.
You should always validate your HTML code. This helps avoiding such errors.

Trouble with PHP sending emailing from contact form

I have a site up and running and cant seem to get the php to send the form data. Get thank you message sometimes but never appears in mail. my aim is to have the form collect the data then email the data to the client.
Thanks for any help. I am struggling to get to grips with php it would appear.
<?php
$to = "********#*******.com";
$subject = "Contact Us";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['enquiry'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print "Your mail was sent successfully";}
else
{print "We encountered anerror sending your mail";}
?>
<section class="container">
<section class="col-lg-6 col-lg-offset-1">
<form class="form-horizontal text-center" role="form" method="post" action="form_send.php">
<div class="form-group">
<label for="Name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-6">
<input type="name" class="form-control" id="name" name="name" placeholder="Enter Name...">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-6">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email...">
</div>
</div>
<div class="form-group">
<label for="Phone" class="col-lg-2 control-label">Phone:</label>
<div class="col-lg-6">
<input type="phone" class="form-control" id="phone" placeholder="Enter Phone Number...">
</div>
</div>
<div class="form-group">
<label for="Enquiry" class="col-lg-2 control-label">Enquiry:</label>
<div class="col-lg-6">
<textarea type="enquiry" class="form-control" rows="5" id="enquiry" name="enquiry" placeholder="Enter Email..."></textarea>
</div>
</div>
<div class="form-group">
<label for="Enquiry" class="col-lg-2 control-label"></label>
<div class="col-lg-6">
<button class="btn btn-primary" type="submit">Send Message</button>
</div>
</div>
</form>
Reply from email server:
Technical details of permanent failure: Google tried to deliver your message, but it was rejected by the server for the recipient domain peoplespropertyshop.co.uk by aspmx.l.google.com. [2607:f8b0:4003:c02::1a]. The error that the other server returned was: 550-5.1.1 The email account that you tried to reach does not exist. Please try 550-5.1.1 double-checking the recipient's email address for typos or 550-5.1.1 unnecessary spaces. Learn more at 550 5.1.1 support.google.com/mail/bin/answer.py?answer=6596 t6si822722oei.44 - gsmtp
First you should check that your code is in one file (form_send.php). After that add some check if user have submitted the form:
if (!empty($_POST))
Message is sent to $to email. If you want to change that, pass $email paramter to mail() function:
$sent = mail($to, $subject, $message, $headers) ;
to:
$sent = mail($email, $subject, $message, $headers);
Also check your mail server configuration and add some validation.
$email = htmlentities($_REQUEST['email']);
$message = htmlentities($_REQUEST['enquiry']);

Categories