my php for sending email is not working [duplicate] - php

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?

Related

how to redirect a page using php [duplicate]

This question already has answers here:
PHP header() will not redirect issue
(1 answer)
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 3 years ago.
define('DB_PASS' ,'');
define('DB_NAME', 'codexworld');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if(isset($_POST['submit'])){
$username=$_POST['username'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$q=mysqli_query($con,"insert into demo (username,email,subject,message)values('$username','$email','$subject','$message')");
if ($q){
echo 'Not Inserted';
} else {
echo 'Inserted Successfully';
header('Location: ib.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple Contact form in PHP & MySQL</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row">
<form action="ib.php" class="col-md-6 col-md-offset-3" method="post">
<h2>Contact Us</h2>
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" name="username" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Full Name" required="">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" required="">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Subject</label>
<input type="text" name="subject" class="form-control" id="exampleInputPassword1" placeholder="Enter Your Subject" required="">
</div>
<textarea class="form-control" name="message" rows="3" placeholder="Enter Your Query Here" required=""></textarea>
<button type="submit" name="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</body>
</html>
the above code connects to database and saves form data in database and it is supposed to redirect
the form data gets stored in data base but it doesn't redirect.
How can i solve this?Save the form data and redirect
You need to exit after the header redirect so the html doesn't keep loading.
header("Location: ib.php");
exit;
It's also best to not output anything before the header() .. eg; don't echo 'Inserted Successfully' in your production code.

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']);
}

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

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

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.

php mail() function not working with html page

In my website I have used mail form which invokes mail_handler.php contains mail function.But its not working and not echoing any output from if-else.
I checked my webmail account its empty for verification. below is my code for form:
<div id="form-main" style="height:500px"; action="mail_handler.php">
<div id="form-div">
<form class="form" id="form1">
<p class="name">
<input name="name" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" type="text">
</p>
<p class="email">
<input name="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" type="text">
</p>
<p class="text">
<textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
</p>
<div class="submit">
<input id="button-blue" name="Submit" type="submit">
<div class="ease"></div>
</div>
</form>
div>
and below is my php code which not taking any values from form but just checking whether php mail function is working or not when form submit button is clicked:
<?php
$to = 'yogi#yogeshkorke.com';
$message='Hii';
$mail=mail($to, "Subject: $subject",$message );
if($mail){
echo "Thank you for using our mail form";
}
else{
echo "Mail sending failed.";
}
ini_set('display_errors',1);
?>
use method post or get in your form
collect in variable
<form class="form" action="mail_handler.php" id="form1" method='post'>

Categories