This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
am trying to use PHP to send mails from a form using one page submission but i seem to be missing something since no mail is sent on submission neither does it throw up an error (which is rather weird). Below is the PHP and HTML form codes which are on the same file
PHP Script
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: risingventures.com.ng';
$to = 'inquiry#risingventures.com.ng';
$subject = 'New message from contact form';
$body = "From: $name\n E-mail: $email\n Phone: $phone\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: from#example.com' . "\r\n" .
'Reply-To: reply#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $body, $headers)) {
$result = '<div class="alert alert-success">Thank You! We will be in touch with you soon</div>';
} else {
$result = '<div class="alert alert-danger">Sorry! There was an error submitting your message. Please try again</div>';
}
}
?>
HTML code
<form action="contact.php" method="POST" role="form">
<legend>Contact Us</legend>
<div class="form-group">
<div class="col-lg-12" style="margin-bottom: 20px">
<label>Name *</label>
<input type="text" class="form-control" name="name" placeholder="Please type your Firstname followed by your Lastname">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-6">
<label>Email *</label>
<input type="email" class="form-control" name="email" placeholder="Please type in your email address *">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-6" style="margin-bottom: 20px">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" placeholder="Please type in your phone number">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-12" style="margin-bottom: 20px">
<label>Message *</label>
<textarea class="form-control" name="message" rows="6"></textarea>
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-12" style="margin-bottom: 20px">
<input type="submit" class="btn btn-primary" value="Send Message">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-12" style="margin-bottom: 10px">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</form>
after I have checked the form tag of your html, I observed the following
The enctype attribute that specifies how the form-data should be encoded when submitting it to the server was absent, the enctype attribute is required if method="post". this can be either multipart/form-data,text/plain, or application/x-www-form-urlencoded
if this does not fix it remove the role = form and try again
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";
I currently have a contact form on my index.html & contact.html pages. When testing the PHP contact form they do not send me an email or give any notification that the message has been sent, just appearing to do nothing.
HTML
<div class="form">
<form action="php/contact.php" method="post">
<h5 class="grey-bg text-center iq-font-black iq-tw-6 iq-pall-20">Service Inquiry</h5>
<div class="iq-pall-30">
<div class="row">
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="name" placeholder="Name" id="name" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="email" placeholder="Email" id="email" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<select name="service" id="service">
<option value="Choose Service" >Choose Service</option>
<option value="test1" >test1</option>
<option value="test" >Test</option>
<span class="error">A service is required</span>
</select>
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field">
<input name="phonenumber" placeholder="Phone Number" id="phonenumber" type="text">
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<div class="section-field ">
<textarea class="form-control" placeholder="Comment" rows="5" id="comment" style="resize: none;"></textarea>
</div>
</div>
<div class="col-sm-12 iq-mtb-10">
<button id="submit" name="submit" type="submit" value="Send" class="button pull-right iq-mt-20">Send Message</button>
</div>
</div>
</div>
</form>
</div>
PHP Code
<?php $ToEmail = 'test#test.com';
$EmailSubject = 'User Contact Information';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Service: ".$_POST["service"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Service: ".$_POST["service"]."";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");?>
The email's I do receive are like this:
User Contact Information
Name: Email: Phone: Comment:
Do the fields match the PHP form, and the send message button, how can that have a response for if the email has been sent or not?
A couple errors:
Your <div class="iq-pall-30"> and <div class="row"> elements are unclosed, which make your markup invalid and might affect the output you're getting to your PHP script.
You reference form elements that doesn't actually exist in your form:
$_POST["phone"] is probably supposed to be a reference to your HTML form element <input type="text" name="phonenumber">. You'll have to change your variable reference to $_POST["phonenumber"] to correctly retrieve the data.
$_POST["message"] is probably supposed to be a reference to your textarea form element, but you haven't assigned a name to that element in your markup. You should add name="message" as an attribute of the textarea in order to correctly retrieve that data.
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 am a complete novice with PHP, I just want a simple email contact form that will display an error if a required field isn't entered, and will give a thank you message if the email is sent.
When I click my submit button the email does get sent but the website redirects straight to the /send-email.php page, which is blank and useless. The required form fields also don't seem to do anything in the way of preventing an email from being sent if the forms aren't filled.
HTML form code:
<form class="contact-form" action="php/send-email.php" method="post" novalidate>
<div class="row">
<div class="column width-6 pad-1 contact-column">
<h5>Your Name*</h5>
<input type="text" name="name" class="form-box" tabindex="1" required>
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Email*</h5>
<input type="text" name="email" class="form-box" tabindex="2" required>
</div>
</div>
<div class="row mt-2">
<div class="column width-6 pad-1 contact-column">
<h5>Your Website</h5>
<input type="text" name="website" class="form-box" tabindex="3">
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Company</h5>
<input type="text" name="company" class="form-box" tabindex="4">
</div>
</div>
<div class="column width-12">
<input type="text" name="honeypot" class="form-honeypot">
</div>
<div class="row mt-2">
<div class="column width-12 pad-1">
<h5>Your Message*</h5>
<textarea name="message" class="form-text" tabindex="5" required></textarea>
</div>
</div>
<div class="row mt-2">
<input type="submit" value="Send Your Message!" class="btn btn-large bg-blue">
</div>
</form>
Php file
<?php
$recipient = "contact#mysite.com";
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST["website"], FILTER_SANITIZE_URL);
$company = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email.'' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$subject = "New email from contact form";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Website: $website\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n\n\n";
if(mail($recipient, $subject, $email_content, $headers)){
echo "Thanks for the email, we'll get back to as soon as possible!";
}
?>
Any help in the right direction is appreciated!
Check for empty input. Try using if else statement. eg.
if($name == '')
{
echo "Please fill in name";
}
else if($var == '')
{
//error message
}
or you can try
if($name == '' || $email == '' || $var == '')
{
echo "Please fill in all the blank";
}
I'm quite new too to PHP, so this is just a simple checking. Hope it'll helps!
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".