Email sending using PHP and HTML without any database [duplicate] - php

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 send emails without using any database that's why I was trying to send an email using PHP and HTML, but the problem is that after pressing the submit button it does not sending any emails. Here are my codes:
<form role="form" name="contactform" id="contactform" method="POST" action="send.php" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="input-text form-group">
<input type="text" name="contact_name" class="input-name form-control" placeholder="Full Name" />
</div>
<div class="input-text form-group">
<input type="email" name="email" class="input-name form-control" placeholder="Enter Email-ID" />
</div>
<div class="input-text form-group">
<input type="text" name="contact_phone" class="input-name form-control" placeholder="Phone Number" />
</div>
</div>
<div class="col-md-6">
<div class="input-text form-group">
<input type="text" name="contact_sub" class="input-name form-control" placeholder="Subject" />
</div>
<div class="textarea-message form-group">
<textarea name="contact_message" class="textarea-message hight-82 form-control" placeholder="Message" rows="2"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-default top-margin-10 hblack" name="Submit" value="Submit" />
</div>
</div>
</form>
and the PHP code are:
<?php
$strTo = $_POST["contact_name"];
$strSubject = $_POST["email"];
$strMessage = nl2br($_POST["contact_phone"]);
$strSub = nl2br($_POST["contact_sub"]);
$strmsg = nl2br($_POST["contact_message"]);
$body="Job-title:$title \r\n
Name:$strTo \r\n
Email:$strSubject \r\n
Phone:$strMessage \r\n
Subject:$strSub \r\n
Message:$strmsg";
$from=$strSubject;
$to='abc#abc.com'; // removed for spam protection
mail($to,$from,$body);
echo "<script> alert('Your Form has been Submitted successfully'); window.location.href='index.php'; </script>";
?>
Thanks in advance.

If you use the mail function then you will have to make some server side changes to your php settings. Most commonly these changes are found in a file called "php.ini". I encourage looking up changes needed for the mail function to work on your specific server and including details about your server and php version when searching

Related

Make File Upload Field Optional on Contact Form

I have a basic HTML/PHP web form. Everything works fine when I fill out all fields, except I want to make the file upload optional, not required. I have taken "required" off of the form html for the upload. I checked my handler.php and it does not say that field is required. I checked the formhandler.php and it says that attachments are null. I have looked through similar questions, but any of the more complex solutions I am probably implementing incorrectly. I've expended my knowledge of php looking for a solution. What am I missing?
Here is the html of the form:
<div class="row pb6">
<div class="col-md-6 offset-md-3">
<form role="form" method="post" id="reused_form" enctype="multipart/form-data" >
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" id="name" name="name" required maxlength="50">
</div>
<div class="form-group">
<label for="email"> Email:</label>
<input type="email" class="form-control" id="email" name="email" required maxlength="50">
</div>
<div class="form-group">
<label for="tel"> Phone: </label>
<input type="tel" class="form-control" id="tel" name="tel" maxlength="50">
</div>
<div class="form-group">
<label for="name"> Message:</label>
<textarea class="form-control" type="textarea" name="message" id="message" placeholder="We want to hear all about your performance! Also, please include the date you need the music." maxlength="6000" rows="7"></textarea>
</div>
<div class="form-group">
<label for="file">File Upload (optional)</label>
<input type="file" class="form-control" id="image" name="image">
</div>
<button type="submit" class="btn btn-lg btn-success pull-right" id="btnContactUs">Post It! →</button>
</form>
<div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
<div id="error_message" style="width:100%; height:100%; display:none; "><h3>Error</h3>We are very sorry. There was an error sending your form. Email us and we will send you a free demo.</div>
</div>
</div>
</div>
And here is the handler.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','tel'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
if($_FILES['image']){ $pp->attachFiles(['image']); }
$pp->sendEmailTo('email'); //
echo $pp->process($_POST);
after code edit:
1)Chrome and Firefox will send form, Safari will not.
2)All browsers show a perpetual "Sending" message, and do not show a success or failure message for the form.
3) Forms send WITH attachments will not send the form with attachment to email, only the form alone.
You are trying to attach the image whether it exists or not with $pp->attachFiles(['image']);. You should first check if it does indeed exists and only attach it if it does like so:
if($_FILES['image']){
$pp->attachFiles(['image']);
}

Contact form posts empty fields [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have designed a form in html and I'm using php to send it to my email. The form validates OK and everything is fine apart from the fact that it doesn't send some information. Here is how it comes to my email:
Name:
Email:
Tel.:
Message: test
So when I fill it in the only field that goes through is the "Message" field. The other ones are blank although the form validates and process. I guess there is something in php code. I don't know much about php, so need some help please. Thanks.
<form action="post.php" method="post">
<div class="form-group">
<label for="name">Name *</label>
<input type="text" class="form-control" id="name"
placeholder="" required="required" oninvalid="this.setCustomValidity('Please enter your name')"
oninput="setCustomValidity('')">
</div>
<div class="form-group">
<label for="email">E-mail *</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="" required="required"
oninvalid="this.setCustomValidity('please enter your email')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group">
<label for="name">phone *</label>
<input type="text" class="form-control" id="phone"
placeholder="" required="required" oninvalid="this.setCustomValidity('please enter your phone')"
oninput="setCustomValidity('')">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="name">message *</label>
<textarea name="message" id="message" class="form-control"
rows="10" cols="25" required="required" placeholder="" oninvalid="this.setCustomValidity('please enter your message')"
oninput="setCustomValidity('')"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary pull-right" id="btnContactUs">send</button>
</form>
and php code:
<?php
$addressto = "info#xxx.com";
$subject = "Message from the website";
$content = "Name: ".$_POST['name']."\n"
."Email: ".$_POST['email']."\n"
."Tel.: ".$_POST['phone']."\n"
."Message: ".$_POST['message']."\n";
if(!$_POST['name'] || !$_POST['email'] || !$_POST['message']){
header("Location: contact.html");
}
$email = $_POST['email'];
if (mail($addressto, $subject, $content, 'Od: <' . $email . '>')) {
header("Location: contact-ok.html");
}
?>
Your input fields are missing the name attribute
id is mostly for JS/CSS manipulation, while name will be the keys in your POST array.
They can both be the same. Hence why your message works.
Remember that JS validation can easily be bypassed and that the user input should also be checked in php.
<input type="text" class="form-control" id="name" name="name"...>

my php for sending email is not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
im working on a html form where i need to send mail using php. i need to upload it in my hosting server. i uploaded these two files in the root directory. still im not able to send mail.
Here is my html code
<!-- form -->
<form action="http://shankiyan.in/sendEmail.php" method="post" name="form" id="contactForm" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8" >
<fieldset>
<div class="form-field">
<input name="name" type="text" id="contactName" placeholder="Name" value="" minlength="2" required="">
</div>
<div class="form-field">
<input name="email" type="email" id="contactEmail" placeholder="Email" value="" required="">
</div>
<div class="form-field">
<input name="subject" type="text" id="contactSubject" placeholder="Subject" value="">
</div>
<div class="form-field">
<textarea name="message" id="contactMessage" placeholder="message" rows="10" cols="50" required=""></textarea>
</div>
<div class="form-field">
<button class="submitform"type="submit" name="submit" id="submitform">Submit</button>
</fieldset>
</form>
<!-- Form End -->
and my php code is
<?php
require('sendEmail.php');
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$formsubject=$_POST['subject'];
$formmessage=$_POST['message'];
$to="shmediyosel#gmail.com";
$subject="subject:". $formsubject;
$message="Name".$name."\n"."wrote the following".$formmessage;
$headers="from".$email;
if(mail($to,$subject,$message,$headers))
{
echo"<h1>message sent successfully</h1>";
}
else
{
echo"<h2> something went wrongg!</h2>";
}
?>
i cant send mail from my html form .
please help me out what mistake i have done ?
Try to put this code on the top of your php file and check for any errors:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also, is the sendEmail.php requiring itself? Because this will not work.
If not, can you provide us with the sendEmail.php so we can understand easier the problem?

PHP mail from HTML Form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm trying to link my HTMl form to my PHP contact code but it doesn't work, no mail is sending. I know all the PHP settings on the server are ok, I've done a test with a single-page-form and I received a mail.
Do you know what could be wrong ?
Thank you very much!
HTML CODE :
<section id="contact-full">
<div class="container">
<h2 class="title">Message</h2>
<form action="scripts/contact.php" role="form" id="contact_form" novalidate>
<div class="form-group">
<input type="text" class="form-control" id="contact_name" placeholder="Full name" name="name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="contact_email" placeholder="Email Address" name="email">
</div>
<div class="form-group">
<textarea class="form-control" rows="4" placeholder="Your message or question" id="contact_message" name="message"></textarea>
</div>
<button type="submit" id="contact_submit" data-loading-text="•••" class="btn btn-lg btn-block btn-primary"><i class="icon icon-paper-plane"></i>Send</button>
</form>
</div>
</section>
PHP CODE :
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!empty($_POST['contactname']) && !empty($_POST['contactemail']) && !empty($_POST['contactmessage'])) {
$to = 'your#email.com'; // Your e-mail address here.
$body = "\nName: {$_POST['contactname']}\nEmail: {$_POST['contactemail']}\n\n\n{$_POST['contactmessage']}\n\n";
mail($to, "Message from yoursite.com", $body, "From: {$_POST['contactemail']}"); // E-Mail subject here.
}
}
?>
You're missing a few things;
First off in your HTML code you need to add method = "POST" to your <form> tag.
Secondly in your PHP code you grab all the incorrect names with $_POST. Change the following:
if(!empty($_POST['contactname']) && !empty($_POST['contactemail']) && !empty($_POST['contactmessage']))
to
if(!empty($_POST['contact_name']) && !empty($_POST['contact_email']) && !empty($_POST['contact_message']))
And all other parts of the code that call those objects.

how to configure email address on submit button in html contact form

I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class

Categories