I'm having trouble making this properly work.
What I want to happen is for someone to be able to fill out the given information but there's an email validation which is just you put your email in Email: and is suppose to match in Verify Email: then in hopes that they did that write and if they answer the spam question right which is just "what is 5+5?" then the form will send to my email
The problem is that, although the email validation is working, the spam question even if you write the answer wrong, the form is still sent. I need it so that the spam and the email validation either or must be correct or the form won't send.
I hope made this simple for some to understand and help me out! Here's my PHP code. Let me know what I'm doing wrong.
<script>
<?php
$email1=$_POST['Email1'];
$email2=$_POST['Email2'];
$from=$_POST['Email1'];
$email="!YOUREMAIL#GOES.HERE!";
$subject="maintenance Request";
$message=$_POST['Box'];
$select=$_POST['Select'];
$name=$_POST['Name'];
$number=$_POST['Question'];
$message="Name: ".$name. "\r\n" ."\r\n" . "Email: " .$from ."\r\n" . "\r\n" . "Comment: " .$message ."\r\n" . "\r\n" . "Selected Machine: " .$select;
if($number==10) {
}
if(filter_var($email1, FILTER_VALIDATE_EMAIL)) {
}
if (filter_var($email2, FILTER_VALIDATE_EMAIL)) {
mail ($email, $subject, $message, "from:".$from);
echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
echo "This ($email2) email address is different from ($email1).\n";
}
?>
</script>
If the answer to the validation question is wrong, you should print a message and exit the script.
if ($number != 10) {
die("You are not a human!");
}
You're also never checking if the two emails are the same.
if(!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
die("Invalid email ($email1)");
}
if ($email1 == $email2) {
mail ($email, $subject, $message, "from:".$from);
echo 'Thanks You for the Maintenance Request! We will Contact you shortly. ';
}
else {
echo "This ($email2) email address is different from ($email1).\n";
}
There's no need to call filter_var() on $email2. If it's equal to $email1 then it must be valid. If it's not equal to $email1, we don't care if it's valid.
Related
I am trying to send email from a web page hosted on a shared platform over at fasthosts. I cannot for the life of me get this to work, I had a much more extensive script which checked the validity of email etc, but now I've been reduced to using the basic example from fasthosts and it is still not working.
Please could someone take a look and let me now where I am going wrong...
<?php
// You only need to modify the following two lines of code to customise your form to mail script.
$email_to = "contact#mywebsite.co.uk"; // Specify the email address you want to send the mail to.
$email_subject = "Feedback from website"; // Set the subject of your email.
// This is the important ini_set command which sets the sendmail_from address, without this the email won't send.
ini_set("contact#mywebsite", $email_from);
// Get the details the user entered into the form
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// The code below creates the email headers, so the email appears to be from the email address filled out in the previous form.
// NOTE: The \r\n is the code to use a new line.
$headers = "From: " . $email_from . "\r\n";
$headers .= "Reply-To: " . $email_from . "\r\n"; // (You can change the reply email address here if you want to.)
// Now we can construct the email body which will contain the name and message entered by the user
$message = "Name: ". $name . "\r\nEmail: " . $email . "\r\nMessage: " . $message ;
// Now we can send the mail we've constructed using the mail() function.
// NOTE: You must use the "-f" parameter on Fasthosts' system, without this the email won't send.
$sent = mail($email_to, $email_subject, $message, $headers, "-f" . $email_from);
// If the mail() function above successfully sent the mail, $sent will be true.
if($sent) {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$name .' Thank you for contacting us.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}
?>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have an event that fires every time a user successfully submits a form on my website. I can track the exact time that the event fired from my Google Analytics account. The time that the event fired, mirrors the delivery time of the email delivered to my inbox (yahoo mail).
However, I've noticed many times that, when the same event fires, no email is delivered to the the same email address. It never reaches its destination.
How can I fix this? What is the root of this problem?
Here is my php code:
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userLastName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userArrival"]) || !isset($_POST["userNumberPeople"])
|| !isset($_POST["userPickupLocation"]) || !isset($_POST["userRequest"]) || !isset($_POST["userTourSelection"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_LastName = filter_var($_POST["userLastName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_TourSelection = filter_var($_POST["userTourSelection"], FILTER_SANITIZE_STRING);
$user_Arrival = filter_var($_POST["userArrival"], FILTER_SANITIZE_STRING);
$user_NumberPeople = filter_var($_POST["userNumberPeople"], FILTER_SANITIZE_STRING);
$user_PickupLocation = filter_var($_POST["userPickupLocation"], FILTER_SANITIZE_STRING);
$user_Request = filter_var($_POST["userRequest"], FILTER_SANITIZE_STRING);
$to_Email = "something#yahoo.com"; //Replace with recipient email address
$subject = 'Tour request: '.$user_TourSelection.' '; //Subject line for emails
//additional php validation
if(strlen($user_Name)<2) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email address!');
exit();
}
if(strlen($user_Request)<5) //check emtpy message
{
header('HTTP/1.1 500 Please explain in a few words how you would like us to assist you.');
exit();
}
// message
$message = '<strong>Name:</strong> '.$user_Name.' '.$user_LastName.' <br><br>
<strong>Date of Arrival:</strong> '.$user_Arrival.' <br><br>
<strong>Tour:</strong> '.$user_TourSelection.' <br><br>
<strong>Number of people:</strong> '.$user_NumberPeople.' <br><br>
<strong>Pick-Up Location:</strong> '.$user_PickupLocation.' <br><br>
<strong>Request:</strong> '.$user_Request.'
';
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
$headers .='Reply-To: '.$user_Email.'' . "\r\n" ;
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .='X-Mailer: PHP/' . phpversion();
#$sentMail = mail($to_Email, $subject, $message, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>something#yahoo.com</strong>');
exit();
}else{
echo 'Congratulations '.$user_Name .'! ';
echo 'We have received your request and we will respond as soon as possible.';
}
}
?>
mail()'s delivery function ends when it hands off your mail to the SMTP server. Its sole responsibility is the real-world equivalent of taking your envelope and dropping it into the mailbox on the corner. The rest of the postal service (emptying that box, running it through processing centers, flying it to the recipient's country/city, etc...) is completely outside of mail()'s scope. As long as the envelope drops into the mailbox, mail() will return true and pretend it was delivered.
So... check your SMTP server's logs to see what really happened to the mail. Maybe it got marked as spam by the receiver and bounced. Maybe it's stuck in a queue somewhere, etc... Only the logs will tell you this - anything you can see/do in PHP is useless, because PHP and mail() only do maybe 1% of the email sending/delivery process, and something's wrong in that other 99%.
I need my contact form on my website to be adjusted. It's a PHP/Ajax Contact Form.
Currently i have a problem - When a client fills out my contact form NAME - EMAIL - SUBJECT - MESSAGE there is an issue with my DREAMHOST Server due to their new anti spam policy and i dont receive some messages - If their email is #hotmail.com that's fine. But if their email is #gmail.com i dont get the message etc.
DREAMHOST TELLS ME:
Thanks for contacting Tech Support I checked the logs for the form on your site and did see that the emails are being bounced by the server due to recently implemented anti spam policies which won't allow email sent from the server using non-Dreamhost outgoing servers or "send from" email addresses. You can read more details on this policy here:
http://wiki.dreamhost.com/Sender_Domain_Policy_and_Spoofing
Your mail form uses the visitor's email address as the 'From' address which in most cases is not a Dreamhost hosted email address. Due to the spam policy above, the server will block mail being sent off the server if the email addresses are not using Dreamhost mail servers. So what you will need to do is either set the mail form to use your Dreamhost hosted address as the 'From' address.
Or you will need to find another mail form that will allow you to set a fixed email address as the 'From' address. This way you can set a Dreamhost hosted email address in the form as the 'From' address.
THE CODE AS FOLLOWS:
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/
include dirname(dirname(__FILE__)).'/config.php';
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
include 'functions.php';
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.<br />';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}
// Check message (length)
if(!$message || strlen($message) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.<br />";
}
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}
}
?>
All i need to know is what i need to do to the code so i can receive all submissions of my contact form. I would really be grateful if someone could help.
Replace the following:
mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
with just:
$mail = mail(WEBMASTER_EMAIL, $subject, $message,"X-Mailer: PHP/" . phpversion());
and add the user's email address to your message if you need to. What you are doing right now is called spoofing and even when well-intentioned, you are essentially committing fraud. It would be the equivalent of someone calling you to show interest in your product and you taking down their physical address and putting that in the upper-left corner of an envelope and then sending yourself a letter saying that the caller is interested. In addition to this just being a fairly-odd way of doing things, it also suggests an electronic paper trail that suggests the user actually emailed you, which they did not. I personally would be uncomfortable finding out my email address was used in this way after filling out an online form.
How do I send an email to the user with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form.
Here is my current php code. It currently just shows them a message that says there name and that the message has been sent and then sends me an email to my email address.
<?php
if(isset($_POST['submit'])){
$to = "benlevygraphics#gmail.com";
$headers = "From: " . $_POST['email'];
$subject = "Ben, you have been contacted...";
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
echo("<p class=contactformsent>".$_POST['name'].", your message has been sent!</p>");
}
else{
echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
}
}
?>
I am new to php and I have read stuff online and I still don't understand so if you could be clear in your examples or help I would greatly appreciate it very much. Thanks!
Assuming your current code is already working fine, you can do this to send yourself an email together with the recipient:
Set $to to $_POST['email']
Set $headers to "From: {$_POST['email']}\r\nBcc: benlevygraphics#gmail.com"
Adjust $body and $subject to your needs.
Btw, I can't say this often enough; make sure that your page has some form of CSRF protection.
How to properly add CSRF token using PHP
The above is just one way, there are others, just search for it :)
Look into your php.ini beacuse you have to enter a SMTP Server.
At my file it begins in line 1087 with "[mail function]"
I am using a PHP Mail form with AJAX and it's not sending any mail. What am I missing here?
send.php
<?php
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_POST['first_name']!='' && $_POST['last_name']!='' && $_POST['e_mail']!='' && valid_email($_POST['e_mail'])==TRUE && strlen($_POST['message'])>30)
{
$to = 'zacharyrs#gmail.com';
$headers = 'From: '.$_POST['e_mail'].''. "\r\n" .
'Reply-To: '.$_POST['e_mail'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Hello! I'm testing my new ajax email that I got from roscripts.com";
$message = htmlspecialchars($_POST['message']);
if(mail($to, $subject, $message, $headers))
{//we show the good guy only in one case and the bad one for the rest.
echo 'Thank you '.$_POST['first_name'].'. Your message was sent';
}
else {
echo "Message not sent. Please make sure you're not
running this on localhost and also that you
are allowed to run mail() function from your webserver";
}
}
else {
echo 'Please make sure you filled all the required fields,
that you entered a valid email and also that your message
contains more then 30 characters.';
}
?>
Is your email setup in PHP? Check that first, otherwise there doesn't seem to be any issue here that I can see. You should get some kind of output from this if/else block.
I would start by reviewing PHP error logs, to see if your mail() fn is working. That may be the cause.