This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I have a form which upon submission directs to a aspx page, which uses the form's data. Before directing to the above stated page, jquery is used alongside ajax to retrieve form values, and it sends everything to my php email file, which should be sending me informational emails whenever a form is submitted. Unfortunately I recevie nothing, everything else works, but I don't receive any email. Please help, I'm not sure why this isn't working.
Additional Info: I'm using wordpress and have called the jquery file via the functions.php. The php file is called via the jquery file.
Thanks in Advance!
HTML Form
<form action="https://redirection_page.aspx" method="get" name="nform" id="nform">
<div class="submission">
<div class="fname">
<input type="text" name="firstName" class="fname" placeholder="First name" required="">
</div>
<br>
<div class="lname">
<input type="text" name="lastName" class="lname" placeholder="Last name" required="">
</div>
<br>
<div class="email">
<input type="email" name="email" class="email" placeholder="example#email.com" required="">
</div>
<br>
<div class="phone">
<input type="text" name="homePhone" class="phone" placeholder="Phone Number" required="">
</div>
<br>
<br>
<!-- Edit the Continue Text -->
<div class="form-button">
<input type='button' class="submit tp-button green big :hover.green" value="Continue">
</div>
</div>
</form>
Jquery/Ajax
jQuery(document).ready(function($) {
var nform = $('#nform');
$('#nform .form-button input.submit').click(function(){
var data = {
first : $("#nform input.fname").val(),
last : $("#nform input.lname").val(),
email : $("#nform input.email").val(),
phone : $("#nform input.phone").val(),
};
$.ajax({
type: "POST",
url: "/wp-content/themes/Avada/email.php",
data: data,
success: function(){
$('#nform').submit();
}
});
});
});
PHP Mail
<?php
if($_POST){
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$sendto = "myemailaddress#gmail.com";
$message = "";
$message .= "<p> Name: " . $first . " " . $last "</p>";
$message .= "<p> Email: " . $email . "</p>";
$message .= "<p> Phone Number: " . $phone . "</p>";
$mail = 'no-reply#'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid = md5(uniqid(time()));
$headers = 'From: '.$mail."\r\n";
$headers .= 'Reply-to: '.$mail."\r\n";
$headers .= 'Return-Path: '.$mail."\r\n";
$headers .= 'Message-ID: <'.$uniqid.'#'.$_SERVER['SERVER_NAME'].">\r\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\r\n";
$headers .= 'X-Priority: 3'."\r\n";
$headers .= 'X-MSMail-Priority: Normal'."\r\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\r\n";
$headers .= '------------'.$uniqid."\r\n";
$headers .= 'Content-transfer-encoding: 7bit';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email
mail($sendto, "Apply Submission by " .$email, $message, $headers);
}
?>
Try to make any feedback from mail.php, then success will fired:
...
//send email
mail($sendto, "Apply Submission by " .$email, $message, $headers);
header("Content-Type:text/html");
echo("ok");
}
?>
Try this:
$.ajax({
type: "POST",
async: false,
url: "/wp-content/themes/Avada/email.php",
data: data,
success: function(){
window.location = "https://redirection_page.aspx/"
}
});
PHP Mail
if(mail($sendto, "Apply Submission by " .$email, $message, $headers)){
echo 'true';
}
Related
I have a simple form which sending an email with some fields and also it saves the fields in .csv file.
Here is my code (HTML, jQuery, PHP)
The Form:
<form id="request-size">
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
<input type="hidden" name="size" id="size" value="52">
<input type="hidden" name="sku" id="sku" value="315">
<input type="hidden" name="pridpr" id="pridpr" value="849">
<input class="request-size-btn btn" value="SEND">
</form>
The jQuery:
jQuery('.request-size-btn').live('click', function() {
jQuery.ajax({
type: "POST",
url: "https://example.com/sendmail.php",
data: jQuery('form#request-size').serializeArray(),
success: function( data ) {
jQuery('.cmesg').empty();
jQuery('.cmesg').append(data);
}
});
});
});
The PHP Sendmail:
$EmailFrom = $_REQUEST['email'];
$SKU = $_REQUEST['sku'];
$SIZE = $_REQUEST['size'];
$ID = $_REQUEST['pridpr'];
$EmailTo = "info#example.com";
$Subject = "Contact for Size";
$DATENOW = date('d/m/Y h:i:s a', time());
$Body = "";
$Body .= "<b>Email:</b> ";
$Body .= $EmailFrom . "\r\n";
$Body .= "<br>";
$Body .= "<b>Product SKU:</b> ";
$Body .= $SKU . "\n";
$Body .= "<br>";
$Body .= "<b>Product ID:</b> ";
$Body .= $ID . "\n";
$Body .= "<br>";
$Body .= "<b>Product Size:</b> ";
$Body .= $SIZE;
$Body .= "<br>";
$Body .= "<b>Date:</b> ";
$Body .= $DATENOW;
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\n";
$headers .= "From: ". $EmailFrom ."" . "\n";
$success = mail($EmailTo, $Subject, $Body, $headers);
if ($success){
echo '<div class="alert alert-success">Done!</div>';
$file = fopen('request-size.csv', 'a');
$data = array(
array($EmailFrom, $SKU, $ID, $SIZE, $DATENOW)
);
foreach ($data as $row)
{
fputcsv($file, $row);
}
fclose($file);
}
else{
echo '<div class="alert alert-danger">Error!</div>';
}
I've got all the fields in my csv file and email, except the "email" field. It's always empty. I have also try to change the name of email field (email to emailreq) on both sides (html, php), but didn't work.
Any suggestions? Thank you!
If updating jQuery is an option for you, do that. Version 1.6.2 and up should be working (possibly also before, but i'm not sure).
Otherwise change the
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
to
<input name="email" id="email" type="text" class="cmesgmail" placeholder="email">
Older versions of jQuery did not include support for the HTML5 input types.
Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.
Im trying to send an e-mail with the following script I've made. But seem to encounter a weird problem that I need help with.
The mail script
// Get field values.
$name = strip_tags($_POST["name"]);
$email = strip_tags($_POST["email"]);
$message = $_POST["msg"];
// Check if e-mail address is valid.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set e-mail and subject.
$to = "mail#mydomain.dk";
$subject = "You have a new message.";
// Set header values.
$headers = "From: " . $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Set request body.
$message = "<html>";
$message .= "<body>";
$message .= "<p><b>From:</b><br>" . $name . "</p>";
$message .= "<p><b>Email:</b><br>" . $email . "</p>";
$message .= "<p><b>Message:</b><br>" . $message . "</p>";
$message .= "</body>";
$message .= "</html>";
mail($to, $subject, $message, $headers);
echo "Your email was sent!";
} else {
echo "Invalid Email, please provide an correct email.";
}
The HTML
<form id="contact-form" data-toggle="validator" data-disable="true" role="form">
<div class="form-group">
<label for="name">Navn</label>
<input type="text" name="name" id="contact-name" class="form-control" data-minlength="2" data-error="Please provide a valid name." required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" name="email" id="contact-email" class="form-control" data-minlength="5" data-error="Please provide a valid e-mail address." required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="message">Your message:</label>
<textarea name="msg" id="contact-email" data-minlength="10" data-error="Your message must be at least 10 characters long." class="form-control" required></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<button id="submit" value="send" class="btn btn-primary">Send</button>
<div id="success"></div>
</div>
</form>
The Javascript
$(document).ready(function(){
$('#success').css('display', 'none');
$('#submit').click(function(e) {
e.preventDefault();
$.ajax({
url: "php/form.php",
data: $("#contact-form").serialize(),
type: 'POST',
statusCode: {
500: function(data) {
$('#success').css('display', 'none');
$('#success').css('color', '#A94442');
$('#success').html('Your message was not sent.');
$('#success').fadeIn(200);
},
404: function(data) {
$('#success').css('display', 'none');
$('#success').css('color', '#A94442');
$('#success').html('Your message was not sent.');
$('#success').fadeIn(200);
},
200: function(data) {
console.log(data);
$('#success').css('display', 'none');
$('#success').css('color', '#74C274');
$('#success').html('Your message was sent.');
$('#success').fadeIn(200);
}
}
});
});
});
The e-mail is sent and received, but the textarea is not getting sent through, and it seems to sent the "email" and "name" field twice in the message body.
The e-mail output looks like this:
From:
Someone
Email:
someone#someone.com
Besked:
From:
Someone
Email:
someone#someone.com
Help will be very much appreciated. Have been trying to fix this for hours now.
The error is located here :
$message .= "<p><b>Message:</b><br>" . $message . "</p>";
You are using the same variable for the message to be sent and the message received by your PHP.
This code will be working :
// Get field values.
$name = strip_tags($_POST["name"]);
$email = strip_tags($_POST["email"]);
$message_text = $_POST["msg"];
// Check if e-mail address is valid.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set e-mail and subject.
$to = "mail#mydomain.dk";
$subject = "You have a new message.";
// Set header values.
$headers = "From: " . $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Set request body.
$message = "<html>";
$message .= "<body>";
$message .= "<p><b>From:</b><br>" . $name . "</p>";
$message .= "<p><b>Email:</b><br>" . $email . "</p>";
$message .= "<p><b>Message:</b><br>" . $message_text . "</p>";
$message .= "</body>";
$message .= "</html>";
mail($to, $subject, $message, $headers);
echo "Your email was sent!";
} else {
echo "Invalid Email, please provide an correct email.";
}
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Recently, I have purchased a domain and hosting. I've also created a webpage having contact form in it. I want when the user wrote something in textarea of contact form and clicks submit then that message is sended to my gmail account and so that i can reply also to those messages. I've also used this script to do so but its not working.
This is sendemail.php file
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'raunakhajela#gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
This is my contact form code in index.php
<div class="contact" id="contact">
<div class="container-3x">
<div class="block-header"> <!-- Blocks Header -->
<h2 class="title">Contact Us</h2>
</div>
<div class="block-content"> <!-- Blocks Content -->
<form action="sendemailphp" method="post" class="trigger-form-animate">
<input type="text" value="" id="name" name="name" placeholder="Name *" />
<input type="text" value="" id="email" name="email" placeholder="Email *" />
<input type="text" value="" id="website" name="website" placeholder="Website *" />
<textarea type="text" value="" id="message" name="message" rows="3" placeholder="Your Message *" ></textarea>
<div class="clr"></div>
<button>Submit Message</button>
</form>
</div>
</div>
</div>
I've also tried "http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html" this tutorial but its not working. Unable to find "Send through Gmail (easier to set up)" this option in gmail on adding another accounts windoww.
How can i do that?? Plzz hlp
You don't have a named subject form element which may explain why mail is failing and may very well be sent to Spam instead.
For example: <input type="text" name="subject">. Either add one of replace.
You will however need to make sure that this is filled out using a conditional statement
(see further below for an example).
$subject = #trim(stripslashes($_POST['subject']));
with
$subject = "Form submission";
You could also add this instead to your form:
<input type="hidden" name="subject" value="Form submission">
Either this or what I've outlined just above will work.
Many Email clients will either send mail to Spam or reject it altogether if a "subject" is missing, which is clearly missing from your code.
Checking if subject has been filled example:
if(isset($_POST['subject']) && !empty($_POST['subject'])){
// do someting
}
try this,both will work
<?php
$to = "usermailid#gmail.com";
$subject = "Welcome to";
$message = " Hi $username,<br /><br />
Thank you for signing up with us.<br />
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
if($mail)
{
$to = "admin#gmail.com";
$subject = "Following Customer Signed Up";
$message = " $username,Customer is signed up with us,<br /><br />
Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
Phone:$phone<br/>Zip Code:$zip<br/>Message:$message_cust<br/><br/><br/>
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
}
?>
i have this code in my contact page.
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/main.js"></script>
<script>
jQuery(document).ready(function() {
$("#submit").click(function()
{
var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*#[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
//var pn = /^(\+91-|\+91|0)?\d{10}$/;
var email = $("#email").val();
var message = $("#message").val();
var subject = $("#subject").val();
var name = $("#name").val();
if(name=="")
{
//$('#empty1').show(1).delay(5000).fadeOut();
$('#name').focus();
return false;
}
else if(message=="")
{
//$('#empty4').show(1).delay(5000).fadeOut();
$('#message').focus();
return false;
}
else if(subject=="")
{
//$('#empty4').show(1).delay(5000).fadeOut();
$('#subject').focus();
return false;
}
else if(!(pattern.test(email)))
{
//$('#error2').show(1);
$('#email').focus();
}
else if(email=="")
{
//$('#empty2').show(1).delay(5000).fadeOut();
$('#email').focus();
return false;
}
else
{
var dataString = 'name='+ name + '&email=' + email + '&message=' + message + '&subject=' + subject;
$.ajax({
type: "POST",
url: "mail.php",
data: dataString,
success: function(){
//$('.success').show('slide').delay(5000).fadeOut();
$("#contact-form")[0].reset();
alert("Your Detail Is Submitted, We Will Connect With You Soon.");
}
});
}
return false;
});
});
and i have form like that.
<form class="b-form b-contact-form m-contact-form" action="" style="margin-bottom: 10px;" id="contact-form">
<div class="input-wrap">
<i class="icon-user"></i>
<input class="field-name" type="text" placeholder="Name (required)" name="name" id="name">
</div>
<div class="input-wrap">
<i class="icon-envelope"></i>
<input class="field-email" type="text" placeholder="Email (required)" name="email" id="email">
</div>
<div class="input-wrap">
<i class="icon-pencil"></i>
<input class="field-subject" type="text" placeholder="Subject" name="suject" id="subject">
</div>
<div class="textarea-wrap">
<i class="icon-pencil"></i>
<textarea class="field-comments" placeholder="Message" name="message" id="message"></textarea>
</div>
<input class="btn-submit btn colored" type="submit" value="Submit Comment" id="submit" name="submit">
</form>
and also i have ajax mail.php page.
<?php
$to = "demo#example.com";
$subject = $_REQUEST["subject"];
$message = "message=".$_REQUEST["message"]."<br />";
$message .= "name=".$_REQUEST["name"]."<br />";
$message .= "email=".$_REQUEST["email"];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From:" .$_REQUEST["email"]. "\r\n";
mail($to,$subject,$message,$headers);
?>
i done servers mail forwarding setting, and also i check the ajax call, and what data will it parsing. using console apnel.
but i cant reached the mail in my email id.
please help me, what can i do now?
Here, give this a try.
Your message variables were not properly formatted and I modified your headers a bit.
This worked for me, minus your jQuery method.
<?php
$to = "demo#example.com";
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$message .= "" . "<br/>";
$message .= "name= $name" . "<br/>";
$message .= "email= $email" . "<br/>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(mail($to,$subject,$message,$headers)){
echo "Success!!";
}
else {
echo "Sorry.";
}
?>