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.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I had my contact form up and running and I was doing a bit of housekeeping with how it actually looked when it arrived in my inbox. I dunno what I changed but even after reverting it back to when I know it was working it no longer sends emails but it 100% was working at some point!
Maybe its an issue with the host or the server but I've no clue.
Here is the contact form php.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$emailFrom = 'myemail#gmail.com';
$emailSubject = 'New Form Submission';
$emailBody = "Name: $name. \n".
"Email: $email. \n".
"Phone: $phone 'n".
"Message: $message.";
$to = 'myemail#gmail.com';
$headers = "From: $emailFrom \r\n";
$headers .= "Reply to $email \r\n";
mail($to,$emailSubject,$emailBody,$headers);
header("Location: index.php")
?>
And here is the forms html
<div id="contact" class="container contact-form">
<div id="contactAnchor"></div><!--CONTACT ANCHOR TAG-->
<div class="contact-image">
<img id="contactLogo" src="/img/sam avatar no bg.png" width="150px" alt="SDB logo"/>
</div>
<form method="post" action="contactform.php">
<h3>Drop Me a Message</h3>
<div class="row">
<div class="col">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your Name *"/>
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" placeholder="Your Email *"/>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Your Phone Number"/>
</div>
</div>
<div class="col">
<div class="form-group">
<textarea name="message" class="form-control" placeholder="Your Message *" style="width: 100%; height: 150px;"></textarea>
</div>
<div class="form-group">
<input type="submit" name="btnSubmit" class="btn btn-success" value="Send Message" />
</div>
</div>
</div>
</form>
</div>
Not sure if this solves it, but your reply to header is incorrect:
$headers .= "Reply-To: $email \r\n";
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to be able to collect all the data from the form of my site that is already live and send it to my email.
I based myself in this question here and adjusted my php based on that. However, when I click on submit button, the fields get empty but when I check my email I receive nothing.
How can I collect the data from the site and send to my email?
My code looks like this:
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option value="Unemployed">Unemployed</option>
<option value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'sidney#web2web.co.za';
$to = 'sidney#web2web.co.za';
$subject = '$subject';
$body ="From: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
if (mail($to,$subject,$body,$headers)) {
$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>';
}
}
?>
</form>
PHP requires an installed and running mail system. The program to be used is defined by the configuration settings in the php.ini file.
First you need change your PHP mail configuration:
Open your php.ini file.
Search [mail function].
Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
Save the php.ini file.
Restart your server.
You can use PHPMailer For mail
this is probably the world's most popular code for sending email from PHP!
Your Code is perfect. i checked it after some small changes, Like you put $subject in single quotation marks which is wrong , and you also forgot to print the output which is $result . other wise your code is perfect.
Run this code, if it shows error then please enable SEND MAIL option to your server.
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option value="Unemployed">Unemployed</option>
<option value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'sidney#web2web.co.za';
$to = 'sidney#web2web.co.za';
$subject = $subject;
$body ="From: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
if (mail($to,$subject,$body,$headers)) {
$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>';
}
echo $result;
}
?>
I'm currently working through setting up a fully functional contact form using the Bootstrap framework and PHP. I'm receiving test emails from the form but it's not displaying the content the way I understood it would and I'm having some other issues.
First, the user-submitted email is showing up as being received from the host of my site. It is also not including the name of the person, which I thought I included in my PHP
Here is my code for contact.php and the form in my index.html file
<?php
$to = 'example#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject=$_POST['subject'];
$message = $_POST['msg'];
if ($_POST){
mail($to, $subject, $message);
$confirm= "Your email was sent successfully!";
echo "<script type='text/javascript'>confirm('$confirm');</script>";
}
else {
$error_msg= "All fields are required!";
echo "<script type='text/javascript'>alert('$error_msg');</script>";
}
?>
index.html
<form class="form-horizontal" role="form" action="contact.php" method="post">
<div class="modal-header">
<h2>Contact</h2>
</div>
<div class="modal-body">
<!--name-->
<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" name="name" id="name" placeholder="First and Last Name">
</div>
</div>
<!--email-->
<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" name="email" id="email" placeholder="example#somewhere.com">
</div>
</div>
<!--subject-->
<div class="form-group">
<label for="subject" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Enter subject here...">
</div>
</div>
<!--message-->
<div class="form-group">
<label for="msg" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="msg" id="msg" placeholder="Enter message here..."></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
Another unintended issue is that once the page is submitted, a blank page (contact.php) is loaded. I thought, looking at my code, a javascript alert or confirm box would pop up instead though? I'm new at this so, I apologize if these are dumb questions.
Thanks
-K
First of, you don't have from defined in your header. You need
$headers = "From: sender_email#your_domain.com\r\n";
mail($to,$subject,$message,$headers);
You can also modify header to send html message which is desired in most cases in addition to setting from and cc: carbon copy or bcc: blind carbon copy email addresses.
// Always set content-type when sending HTML email
$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";
$headers .= 'Cc: myboss#example.com' . "\r\n";
Get this code incorporated in your if statement. I also noticed you have
else {
$error_msg= "All fields are required!";
Even if there was only one field passed in as $_POST method, the else statement isn't checking if all fields were passed in. You need to break that down or check the fields individually or do it on with client side JavaScript field validation.
What is the confirm javascript function? can you post it on here? if you're only looking for an alert, you can straight insert alert function in between script tags.
I'm trying to make a simple submission form in my index.html file that will send an email with the values, but they are always blank after grabbing them with $_POST. Here is my form:
<form id="contact-form" action="/rtp/php/submit.php" method="POST" enctype="text/plain">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">
Name</label>
<input type="text" name="theName" class="form-control" id="theName" placeholder="Enter name" required="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" name="theEmail" class="form-control" id="theEmail" placeholder="Enter email" required="required" /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="theSubject" id="theSubject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="service">Criminal Defense</option>
<option value="service">Personal Injury</option>
<option value="service">Consitutional Law</option>
<option value="service">Immigration</option>
<option value="suggestion">General Inquiry</option>
<option value="suggestion">Scholarship</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="name">
Message</label>
<textarea name="message" name="theMessage" id="theMessage" class="form-control" rows="9" cols="25" required="required"
placeholder="What can we help you with?"></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>
And here is my submission script:
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
// Set destination
$email_to = "bali*****l.com";
$email_from = "br******offee";
$email_subject = "Rai*******rship";
// Grab fields
$name = isset($_POST["theName"]) ? $_POST['theName'] : 'Name Not set';
$email = isset($_POST['theEmail']) ? $_POST['theEmail'] : 'Email not set';
$subject = isset($_POST['theSubject']) ? $_POST['theSubject'] : 'subject not set';
$message = isset($_POST['theMessage']) ? $_POST['theMessage'] : 'message not set:(';
$email_message = "Form details below.\n\n";
// Clean up
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// Set string
$email_message .= $name."\n";
$email_message .= $email."\n";
$email_message .= $subject."\n";
$email_message .= $message."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
print("The message -> $email_message");
?>
Congratulations, you are entered to win! You should hear from us shortly :)
All variables end up blank. What am I doing wrong? Any help is appreciated, I've been trying to figure this out for hours. I do not understand why the POST function is not grabbing the values and passing them through.
remove enctype="text/plain" from form tag and check.
I agree that you can just remove the enctype tag altogether -- I've never needed to include that on a form sent via Post.
As an additional aside, though:
Do you mean that the variables are blank if the web form is left blank?
When a form is submitted via POST and a field is empty, it is sent as an empty string.
So, for example, if you left the entire form blank then $_POST['theName'] would be sent as "".
ISSET will return true on a blank string -- so, all of your ISSET tests are returning true.
Try !empty instead. For example:
$name = !empty($_POST['theName']) ? $_POST['theName'] : 'No Name Set';
If it is returning blanks even when something is entered on the form please clarify.
Also, in your <select><option> tags, POST is going to send the value of whatever is selected. You currently have multiple Options with the same value. It does NOT send what is between the <option> tag (in other words, whether they choose "Criminal Defense" or "Personal Injury" $_POST['theSubject'] will be "service" for both.
Again - I've looked and troubleshooted for about an hour and cannot get the php contact form to work. I get the "Thank you!" email, but I haven't received the email to my inbox ever. I've done it about 20 times.
<div class="container">
<div class="row">
<div class="col-lg-12">
<h5>Want to contact me quickly? Use this form!</h5>
<form id="contact" method="post" action="submit.php" class="form" role="form">
<div class="row">
<div class="form-group col-lg-4">
<label>Name</label></br>
<input type="text" name="name" placeholder="Enter your full name" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Email address</label></br>
<input type="email" name="email" placeholder="Enter your email address" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Subject</label></br>
<input type="text" name="subject" placeholder="Subject">
</div>
<div class="form-group col-lg-12">
<label>Message</label>
<textarea name="comments" name="message" data-provide="markdown-editable" rows="6" placeholder="Let's chat!" style="width:100%"></textarea>
</div>
<div class="form-group col-lg-12 form-action">
<input type="hidden" name="Submit" value="contact">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
PHP:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='MY EMAIL ADDRESS';
$from='website';
if (mail($to, $subject, $name, $email, $comments))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}?>
Any help is greatly appreciated. I have my MAMP server on and I get the "thank you!" message every time, but I haven't gotten any emails. Also, I'd really like it to show the "thank you!" and then return to my webpage, but I can't even get the php to work so I maybe should hold off on that part.
You have a wrong call of mail() function.
It should be like this:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So, for your case:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='email#email.com';
$from='website';
$message = 'Message: ' . $comments . "\r\n";
$message .= 'From: ' . $from;
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}
?>
The mail() function simply returns true if the message has been queued without error in your system... but it doesn't tell you whether the email was actually sent or not. I see you're using Mac OS X, so you'll have to configure SMTP settings (provided by your ISP) in the PHP environment to get mail sending working.
Alternately, you can use a service like Amazon SES to send emails through PHP.