Good Day,
I have a form that saves Name, Email, Subject, Tel Number, Message, and a checkbox.
I m sadly not so good in php at all. Im just started to learn the basics. Thats why i need a little help from you :)
The Form
<form class="contact-form" id="contact" role="form">
<!-- IF MAIL SENT SUCCESSFULLY -->
<h6 class="success">
<span class="olored-text icon_check"></span> Your Message has been send. </h6>
<!-- IF MAIL SENDING UNSUCCESSFULL -->
<h6 class="error">
<span class="colored-text icon_error-circle_alt"></span> Error </h6>
<div class="field-wrapper col-md-6">
<input class="form-control input-box" id="cf-name" type="text" name="cf-name" placeholder="Name*" required>
</div>
<div class="field-wrapper col-md-6">
<input class="form-control input-box" id="cf-email" type="email" name="cf-email" placeholder="E-Mail*" required>
</div>
<div class="field-wrapper col-md-6">
<input class="form-control input-box" id="cf-subject" type="text" name="cf-subject" placeholder="Subject*" required>
</div>
<div class="field-wrapper col-md-6">
<input class="form-control input-box" id="cf-number" type="tel" name="cf-number" placeholder="Number">
</div>
<div class="field-wrapper col-md-12">
<textarea class="form-control textarea-box" id="cf-message" rows="7" name="cf-message" placeholder="Message*" required></textarea>
</div>
<div class="form-check col-md-12">
<input type="checkbox" class="form-check-input" id="Checkdata" required>
<label class="form-check-label" for="Checkdata"> yes to this*</label>
</div>
<div class="col-md-12 ">
<p>*required</p>
</div>
<button class="btn standard-button" type="submit" id="cf-submit" name="submit" data-style="expand-left">Send</button>
</form>
Now i need this information: Name, Subject, Email, Number and if the checkbox was checked. I would like to include the Number and the checkbox info in the message, that will send as a email to me, because the Name, Subject and Email info will go in the email header.
I figured out how i could save all to the header, but im not really sure about how to include something in the message. Do i have to sent a HTML email for that? or can i do it without html?
There goes my php code
<?php
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['number']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
$headers = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
'Reply-To: ' . $_POST["email"] . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail( "hey#example.net", $_POST['subject'], $_POST['message'], $headers );
}
?>
Thank you all for your help.
Your question is not clear but assuming you want to append more text to the message?
You can do something like this:
$more_message = $_POST['name']. "hello, this is more \n";
mail( "hey#example.net", $_POST['subject'], $more_message.$_POST['message'], $headers );
Update based on comment below on additional requirements:
$header_1 = $_POST['subject'] ."\n" .$_POST['name'] ."\n";
$more_message = $_POST['message']. "\n". $_POST['number'] ."\n". $_POST['Checkdata'];
something like this...
Related
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.
When I try to click sent button, the content from the webpage is not redirecting to the .php page.I am using recaptcha in the form .Can you please help me to solve this issue..
my HTML code is:
<form action="sendform.php" id="contact-form" class="form-horizontal"
method="post">
<fieldset>
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Your Name</label>
<div class="col-sm-8">
<input type="text" placeholder="Your Name" class="form-control" name="name" id="name">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="email">Email Address</label>
<div class="col-sm-8">
<input type="text" placeholder="Enter Your Email Address" class="form-control" name="email" id="email">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="subject">Subject</label>
<div class="col-sm-8">
<input type="text" placeholder="Subject" class="form-control" name="subject" id="subject" list="exampleList">
<datalist id="exampleList" >
<option value="a">A</option>
<option value="b">B Combo</option>
</datalist>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="message">Your Message</label>
<div class="col-sm-8">
<textarea placeholder="Please Type Your Message" class="form-control" name="message" id="message" rows="3"></textarea>
</div>
</div>
<div class="col-sm-8" class="form-group" class="g-recaptcha" data-sitekey="xxxxxxyyyyyyy"></div>
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l">Submit</button>
<button type="reset" class="btn btn-primary">Cancel</button>
</div>
</fieldset>
</form>
And my PHP Code sendform.php
<?php
if (isset($_POST['submit']) && !empty($_POST['submit'])):
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'xxxxxxxxxxxxx';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if ($responseData->success):
$to = "aaa#abc.com"; // this is your Email address
$from = !empty($_POST['email']) ? $_POST['email'] : ''; // this is the sender's Email address
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$subject = !empty($_POST['subject']) ? $_POST['subject'] : '';
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From:" . $from;
$headers .= 'From:' . $name . ' <' . $from . '>' . "\r\n";
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
$succMsg = 'Your request have submitted successfully.';
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
I have tested your code and it works.
Please make sure that you have placed your html and php files in same directory and also your files should be served via a local running server.
So your url should look like this http://localhost/testing/index.html
Although, your sendform.php gives me captcha error ofcourse.
"Please click on the reCAPTCHA box."
I want to change my email headers on php mail send function , but i cant do this , i try use this into my php.ini
sendmail_path = /usr/sbin/sendmail -t -i -f'user#domain.com'
but its not working , i use plesk .
then i try to change my php script and add headers Parameter into my php code [ im new at php and using someone's script on internet ] but its not working , i means the mail not sending ..
php send mail script with out any change work correctly and send email
<?php
$emailTo = "user#domain.com";//change this to the email address which should receive the form data
/*
NOTE: do not change anything below this
*/
if( isset($_POST['maximus']) ) {
//honey pot detection
if( $_POST['maximus'] != '' ) {
die('Bad spam bot!!');
}
$message = "";
foreach( $_POST as $field => $val ) {
if( $field != 'maximus' ) {
$message .= $field . " : ". $val . "\n";
}
}
$subject = $_POST['theSubject'];
//send the email
if( mail($emailTo, $subject, $message) ) {
echo "Message sent!";
} else {
echo "Could not send message";
}
}
?>
but when i add headers parameter into my code the mail not sending
<?php
$emailTo = "user#domain.com";//change this to the email address which should receive the form data
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
/*
NOTE: do not change anything below this
*/
if( isset($_POST['maximus']) ) {
//honey pot detection
if( $_POST['maximus'] != '' ) {
die('Bad spam bot!!');
}
$message = "";
// Always set content-type when sending HTML email
foreach( $_POST as $field => $val ) {
if( $field != 'maximus' ) {
$message .= $field . " : ". $val . "\n";
}
}
$subject = $_POST['theSubject'];
//send the email
if( mail($emailTo, $subject, $message, $headers) ) {
echo "Message sent!";
} else {
echo "Could not send message";
}
}
?>
html form :
<form data-toggle="validator" method="post" action="form.php" id="quickQuoteForm" class="aSubmit">
<div style="display:none"><input type="text" name="maximus" value=""></div>
<input type="hidden" name="subject" value="Blue: Quick Quote Request">
<div class="contact-form-small">
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="form-group">
<input type="text" placeholder="First and Last Name *" aria-invalid="false" aria-required="true" size="40" value="" name="name" required>
</div>
<div class="form-group">
<input type="email" placeholder="E-mail address" aria-invalid="false" aria-required="true" size="40" value="" name="email" required>
</div>
<select aria-invalid="false" name="service">
<option value="Cargo">Cargo</option>
<option value="Ground Transport">Ground Transport</option>
<option value="Logistic Service">Logistic Service</option>
<option value="Storage">Storage</option>
<option value="Trucking Service">Trucking Service</option>
<option value="Warehousing">Warehousing</option>
</select>
</div>
<div class="col-xs-12 col-md-6">
<div class="form-group">
<input type="text" placeholder="Subject *" aria-invalid="false" aria-required="true" size="40" value="" name="subject" required>
</div>
<div class="form-group">
<textarea placeholder="Message *" aria-invalid="false" rows="10" cols="40" name="message" required></textarea>
</div>
</div>
<div class="col-xs-12 col-md-12">
<input type="submit" class="btn btn-primary pull-right" value="SEND MESSAGE">
<img class="ajax-loader" id="loader" src="images/ajax-loader.gif" alt="Sending ..." style="display:none;">
</div>
</div><!-- /.row -->
<div class="response success">Your message was sent; we'll be in touch shortly!</div>
<div class="response error">Unfortunately, we could not sent your message right now. Please try again.</div>
</div><!-- /.contact-form-small -->
</form>
i see this part of code NOTE: do not change anything below this but i really need to change my headers name ..
I am able to receive email with all the information except for the email address that is entered in the form, and when I receive the email, I get "No Sender" instead of the person's name or email.
This is my HTML:
<!-- Contact Form -->
<div class="col-xs-12 col-sm-8 col-md-8">
<div id="contant-form-bx" class="contant-form-bx">
<div class="contact-loader"></div>
<form action="mail.php" id="contact-form" class="contact-form" name="cform" method="post">
<div class="row">
<div class="col-md-6">
<label for="name" id="name_label">Name</label>
<span class="name-missing">Please enter your name!</span>
<input id="name" type="text" value="" name="name" size="30">
</div>
<div class="col-md-6 columns">
<label for="e-mail" id="email_label">Email</label>
<span class="email-missing">Please enter a valid e-mail!</span>
<input id="e-mail" type="text" value="" name="email" size="30">
</div>
<div class="col-md-12">
<label for="message" id="phone_label">Message</label>
<span class="message-missing">Say something!</span>
<textarea id="message" name="message" rows="7" cols="40"></textarea>
</div>
<div class="col-md-12 text-center">
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message">
</div>
</div>
</form>
</div>
</div>
This is my PHP:
<?php
// declare our variables
$name = $_POST['name'];
$email = $_POST['e-mail'];
$message = nl2br($_POST['message']);
// set a title for the message
$subject = "From $name via WEB";
$body = "From $name, \n\n$message";
$headers = 'From: '.$email.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// put your email address here
mail("info#wziel.com", $subject, $body, $headers);
?>
<!--Display a Thank you message in the callback -->
<div class="mail_response">
<h4>Thank you <?php echo $name ?>!</h4>
<p>I'll be in touch real soon!</p>
</div>
According to your form this:
$email = $_POST['e-mail'];
Should be this:
$email = $_POST['email'];
You have e-mail as the id not the name.
The php looks at name and not id
You're using $_POST['e-mail']. e-mail is the ID of the <input> tag. You should be using $_POST['email'] because name="email".
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I can't seem to get the form to send a message to my email. Is it maybe due to the server? Here's my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form id="contact-form" action:"" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="hidden" name="action" value="submit">
<label for="name">
Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" placeholder="Enter email" required /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="General">General</option>
<option value="Hiring">Hiring</option>
<option value="My Work">My Work</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="message">
Message</label>
<textarea name="message" id="message" class="form-control" rows="9" cols="25" required
placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
Send Message</button>
</div>
</div>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$subject=$_REQUEST['subject'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($subject=="")||($message==""))
{
echo "All fields are required.";
}
else{
$from="From: $name, $email";
$subject="Message sent using your contact form";
mail("nilsbittmannmyers#yahoo.co.uk", $subject, $message, $from);
echo "Message sent!";
}
}
?>
THANKS :)
Im using byet as a web host by the way, I think they have PHP enabled in the free service, excuse me, I'm a bit of a noob when it comes to coding, to say the least XD XD
You're using a colon action:"" instead of an equal sign which should be action="" which is the main issue in your code.
You're also missing a name attribute for the email and name input fields.
I.e.: name="email" and name="name"
Also, the From: should initially be an email, and not a name as you have in
$from="From: $name, $email";
It's best to use something like this:
$from = "From: ". $name . " <" . $email . ">\r\n";
as your header's 4th parameter.
That way you will have a proper From Email with the person's name appearing in the Email also.
Using $from="From: $name, $email"; will most likely end up in Spam or rejected altogether.
Consult the PHP.net Website for details on mail() and headers:
http://php.net/manual/en/function.mail.php
a few headers options:
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
in your case would be:
$from = "From: ". $name . " <" . $email . ">\r\n";
$from .= 'Reply-To: ' . $email . "\r\n";
$from .= 'Return-Path: ' . $email . "\r\n";
Sidenote:
I noticed you are using enctype="multipart/form-data"
Unless you're wanting to attach/upload a file, it isn't required for what you're using your form as, it's safe to remove it.
Footnotes:
Using error reporting would have signaled these errors, including Undefined index... warnings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
placed just beneath your opening <?php tag.