No email received when sent with contact form - php

When I am filling out the contact form on the website that I am making, the e-mail will be sent, but I am not receiving it in the inbox of my computer.
The code looks like this:
HTML:
<div id="form">
<form action="mailto:psteintj#xs4all.nl" id="contactForm" method="post">
<span></span>
<input type="text" name="name" class="name" placeholder="Enter your name" tabindex=1 />
<span></span>
<input type="text" name="email" class="email" placeholder="Enter your email" tabindex=2 />
<span id="captcha"></span>
<input type="text" name="captcha" class="captcha" maxlength="4" size="4" placeholder="Enter captcha code" tabindex=3 />
<span></span>
<textarea class="message" placeholder="Enter your message" tabindex=4></textarea>
<input type="submit" name="submit" value="Send e-mail" class="submit" tabindex=5>
</form>
</div>
JS:
if ((captchaVal == captchaCode) && (emailFilter.test(emailText)) && (nameFilter.test(nameText)) && (messageText > 50)) {
$.post("mail.php", {
name: $(".name").val(),
email: $(".email").val(),
message:$(".message").val()
});
$("#contactForm").css("display", "none");
$("#form").append("<h2>Message sent!</h2>");
return false;
}
and PHP:
<?php
$name = $POST['name'];
$email = $_POST['email'];
$message = $POST['message'];
Could someone tell me where I am going wrong?

Well, you are not sending any emails (or at least you haven't posted any code about it), so of course you are not receiving any emails. You should configure the mailing, and use the mail function.
The function needs a working SMTP server to actually send out the e-mail.

Your PHP has no mail() call or similar? I'm probably missing something here.

Related

Can't send mail through php in html [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm trying to develope a personal website as a projecet to one of my classes.
I don't understand much of html so i actually can't make my php work.
Please Help.
I've already tried copying and changing php codes from other fonts but nothing works...
This is the html code:
<div class="container">
<form action="" method="POST">
<input type="text" id="name" name="name" placeholder="Name">
<input type="text" id="email" name="email" placeholder="Email">
<textarea id="message" name="message" placeholder="Message" style="height:100px"></textarea>
<input type="submit" value="Send">
</form>
</div>
<form action="" method="POST">
<input type="text" id="name" name="name" placeholder="Name">
<input type="text" id="email" name="email" placeholder="Email">
<textarea id="message" name="message" placeholder="Message" style="height:100px"></textarea>
<input type="submit" name="submit" value="Send">
</form>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to_email = $email; // mail whom you want to send
$subject = ''; // your subject for sending email
$message = $name.' sent you message. <br><br>'.$message;
$headers = 'From: noreply#company.com'; // your mail id
mail($to_email,$subject,$message,$headers);
echo "This email is sent using PHP Mail";
}
?>
Use this above code, this will pass the name, email, message to mail function.
Hope this helps you.

Why won't my PHP form send emails correctly?

It sends the email, but without a body, subject or anything. This is what I get:
HTML:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" class="text" /></label>
<label for="message">Message:<textarea id="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
PHP (mail.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$headers = "From: " . $email;
mail($recipient, $subject, $message, $headers);
echo "Thanks! The message was sent. :)";
?>
Any insight? Thanks so much.
Forms fields are based on their names not their ids.
replace your :
id="(...)"
by
id="(...)" name="(...)"
You should have a look to Swift Mailer which is strongly safer than your current method.
You need to have a name attribute on your HTML input fields. That's what the form uses to create the indexes in the $_POST array. Corrected HTML is below
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
you need to give name attribute for each html element . Use below html code:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
Into my server works, are you sure that in $_POST are set?
If you can't send email assicure you can try to set SMTP to send your email
Try to use something like phpmailer to send email.
In your form for every field you have to put:
- id="example_id"
- name="example_name"

how to call a php function from my html form?

I have a template for a website, and I want to customize the message sender there. I saw this form to help out with the implementation of it.
the php file looks like this:
<?php
echo 'testing php';
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "myEmail#hotmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
$send = mail($receiver, 'Contact Form Submission', $body, $email);
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
?>
UPDATE
I've managed to call the php file like this:
<form id="form" method="post" action="ajaxSubmit.php" >
<fieldset>
<label><input type="text" id="name" name="name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
<label><input type="text" id="email" name="email" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''"></label>
<label><input type="text" id="web" name="web" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''"></label>
<label><textarea id="text" name="text" onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label>
<input type="reset" />
<input type="submit" />
</fieldset>
</form>
but when I run this I get teh following ERROR
Warning: mail() [function.mail]: SMTP server response: 550 The address is not valid. in C:\wamp\www\forSale\dataTable\ajaxSubmit.php on line 17
but then I check the vallues of the variables, they are correct. what does that mean?
As I said in the chat, you should try by starting from the ground up by first submitting the form normally, then improve it by validating it with javascript and after that try to submit it with ajax.
If you modify the form back to basics you get:
<form id="form" method="post" action="ajaxSubmit.php" >
<fieldset>
<input type="text" id="name" name="name" value="Name"
onBlur="if(this.value=='') this.value='Name'"
onFocus="if(this.value =='Name' ) this.value=''" />
<input type="text" id="email" name="email" value="Email"
onBlur="if(this.value=='') this.value='Email'"
onFocus="if(this.value =='Email' ) this.value=''" />
<input type="text" id="web" name="web" value="Phone"
onBlur="if(this.value=='') this.value='Phone'"
onFocus="if(this.value =='Phone' ) this.value=''" />
<textarea id="text" name="text" value="Message"
onBlur="if(this.value==''){this.value='Message'}"
onFocus="if(this.value=='Message') this.value=''}" />
<input type="reset" />
<input type="submit" />
</fieldset>
</form>
Unless my lack of coffee is playing tricks on my eyes, you have not specified a name attribute on those inputs. $_POST does not contain the ID of the element, but rather the 'name' and 'value' attributes.
e.g:
<input type="text" id="name" value="Name" name="name" ...
edit: to debug this theory, try outputting the values of the $_POST variables in your PHP file
add attribute name="field_name" to the input fields. This might fix the issue.

PHP - send two fields through mail

I have a document with 2 textboxes and 1 submit button. I want to send an email when I press the submit button and in it should be the value of the textboxes. My PHP knowledge is a bit dusty. I don't want to bother the user with the emailproces, so he can't see this and should just be redirected.
<html>
<body>
<div class="section_form" id="usernameSection">
<label for="username">Login:</label>
<input size="20" type="text" name="username" id="username" />
</div>
<div class="section_form" id="emailSection">
<label for="email">Email:</label>
<input size="20" type="text" id="email" name="email" maxlength="20"/>
</div>
<div id="submit_button">
<button type="submit" value="Submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button>
</div>
</body>
</html>
Many thanks!
Use the PHP mail function - listed here.
That should be able to help.
Basically the core of what you're looking for is
<?php
// Check that all fields are present, construct the message body, etc
mail($to, $subject, $body);
header("Location: wherever.php");
exit();
?>
See the mail function in PHP documentation. (Also, thank you Alex for the exit() reminder.)
Use this code for your task.
<?php
if(isset($_POST['submit'])){
extract($_POST);
$message = "username : $username ; email : $email";
$to = "your#email.com";
$subject = "Email Subject";
mail($to, $subject, $message);
}
?>
<html>
<body>
<form method="POST" name="form1">
<div class="section_form" id="usernameSection">
<label for="username">Login:</label>
<input size="20" type="text" name="username" id="username" />
</div>
<div class="section_form" id="emailSection">
<label for="email">Email:</label>
<input size="20" type="text" id="email" name="email" maxlength="20"/>
</div>
<div id="submit_button">
<button type="submit" value="Submit" name="submit" onmouseover="this.style.backgroundPosition='bottom';" onmouseout="this.style.backgroundPosition='top';" onclick="return SetFocus();">Submit</button>
</div>
</form>
</body>
</html>
Start with:
- $_POST
- mail()
Also you need to check: Forms in HTML, you might want to use form to send the data of input fields to server.

HTML contact form in PHP block

I'm relatively new to PHP so forgive me if this seems obvious. How would you integrate a block of HTML code like this:
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required="required" placeholder="Your Name">
<label></label>
<input name="email" type="email" required="required" placeholder="Your Email">
<label></label>
<input name="address" type="name" required="required" placeholder="Your Address">
<label></label>
<textarea name="message" required > Dear Mr. X, Please support us... </textarea>
<input id="cancel" name="cancel" value="Cancel" />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
into here:
if(empty($mpemail)){
echo "<h2>Send your MP our pre-written letter</h2>
<p>Unfortunately, your MP's email address is not listed in our database.</p>";
}
else {
echo "<h2>Send your MP our pre-written letter</h2>";
echo "<p>Please fill out the information required below:</p>";
**Insert Contact Form Here**
echo "<h2>Share this with your followers!</h2>";
echo 'Tweet ';
echo "<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>";
}
Thanks in advance for your help.
So, one option is to echo it out line by line, as your PHP code sample is doing, or in one big block. You can also combine PHP and HTML in the same file by starting and stopping the PHP interpreter. Here's an example of that:
if (empty($mpemail))
{
echo "<h2>Send your MP our pre-written letter</h2>
<p>Unfortunately, your MP's email address is not listed in our database.</p>";
}
else
{
?>
<h2>Send your MP our pre-written letter</h2>
<p>Please fill out the information required below:</p>
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required="required" placeholder="Your Name">
<label></label>
<input name="email" type="email" required="required" placeholder="Your Email">
<label></label>
<input name="address" type="name" required="required" placeholder="Your Address">
<label></label>
<textarea name="message" required > Dear Mr. X, Please support us... </textarea>
<input id="cancel" name="cancel" value="Cancel" />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<h2>Share this with your followers!</h2>
Tweet
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<?php
}
In a professional production application, the "right way" to do it is to separate your HTML from your PHP by putting the logic of your application in a separate PHP file, and then putting your HTML in a "template" which you load and display from PHP. Here is a bit of info about getting started with template engines and Model View Controller patterns in an application. But, for what you're trying to do, the example above of starting and stopping the interpreter should work for you.

Categories