PHP mail script won't work properly - php

Allright, so I am pretty new to PHP so I thought I would ask you guys what I am doing wrong. I am currently developing a website were you can contact the company through this e-mail form.
However, for some reason the email won't be delivered. I have tried multiple e-mails (and played a little with the code) and it just won't work.
Here is the mail.php script:
<?php
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$text = $_POST['text'];
//send email
mail("myemail#gmail.com", "email enquiry", $text, "From:" . $email);
}
?>
Here is the snippet about the form from my script.js:
//Contact Form Code:
$(function (e) {
$(".form-button").click(function (e) {
var $error = 0;
var name = $("#form-name").val();
var email = $("#form-email").val();
var text = $("#form-msg").val();
var security = $("#form-security").val();
if(name == "" || email=="" || text=="" ){
$('#details-error-wrap').fadeIn(1000);
$error = 1;
}else{
$('#details-error-wrap').fadeOut(1000);
}
if(security != 8 ){
$('#security-error-wrap').fadeIn(1000);
$error = 1;
}else{
$('#security-error-wrap').fadeOut(1000);
}
if( /(.+)#(.+){2,}\.(.+){2,}/.test(email) ){
} else {
$('#details-error-wrap').fadeIn(1000);
$error = 1;
}
var dataString = 'name=' + name + '&email=' + email + '&text=' + text;
if($error == 0){
$.ajax({
type: "POST",
url: "mail.php",
data: dataString,
success: function () {
$('#details-error-wrap').fadeOut(1000);
$('#security-error-wrap').fadeOut(1000);
$('#form-sent').fadeIn(1000);
}
});
return false;
}
e.preventDefault();
});
});
});
And lastly, here is the HTML:
<div class="seven columns">
<div id="form-sent" class="hide">
<div class="alert-box success">Message sent: Thankyou for your enquiry</div>
</div>
<form id="contact-form">
<label>Name *</label>
<input id="form-name" type="text"/>
<label>Email *</label>
<input id="form-email" type="text" />
<label>Message *</label>
<textarea id="form-msg"></textarea>
<div id="details-error-wrap" class="hide">
<div class="alert-box alert error-box">Error: Please ensure all fields are filled in correctly</div>
</div>
<label>Security Question *</label>
<input id="form-security" type="text" placeholder="7 + 1 = ?" />
<div id="security-error-wrap" class="hide">
<div class="alert-box alert error-box">Error: Security question is incorrect</div>
</div>
<input type="submit" value="Submit" class="form-button right" />
</form>
</div>
</div>
I posted all three related things. Parts of this website is from a template I'm using, so if you think the problem is in a different file just let me know.
Thanks in advance, I hope someone is able to help me :)

Firstly, it might be ending up in your SPAM folder.
Are you sending the email using your personal email address? Such as me#gmail.com?
If so, that's obviously not going to work, you need to setup SMTP in order for the emails to get passed the firewalls and to actually make it to the inbox.
Might want to format your script better, also, use an IF statement to see if the email is actually being sent by the PHP:
$to = 'myemail#gmail.com'; //Change this to the email you're using
$subject = 'Website Contact Form';
$headers = "From: " . "Me!! <myemail#gmail.com>" . "\r\n"; //This too
$content = "Hey!";
if (mail($to, $subject, $content, $headers)) {
echo "Success! Email was sent - Yay!";
} else {
echo "Error sending Email! booo!";
}

Related

XAMPP on windows 10 is not running php

Project structure is as follows
C:\xampp\htdocs\myProject\Documentation
C:\xampp\htdocs\myProject\HTML\css
C:\xampp\htdocs\myProject\HTML\images
C:\xampp\htdocs\myProject\HTML\js
C:\xampp\htdocs\myProject\HTML\videos
C:\xampp\htdocs\myProject\HTML\404.html
C:\xampp\htdocs\myProject\HTML\contact.php
C:\xampp\htdocs\myProject\HTML\index.html
C:\xampp\htdocs\myProject\PSD
I have a contact form in index.html that is controlled by a javascript file. This code stops default form submit, performs error checks and then uses ajax to make a post request to contact.php. The javascript code runs, it detects the php (see the alert in the code below just after the axjax funtion call. The value of d is the php script in the alert, but none of the debug lines in the php code get called and it never returns 'success'.
Here is the form
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email ID" name="email" id="email">
</div>
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Mobile Number" name="mobile" id="mobile">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
here is the javascript
// JavaScript Document
$(document).ready(function() {
$("#phpcontactform").submit(function(e) {
e.preventDefault();
var name = $("#name");
var email = $("#email");
var mobile = $("#mobile");
var msg = $("#message");
var flag = false;
if (name.val() == "") {
name.closest(".control-group").addClass("error");
name.focus();
flag = false;
return false;
} else {
name.closest(".control-group").removeClass("error").addClass("success");
} if (email.val() == "") {
email.closest(".control-group").addClass("error");
email.focus();
flag = false;
return false;
} else {
email.closest(".control-group").removeClass("error").addClass("success");
} if (msg.val() == "") {
msg.closest(".control-group").addClass("error");
msg.focus();
flag = false;
return false;
} else {
msg.closest(".control-group").removeClass("error").addClass("success");
flag = true;
}
var dataString = "name=" + name.val() + "&email=" + email.val() + "&mobile=" + mobile.val() + "&msg=" + msg.val();
$(".loading").fadeIn("slow").html("Loading...");
$.ajax({
type: "POST",
data: dataString,
url: "http://localhost/myProject/HTML/contact.php",
cache: false,
success: function (d) {
alert("d: "+d);
$(".control-group").removeClass("success");
if(d == 'success') // Message Sent? Show the 'Thank You' message and hide the form
$('.loading').fadeIn('slow').html('<font color="green">Mail sent Successfully.</font>').delay(3000).fadeOut('slow');
else
$('.loading').fadeIn('slow').html('<font color="red">Mail not sent.</font>').delay(3000).fadeOut('slow');
}
});
return false;
});
$("#reset").click(function () {
$(".control-group").removeClass("success").removeClass("error");
});
})
And finally here is the php
<?php
echo "<script>console.log('Debug Objects:' );</script>";
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$mobile = $_REQUEST["mobile"];
$msg = $_REQUEST["msg"];
echo "<script>";
echo "alert('this also works');";
echo "</script>";
$to = "myemail#gmail.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = $name."sent you a message via Raaga";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "From: ".$name."<br/> Email: ".$email ."<br/> Mobile: ".$mobile." <br/>Message: ".$msg;
echo "<script>alert('this maybe works');</script>";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo "<script>alert('name:'+$name);</script>";
echo 'failed';
}
}
echo "<script>alert('this finally works');</script>";
?>
I tried moving contact.php to the htdocs root but that didnt work. Have turned off all antivirus and firewalls but that didnt work either. Am at a loss. Thought php was supposed to work out of the box with xampp?
Okay so thanks to ADyson for the help. The issue was not that php isn't running, its that the mail server was not properly configured.

PHP #mail textarea $message not being sent

So this must look as another PHP email question. However, after researching and trying all available replies on Stackoverflow, I can't seem to get the <textarea name="description"></textarea> to get the content and send it with the rest of the email $to, $from, $body and $headers:
Here's the HTML:
<form method="post" id="tarqus_form">
<div class="inner-frame inverse p-7">
<h5>Hola,</h5>
<div>
<h5>mi nombre es</h5>
<input type="text" class="name" name="name" placeholder="Nombre (obligatorio)" id="name">
</div>
<div>
<h5>y me gustaría saber</h5>
</div>
<div>
<h5>sobre:</h5>
<input type="text" class="subject" name="subject" placeholder="Asunto" id="asunto">
</div>
<div>
<h5>Me pueden responder</h5>
</div>
<div>
<h5>a este:</h5>
<input type="text" class="email" name="email" placeholder="Correo electrónico (obligatorio)" id="email">
</div>
<div>
<h5>Quisiera decir:</h5>
</div>
<div>
<!-- HERE's the textarea -->
<textarea name="description" rows="3" class="message" placeholder="Ingresa tu mensaje (obligatorio)" id="message"></textarea>
</div>
<div class="row pt-21">
<div class="col-md-6 p-0">
<h5>Antispam:</h5>
<div>
<h5>¿12-7+2?</h5>
</div>
<div>
<input type="text" class="antispam pl-0 mt-7 ml-0" name="antispam" placeholder="Respuesta (obligatorio)" id="antispam">
</div>
</div>
<div class="col-md-6 text-center">
<input type="submit" value="Enviar" class="submit uppercase">
</div>
</div>
</div>
</form>
I'm creating some validations with jQuery and if they pass, the POST method is called. This hasn't been a problem because the email is actually being sent with everything I need except the message on the textarea.
jQuery validations:
$("#tarqus_form").submit(function(e){
e.preventDefault();
var name = $("#name").val();
var subject = $("#asunto").val();
var email = $("#email").val();
var text = $("#message").val();
var antispam = $("#antispam").val();
var dataString = 'email=' + email + '&text=' + text + '&subject=' + subject + '&name=' + name;
// Custom RegExp for verifying email authenticity
function isValidEmail(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0- \uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
if (isValidEmail(email) && (name.length > 1) && (text.length > 1) && (antispam == 7)){
$.ajax({
type: "POST",
url: "/form.php",
data: dataString,
success: function(response){
console.log(response);
$('#tarqus_form').fadeOut(500);
$('.success').removeClass('hidden').fadeIn(500);
}
});
} else{
$('.error').removeClass('hidden').fadeIn(1000);
setTimeout(function(){
$('.error').addClass('hidden');
}, 5000);
}
return false;
});
When this passes, the form sends the email according to this PHP code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['description'];
$from = 'TARQUS | Arquitectura MX <contacto#tarqus.mx>';
$headers.="From:".$from."\n";
$to = 'contacto#tarqus.mx';
$body = "Nombre: $name\n Asunto: $subject\n E-Mail: $email\n Mensaje: $message\n";
// detect & prevent header injections
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
#mail($to, $subject, $body, $headers);
?>
As you can see, I'm using the $_POST method for my name="description" textarea. I've also tried with the $_REQUEST method.
Here's an example email, all of the changes that I've made to the PHP code sent the email except for the $message in the $body:
Nombre: Name Lastname
Asunto: Subject
E-Mail: example#mail.com
Mensaje: === empty === :(
you got error on your php file
when you getting post DATA in ajax you sending TEXT not DESCRIPTION
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['text'];
$from = 'TARQUS | Arquitectura MX <contacto#tarqus.mx>';
$headers.="From:".$from."\n";

Correct way of sending mail with name and email in FROM

I am having trouble getting this to work I want the email to send to me as:
Persons Name <personsName#example.com>
I have been looking at other examples and I think I have everything right I can't find anything out of place in my code. The FROM in the email sent to me is FROM username#just122.justhost.com it is not showing up as either Persons Name or personsName#example.com unless I change the $header to
$header = 'From:' $email;
or
$header = 'From: $name;
Can anyone spot what I am doing wrong?
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$subject = "Message from Blog";
$header = 'From: ' . $name . '<'.$email.'>' . "\r\n";
mail("test#example.com", $subject, $message, $header);
?>
form
<label for="name">Name:</label><input class="inputField" type="text" size="30" id="name"/><div style="display: none;" id="error-name"><font color="#FF0000">Your name here!</font></div><br />
<label for="email">Email:</label><input class="inputField" type="text" size="30" id="email" /> <div style="display: none;" id="error-email"><font color="#FF0000">Your email here!</font></div><br />
<div style="display: none;" id="error-message"><font color="#FF0000">Your message here!</font></div>
<label for="message">Message:</label><textarea id="message" rows="9" cols="59"></textarea> <br />
<input type="button" id="submit-button" value="Send Message" onclick="sendEmail();"/>
<div id="messages">
<div style="display: none;" id="loading"> <font color="#FF0000">Sending..</font></div>
<div style="display: none;" id="message-sent"><font color="#008040">Message sent!</font></div>
<div style="display: none;" id="error-content"><font color="#FF0000">Please enter some content!</font></div>
</div>
<br />
</div>
Ajax
function sendEmail() {
$("#loading").fadeIn(100).show();
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var data = "name=" + name + "&email" + email + "&message=" + message;
if(name == "") {
$("#error-name").fadeIn(700).show();
$("#loading").fadeOut(100).hide();
} else if(email == "") {
$("#error-email").fadeIn(700).show();
$("#loading").fadeOut(100).hide();
} else if(message == "") {
$("#error-message").fadeIn(700).show();
$("#loading").fadeOut(100).hide();
} else {
$.ajax({
type: "POST",
url: "send.php",
data: data,
success: function() {
$("#loading").fadeOut(100).hide();
$('#message-sent').fadeIn(500).show();
}
});
}
}
You had a small typo in the Javascript:
var data = "name=" + name + "&email=" + email + "&message=" + message;
Notice the = at the end of &email=
However, I do want to mention that most hosting providers don't allow you to send emails from domains that aren't on your server. Fortunately this problem turned out to be a typo.

PHP/AJAX Mail Contact Form - part of the email message not showing up

Okay so I have a form that is using PHP to mail to an e-mail address. I also have a jQuery/AJAX section that validates the form without reloading the page and executes the PHP. For some reason the Product Interest value from the interest drop down is not coming through in the e-mail. What am I missing? I've been driving myself mad with this. Thanks!
This is the form:
<div class="contact-form">
<div class="row">
<div class="field"><input type="text" id="first" name="first" class="input" placeholder="First Name"></div>
<div class="field"><input type="text" id="last" name="last" class="input" placeholder="Last Name"></div>
<div class="field"><input type="text" id="telephone" name="telephone" class="input" placeholder="Phone Number"></div>
</div>
<div class="row">
<div class="field"><input type="email" id="email" name="email" class="input" placeholder="E-mail"></div>
<div class="field">
<select class="select" name="interest" id="interest">
<optgroup label="Countwise Products:">
<option value="I-Count">I-Count</option>
<option value="Q-Count">Q-Count</option>
<option value="D-Count">D-Count</option>
<option value="Z-Count">Z-Count</option>
</optgroup>
</select>
</div>
<input type="submit" value="Get Better Results!" class="button3 dark-blue submit"/>
<span class="error" style="display:none">All Fields Are Required!</span>
<span class="success" style="display:none">Contact Form Submitted Successfully</span>
</div>
</div>
</form>
This is the PHP for titled header_form_email.php
<?php
// POST Variables
//Name
$first = $_POST['first'];
$last = $_POST['last'];
//Phone
$telephone = $_POST['telephone'];
//E-mail
$email = $_POST['email'];
//Interest
$interest= $_POST['interest'];
// Contact subject
$subject = "CountWise Website - Sales Request";
$name = "$first" . " " . "$last";
$message = "You have received a new information request from Countwise.com\n".
"Name: " . "$name\n".
"Phone: " . "$telephone\n".
"Email: " . "$email\n".
"Product Interest: " . "$interest";
// From
$mailheader = "From: $email \r\n";
// Enter your email address
$to ="kelly#drastikdesigns.com";
$send_contact=mail($to,$subject,$message,$mailheader);
// Check, if message sent to your email
if($send_contact){
echo "<strong>We have received your information and will be in contact you shortly. Thank you.</strong>" ;
}
else {
echo "ERROR";
}
?>
This is the AJAX
$(function() {
$(".submit").click(function() {
var first = $("#first").val();
var last = $("#last").val();
var telephone = $("#telephone").val();
var email = $("#email").val();
var interest = $("#interest").val();
var dataString = 'first=' + first + '&last=' + last + '&telephone=' + telephone + '&email=' + email + '&interest' + interest;
if (first == '' || last == '' || telephone == '' || email == '') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else {
$.ajax({
type: "POST",
url: "http://www.drastikdesigns.com/clients/countwise/php/header_form_email.php",
data: dataString,
success: function() {
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
You forgot the equals:
'&interest=' + interest;
On a side note, why not just do:
data:$("#formid").serialize(),
That will save you tons of time and headache. Second point, using firebug/developers tools/chrome definitely helps when it comes to viewing the headers sent via ajax.

How to get "success" with jQuery AJAX email form

I have created a simple email form on a website - Processing it through AJAX using the jQuery library.
I know that the form is working because I am receiving the test email, but I will need for the user to know that their email has sent. Currently, there is no event happening upon success. It should be replacing the form (in div #contactform) with a success message. What am I missing here?
NOTE: I looked at the activity in Safari after submitting the form and it shows my process.php script, but it says "cancelled". Why is it doing this, and could it have something to do with why it is not showing a success message?
Here's my script:
HTML
<div id="contactform">
<form id="churchcontact" action="" method="POST">
<ul>
<li>
<label for="name">Name</label>
<span class="error error_name">Please enter your name</span>
<input id="name" type="text" name="name" />
</li>
<li>
<label for="email">Email</label>
<span class="error error_email">Please enter your email address</span>
<input id="email" type="text" name="email" />
</li>
<li>
<label for="message">Message</label>
<span class="error error_message">Please write a message</span>
<textarea id="message" name="message"></textarea>
</li>
<li>
<input class="formsubmit" type="submit" value="Send" />
<li>
</ul>
</form>
</div>
jQUERY
$(document).ready(function() {
////////////////////////////////////
//// form submit ////
////////////////////////////////////
$('#churchcontact').submit(function() {
////////////////////////////////////
//// validation ////
////////////////////////////////////
$('.error').hide();
//message
var message = $('textarea#message').val();
if(message == '') {
$('.error_message').show();
$('textarea#message').focus();
var errors = 'true';
}
//email
var email = $('input#email').val();
var atpos = email.indexOf("#");
var dotpos = email.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length || email == '') {
$('.error_email').show();
$('input#email').focus();
var errors = 'true';
}
//name
var name = $('input#name').val();
if(name == '') {
$('.error_name').show();
$('input#name').focus();
var errors = 'true';
}
if(errors) {
return false;
} else {
////////////////////////////////////
//// process ////
////////////////////////////////////
var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "sites/all/modules/custom/churchcontact/process.php",
data: dataString,
success: function() {
$('#contactform').html("success!");
}
});
}
return false;
});
});
PHP (process.php)
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['message'];
$recipient = '[my email]';
$subject = 'Website Test';
$message = '
Name: ' . $name
. ', Email: ' . $email
. ', Message: ' . $msg;
mail($recipient, $subject, $message, $headers);
in process.php add an echo of mail():
$res = mail($recipient, $subject, $message, $headers);
echo $res; //if $res returns 1 that means mail was sent
ajax success:
...
success: function(data) {
if(data == 1){
message = "success!";
}
else{
message = "Error";
}
$('#contactform').empty().html(message);
}

Categories