I'am having trouble with the form. It goes through but I never receive an email. I have checked in spam but nothings showed up. Could someone give me hint. Not able to identify error.
<div class="contact-form">
<form class="email" action="mailer.php" method="post">
<h3>Kontaktirajte nas!</h3>
<div>
<p>Ime:</p>
input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Naslov :</p>
<input type="text" name="subject" />
<p>Poruka:</p>
<textarea name="message"></textarea></p>
<input class="send" type="submit" value="Send">
</form>
</div>
</div>
Below is the php code (mailer.php).
$myemail = "example#gmail.com";
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
mail($myemail, $subject, $message);
header('Location: thanks.html');
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
The code looks fine and you can reference PHP mail here # http://uk1.php.net/manual/en/function.mail.php
You need to check a FROM address is defined in your code or is set in your php.ini (this could be causing it to get bounced back and not hit your emails)
You don't need a smtp server running if you send out via PHP mail but it's worth testing on a SMTP server to see if this is your issue.
Try W3schools code and see if that sends for you and let us know the results from that...
Link can be found here # http://www.w3schools.com/php/php_mail.asp
Sendin emails can be a very thicky thing.
The first thing to check is that you have a valid sender address defined. Some hosts simply refuce sending emails without valid email.
$additional_headers = "From: from#example.com\r\n";
mail($myemail, $subject, $message, $additional_headers);
Check mail error logs (if you access them), it should give you more insight on what is the problem.
Related
Newb here, sorry in advance. I want to check for errors in a simple html form. I want the form to email me the customers information. I have my contact.php and errorcheck.php uploaded to my hosting provider. i actually made a working script contactform.php. (working) So I have a clue that my host can process the php code, but my errorcheck.php script is not working. Can anyone check my errorcheck.php script and see what may be causing the problem. all I want errorcheck.php to do so far, is display the error in the web address bar, just so I know that its working. Thanks in advance for helping a newb. I hope I explained my self and code well enough.
contactform.php (working script will connect and send me an email of form information)
errorcheck.php (will not connect or check the form for errors, I want to display the error in web address bar)
contact.php (this is the html form)
errorcheck.php code below
<?php
if (isset($_POST['submit'])) {
$first = $_POST['name'];
$mail = $_POST['mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//check if inputs are empty
if (empty($first) || empty($mail) || empty($subject) || empty($message)) {
header("Location:contact.php?signup=empty");
exit();
} else {
if (!preg_match("/^[a-zA-Z]*$/", $first)) {
header("Location:contact.php?signup=char");
exit();
} else {
//check if email is valid
if (!fiter_var($mail, FILTER_VALIDATE_EMAIL)) {
header("Location:contact.php?signup=invalid-email");
exit();
} else {
header("Location:contact.php?signup=signup=success");
}
}
}
}
(html code will connect to contactform.php but not errorcheck.php)
contact.php form code is below
<form class="contact-form" action="contactform.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full name..">
<label for="mail">Email Address</label>
<input type="text" name="mail" placeholder="Your E-Mail..">
<label for="subject">Phone Number</label>
<input type="text" name="subject" placeholder="Phone Number..">
<label for="message">Message</label>
<textarea name="message" placeholder="Message.."></textarea>
<input type="submit" value="submit" name="submit"></input>
</form>
contactform.php code is below
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "sales#screenrunners.com";
$headers = "From: ".$mailFrom;
$txt = "You have received and e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.php?mailsend");
}
?>
I'm having an issue with my website. To give some context, I've worked on it using ReactJS for the front-end and Express for the back-end. BUT, my hosting provider doesn't support Node, so I had to change the back-end part to PHP. I only developed the contact form functionality.
I'm not sure if I'm missing something, but I would like to ask your opinion to continue due to I'm stuck.
Here's the front-end part for Contact.js:
import React, {Component} from 'react';
class Contact extends Component{
render(){
return(
<div>
<form id="contact-form" name="c-form" method="post" action="/mailer.php">
<div className="input-field">
<input
id="first_name"
type="text"
className="validate"
name="first_name"
required/>
<label for="first_name">Name</label>
</div>
<div className="input-field">
<input id="sub" type="text" className="validate" name="sub"/>
<label for="sub">Subject</label>
</div>
<div className="input-field">
<input id="email" type="email" className="validate" name="email" required/>
<label for="email">Email</label>
</div>
<div className="input-field">
<textarea
id="textarea1"
className="materialize-textarea"
name="message"
required></textarea>
<label for="textarea1">Message</label>
</div>
<div className="contact-send">
<button
id="submit"
name="contactSubmit"
type="submit"
value="Submit"
className="btn waves-effect">Send
</button>
</div>
</form>
</div>
)
}
}
export default Contact;
and also, the back-end part in the PHP file, mailer.php located under /src folder:
<?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "*HERE I PLACED MY EMAIL*";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
The result is:
POST http://www.*mywebsite.com*/mailer.php 400 (Bad Request)
Any suggestion or recommendation? Thanks in advance!
change mailer.php
$name = strip_tags(trim($_POST["name"]));
to
$name = strip_tags(trim($_POST["first_name"]));
My PHP form isn't submitting successfully. I keep getting the custom error that I wrote ('Oops there was a problem. Please try again").
any help would be greatly appreciated. I'm totally new to PHP so I'm thinking maybe some of my php variables are linked wrong and arent connecting with my mailer-new.php file?
Thanks in advance,
<section class="form-body">
<form method="post" action="mailer-new.php" class="contact-form" >
<div class="row">
<?php
if ($_GET['success']== 1){
echo " <div class=\"form-messages success\"> Thank you!
your message has been sent. </div>";
}
if ($_GET['success']== -1){
echo " <div class=\"form-messages error\"> Opps there was a
problem. Please try again </div>";
};
?>
<div class="field name-box">
<input type="text" name="name" id="name" placeholder="Who
Are You?" required/>
<label for="name">Name</label>
<span class="ss-icon">check</span>
</div>
<div class="field email-box">
<input type="text" name="email" id="email"
placeholder="name#email.com" required/>
<label for="email">Email</label>
<span class="ss-icon">check</span>
</div>
<div class="field msg-box">
<textarea name="message" id="msg" rows="4"
placeholder="Your message goes here..."/></textarea>
<label for="message">Msg</label>
<span class="ss-icon">check</span>
</div>
<input class="button" type="submit" value="Send"/>
</div>
</form>
</section>
MAILER.PHP
<?php
// Get the form fields, removes html tags and whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"), array(" ", " " ), $name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check the data.
if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: http://www.conallen.ie/index.php?
success=-1#form");
exit;
}
// Set the recipient email address. Update this to YOUR desired email address.
$recipient = "allenconallen46#gmail.com";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
mail($recipient, $subject, $email_content, $email_headers);
// Redirect to the index.html page with success code
header("Location: http://www.conallen.ie/index.php?success=1#form");
?>
This code works correctly in my local machine, even though email is not sent response messages are coming correctly. The changes I have done is changed the host name and made the two lined header redirect into one line in the failed response. Also you have mentioned in the HTML the file name as action="mailer-new.php" and the file name mentioned in the question as MAILER.PHP. Are they same in the code?
To check whether the mail is sent or not remove the header redirect and update like this. If you are getting a failed response means mail is not configured in your server.
if(mail($recipient, $subject, $email_content, $email_headers)) {
echo "mail sent";
} else {
echo "mail sent failed";
}
Alternatively you can use the SMTP for sending email. You can use a library called PHP Mailer and can use any of the valid email address like a gmail account. Please have look at this question if that's the case
Sending email with PHP from an SMTP server
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have created a simple html form to send an email using php. Currently whenever I try to send the info I just get redirected to my process.php page with and browser error (myserver.com unable to handle this request).
I have already tried sending a test email by making my php page just the mail() function and it does indeed work so it has to do with my code somewhere. I'm sure it's something simple so here is my code.
HTML (contact.html):
<!-- Contact form -->
<form id="form" action="process.php" method="post">
<div>
<label for='name'><span class='required'></span></label>
<input id="Field1" type='text' name='name' placeholder='Type your Email Here' required/>
</div>
<div>
<label for='message'><span class='required'></span></label>
<textarea id="Field2" name='message' placeholder="Type a Message for us Here" required></textarea>
</div>
<div>
<button type='submit'>SEND MESSAGE</button>
</div>
</form>
PHP (process.php):
<?php
//if "email" variable is filled out, send email
if(isset($_POST['name']) && isset($_POST['message'])) {
//Email information
$admin_email = "test#mydomain.com";
$email = $_POST['name'];
$subject = "Email from contact form";
$comment = $_POST['message'];
//send email
if(mail($admin_email, $subject, $comment, "From:" . $email)) {
echo '<p>Success</p>';
header('Location: contact.html');
} else {
echo '<p>Error sending message</p>';
}
} else {
echo '<p>Please fully fill out the form</p>';
}
?>
You have syntax error in the PHP code you have shared - You are missing ; in the $subject = "Email from contact form" line.
Please see bellow -
<?php
//if "email" variable is filled out, send email
if (isset($_POST['name'], $_POST['message'])) {
//Email information
$admin_email = "test#mydomain.com";
$email = $_POST['name'];
$subject = "Email from contact form";
$comment = $_POST['message'];
// //send email
if(mail($admin_email, $subject, $comment, "From:" . $email)) {
echo '<p>Success</p>';
header('Location: contact.html');
} else {
echo '<p>Error sending message</p>';
}
} else {
echo '<p>Please fully fill out the form</p>';
}
?>
Check your php.ini file for mail configurations.
open php.ini file
search "mail function" there
set SMTP to your server
set sendmail_from (sending mail_ID)
Forgive me if this is a stupid question, but I am pretty new to PHP and I am running into some issues. I am trying to build a contact form with HTML, CSS, and PHP, but I can't seem to get my PHP form to send the contents of the form to my email address. This is what the code looks like for the HTML:
<div id="contact-form">
<ul>
<li><button id="quote" class="button1">Project Quote</button></li>
</ul>
<form class= "emai" action="mailer.php" method="post">
<p>Have a project in mind? Fill in the form for a quote!</p>
<div>
<p><label for="name">What can I call you? <span>*</span></label></p>
<input type="text" id="name" name="name">
</div>
<div>
<p><label for="email">What is your email? <span>*</span></label></p>
<input type="email" name="email" id="email">
</div>
<div>
<p><label for="type">Type of Project? <span>*</span></label></p>
<select id="type" name="type">
<option value="logo">Logo Design</option>
<option value="web dev">Website/WebApp Dev</option>
<option value="other">Other</option>
</select>
</div>
<div>
<p><label for="purpose">What is the main purpose of your project? <span>*</span></label></p>
<textarea id="purpose" name="purpose"></textarea>
</div>
<div>
<p><label for="features">Any extra features?</label></p>
<textarea id="features" name="features"></textarea>
</div>
<input class="button1" type="submit" value="submit">
</form>
</div>
And in a separate doc called "mailer.php" this is what the code looks like:
<?php
/* Set e-mail recipient */
$myemail = "christopher.kenrick#gmail.com";
$subject = "Project Request";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email']);
$type = check_input($_POST['type'], "Select a type of project");
$purpose = check_input($_POST['purpose'], "What is the purpose of your project?");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Name: $name
E-mail: $email
Type: $type
Purpose: $purpose
Features: $features
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Here is a link to my website if it will help. Can someone tell me what it is I am doing wrong?
Your code lacks proper mail headers. (and originally had a missing subject variable which you now added).
Add and modify your present mail() function with the following code, otherwise mail will be sent directly to Spam as it did for my test.
$headers = 'From: ' . $email . "\r\n";
mail($myemail, $subject, $message, $headers);
with a conditional statement:
if(mail($myemail, $subject, $message, $headers)){
echo "Success"; } else{ echo "There was a problem.";}
After running sudo apt-get install sendmail I finally started receiving the contents of the contact form. This solution should work for those using DigitalOcean as their host.