I am trying to create a working contact form, but this warning is showing when i submit form, Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().How can i solve this issue ? can anybody help
The main code is here
<?php
if(empty($_POST) === false)
{
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(empty($name) === true || empty($email) === true || empty($message) === true )
{
$errors[] = "Name, email and Message are required";
}
else if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
$errors[] = "This is not valid email address";
}
if (empty($errors)=== true)
{
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" ;
mail($to, $subject, $message, $headers);
header('Location : index.php?sent');
exit();
} }
?>
<!DOCTYPE html>
<html>
<head>
<title>A contact Form</title>
</head>
<body>
<div class="container" style="width: 350px; margin: 0 auto;">
<?php
if(empty($errors) === false)
{
echo "<ul>";
foreach ($errors as $error) {
echo "<li>" .$error. "</li>";
}
echo "</ul>";
}
?>
<form action="" method="POST">
<p>
<label for ="name">Name:</label> </br>
<input type="text" name="name" id="name"
<?php if (isset($_POST['name']) === true)
{ echo 'value =" ', strip_tags($_POST['name']), '"'; } ?>>
</p>
<p>
<label for ="email">Email:</label> </br>
<input type="text" name="email" id="email"
<?php if (isset($_POST['email']) === true)
{ echo 'value =" ', strip_tags($_POST['email']), '"'; } ?>>
</p>
<p>
<label for ="message">Message:</label> </br>
<textarea name="message" id ="message">
<?php if (isset($_POST['message']) === true)
{ echo strip_tags($_POST['message']); } ?> </textarea>
</p>
<p>
<input type="submit" value="Submit">
</p>
</form>
</div>
</body>
</html>
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I am trying to make a contact form for my website in php, but it seems something wrong somewhere, which i am unable to locate. Can you please help me to locate the error in the following code will be of great help and suggest further improvement on this.
Will of great help.
Thanks in advance.
<?php
// Message Vars
$msg = '';
$msgClass = '';
//check for submit
if (filter_has_var(INPUT_POST, 'submit')) {
//GET FORM DATA
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// check required fields
if (!empty($email) && !empty($name) && !empty($message)) {
// passed
// check email
if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) {
//failed
$msg = 'Please use a valid email';
$msgClass = 'alert-danger';
} else{
//passed
// reciepient email
$toEmail = 'myemail#gmail.com';
$subject = 'Contact Request From '.$name;
$body = '<h2> Contact Request</h2>
<h4> Name </h4><p>'.$name.'</p>
<h4> Email </h4><p>'.$email.'</p>
<h4> Message </h4><p>'.$message.'</p>';
// email headers
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type: text/html;charset=UTF-8" . "\r\n";
// additional headers
$headers .= "From:" .$name. "<".$email.">". "\r\n";
if (mail($toEmail, $subject, $body, $headers)) {
// email sent
$msg = ' Your email has been sent';
$msgClass = 'alert-success';
}
}
# code...
} else {
//failed
$msg = ' Please fill in all fields';
$msgClass = 'alert-danger';
// failed
# code...
}
# code...
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" >
<?php if ($msg != ''): ?>
<div class="alert <?php echo $msgClass; ?> "><?php echo $msg; ?></div>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="contact-form" >
<h1>Contact Us</h1>
<div class="txtb" >
<label>Name</label>
<input type="text"
name="name"
class="form-control"
value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
</div>
<div class="txtb" >
<label>Email</label>
<input type="text"
name="email"
class="form-control"
value="<?php echo isset($_POST['email']) ? $email : ''; ?>" >
</div>
<div class="txtb" >
<label>Message</label>
<textarea name="message" class="form-control" ><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>
</div>
<br>
<a type="submit" name="submit" class="btn" >Submit</a>
</form>
</div>
</body>
</html>
I don't know what type of the error you're seeing but there's a little thing to change
You need to change
<a type="submit" name="submit" class="btn" >Submit</a>
to
<input type="submit">
I've made a PHP form to submit to self with error validation, but the form is not submitting. The idea is, when the user clicks on the submit button and hasn't filled in all required fields or email address they entered is flawed, then errors occur by adding an error class that's sorted by CSS. The CSS is fine, but the form is not submitting. I'd appreciate the help.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Email</title>
</head>
<body>
<?php
$error = '';
$to = "name#example.com";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["message"])) {
$error = 'class="error" ';
} else {
$name = stripslashes(trim($_POST["name"]));
$email = stripslashes(trim($_POST["email"]));
$message = stripslashes(trim($_POST["message"]));
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email)) {
$error = 'class="error" ';
}
$emailIsValid = filter_var($email, FILTER_VALIDATE_EMAIL);
if ($name && $email && $emailIsValid && $message) {
$subject = "From $name";
$body = "Name: $name <br /> Email: $email <br /> Message: $message";
$headers = "Reply-To: $email";
$success = mail($to, $subject, $body, $headers);
if ($success) {
header("Location: /email/sent/");
} else {
header("Location: /error/");
}
}
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input <?php echo $error; ?>type="text" name="name" placeholder="Full Name" spellcheck="false">
<input <?php echo $error; ?>type="text" email="email" placeholder="Email Address" spellcheck="false">
<textarea <?php echo $error; ?>type="text" message="message" placeholder="Message" rows="6" spellcheck="false"></textarea>
<button type="submit" name="submitted">submit</button>
</form>
</body>
</html>
NOTE : You have typo mistakes in your form tag.you used double quote inside double quote.
Insted of using this
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
and
if ($_SERVER["REQUEST_METHOD"] == "POST") {
Use
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
and
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submitbutton'])) {
//SO IT WILL PERFORM ONLY WHEN SUBMIT BUTTON WAS PRESSED
For More You can Learn it here
Or Also Live Demo is available
I am creating a site on Yahoo Small Business hosting, and am having issues with the PHP contact form that I use on my WordPress install on Bluehost. The form is:
<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = 'chris#bingetech.com';
}
$subject = '[PHP Snippets] From '.$name;
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?>
<div id="contact-wrap">
<?php if(isset($emailSent) && $emailSent == true)
{ ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error">Sorry, an error occurred.<p>
<?php } } ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ul class="formList">
<li>
<label class="contact_labels" for="contactName">Name:</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>
<li>
<label class="contact_labels" for="email">Email: </label>
<input type="email" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>
</ul>
<div id="subContain">
<input type="submit" id="submit" value="Ask Away!"></input>
</div>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
</div>
I have the appropriate header calls and this does work on Bluehost, and from my research it appears Yahoo Small Business hosting can be tricky with contact forms/email. How can I get around this?
You should use some plugin instead of your own PHP code. Try Contact form 7 its completely customize-able
I have been trying to fix my contact form wherein the data can be sent via email. But i seem to have some errors at the start. It says in the web page "Undefined variable" yet. I'm only following a tutorial that i have been reading and i'm not yet adept in PHP. I'm using XAMPP at the moment in order to run my PHP
Here is the HTML Markup
<html>
<head>
<title>Contact Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Contact Form</h1>
<p class="error"> There are some misisng fields.</p>
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
<p class="sent">Thank you for sending your message</p><?php } ?>
<div class="contactform">
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" />
<label for="email">Email:</label>
<input type="email" name="email" />
<label for="comments">Comments:</label>
<textarea name="comments"></textarea>
<input type="submit" name="submit" class="submit" value="submit" />
</form>
</div>
Here is the PHP Code
<?php if($_POST['submit']) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
</body>
</html>
Undefine Variables
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
if($_POST['submit']) {
Try declaring the variables before you use it. PHP will give out a notice if you don't pass a value first.
<?php
$error=false;
$sent=false;
if(isset($_POST['submit'])) {
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
$error = true;
} else {
$to = "clestcruz#gmail.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comments = trim($_POST['comments']);
$subject = "Contact Form";
$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
$headers = "From:" . $name;
$mailsent = mail($to, $subject, $messages, $headers);
if($mailsent){
$sent= true;
}
}
}
?>
These lines:
<?php if($error == true) { ?>
<?php } if($sent == true) { ?>
appear near the top of your HTML, but as far as I can see, there's been no PHP executed at this point, so $error and $sent won't be defined.
This line:
if($_POST['submit']) {
is testing for a value, but unless your form has been submitted, it too won't be defined. You could test this more effectively with
if (isset($_POST['submit'])) {
// do stuff
}
I have a problem here. I created a mail() PHP script to send attachments and you would normally send in your regular email provider. The problem is that I can only send files up to a certain number of characters. For example I can send a file with this name "New Text Document" but if I try to send a file with this name "New Document of Microsoft Word (3)" it never gets to my email.
Can someone please tell me why this happens?
error_reporting(-1);
if(empty($_POST) === false){
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$file = $_FILES['filename'];
if(empty($name) === true || empty($email) === true || empty($_POST['message']) === true){
$errors[] = 'Name, email and message are required!';
} else {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$errors[] ='Not a valid email';
}
if(ctype_alpha($name) === false){
$errors[] ='Name must only contain letters';
}
}
$message;
if(empty($errors) === true){
if($_FILES['filename']['error'] == UPLOAD_ERR_OK){
$boundary = '-----' . md5(rand()) . '---';
$headers = array(
'MIME-Version: 1.0',
"Content-type: multipart/mixed; boundary=\"{$boundary}\"",
"From: {$email}"
);
$message = array(
"--{$boundary}",
'Content-type: text/html',
'Content-Transfer-Encoding: 7bit',
'',
chunk_split($_POST['message']),
"--{$boundary}",
"Content-type: {$file['type']}; name=\"{$file['name']}\"",
"Content-Disposition: attachment; filename=\"{$file['name']}\"",
'Content-Transfer-Encoding: base64',
'',
chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
"--{$boundary}--"
);
$message = implode("\r\n", $message);
} else {
$headers = array(
"From: {$email}"
);
$message = &$_POST['message'];
}
//send email
var_dump(mail($email, 'Contact form', $message, implode("\r\n", $headers)));
echo $_FILES['filename']['name'];
/*//redirect user
header('Location: index.php?sent');
exit();*/
}
}
?
<!doctype html>
<html>
<head>
<title>A contact form</title>
</head>
<body>
<?php
if(isset($_GET['sent']) === true){
echo '<p>Thanks for contacting us</p>';
} else {
if(empty($errors) === false){
echo '<ul>';
foreach($errors as $error){
echo '<li>', $error ,'</li>';
}
echo '</ul>';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<p>
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" <?php if(isset($_POST['name']) === true){ echo 'value="', strip_tags($_POST['name']), '"';} ?>>
</p>
<p>
<label for="email">Email:</label><br />
<input type="text" name="email" id="email" <?php if(isset($_POST['email']) === true){ echo 'value="', strip_tags($_POST['email']), '"';} ?>>
</p>
<p>
<label for="message">Message:</label><br />
<textarea name="message" id="message"><?php if(isset($_POST['message']) === true){ echo strip_tags($_POST['message']); } ?></textarea>
</p>
<p>
List of files allowed: .pdf, .odt, .doc(x), xls(x), ppt(x), >xps, xml
</p>
<p>
<input type="file" name="filename">
</p>
<p>
<input type="submit" value="submit">
</p>
<?php
}
?>
</form>
</body>
</html>
It sends attachments but only if the file has for example the file has a name 10 characters, but if it has 15 it won't send