I have included my PHP code and hopefully someone can spot the error!
I have replaced my email for privacy and spam reasons.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contacting us. We will get back to you as soon as possible. '
);
$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 = 'test#email.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;
?>
Related
The messages I am receiving from my web form coming up without any sender's details. I am a beginner. Could someone guide me to get it to work. Here is the screenshot of the error and the php and js.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',`enter code here`
'message'=>'Email sent!'
);
$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 = 'myname#gmail.com';
$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;
?>
Validation
$('.contact-form').submit(function () {'use strict',
$this = $(this);
$.post("sendemail.php", $(".contact-form").serialize(),function(result){
if(result.type == 'success'){
$this.prev().text(result.message).fadeIn().delay(3000).fadeOut();
}
});
return false;
});
When ever I test this code it always fails can anyone help?
<?php if(isset($_POST['submit'])){
$to = "<<<___myEmail___>>>";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact Form: LewisDerbyshire.co.uk";
$subject2 = "Copy of your form submission : LewisDerbyshire.co.uk";
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$IP = "Senders IP :" . [REMOTE_ADDR];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$IP,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.";
if ($sent) {
$result = 'Thank you,' . $name . 'Your message has been sent.';
} } else {
$result = 'Sorry' . $name . ', there was a problem.'; } ?>
Also I have <?php echo $result; ?> next to my table but how do I stop it showing the message before anyone clicks submit.
Live view
By the first look, I see that you are missing the variable name in the line where you try to get the remote IP.
Instead of just [REMOTE_ADDR], try $_SERVER['REMOTE_ADDR'].
If it does not work after this fix, please post some error messages to make it easier to help you.
You missed your input code, so I tried to make one, hope this helps.
First, you closed if tag too early.
Second, variable $sent is not defined.
Third, my $sent variable is not clear yet...
<form method="POST">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="message" />
<input type="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) {
$to = "Your mail";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact Form: LewisDerbyshire.co.uk";
$subject2 = "Copy of your form submission : LewisDerbyshire.co.uk";
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$IP = "Senders IP :" . $_SERVER["REMOTE_ADDR"];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
$sent = mail($to, $subject, $message, $IP, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.<br/>";
if ($sent) {
$result = 'Thank you,' . $name . ' Your message has been sent.';
echo $result;
} else {
$result = 'Sorry' . $name . ', there was a problem.';
echo $result;
}
} ?>
Can anyone suggest corrections in these few line of code. I am very much disturbed that it is not working and time elapsing. Please help me with corrections or suggest a new one.
<?php
$email_to = 'info#khawabnama.com';
$subject = "Contact US";
$name =$_POST['flname'];
$email_from = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success=#mail($email_to, $subject, $body, $headers);
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
// $success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
//header("Location: index.php");
//header('Location: http://www.khawabnama.com/index.php');
//die;
I have set the <form action="sendemail.php">. The below code is written in the sendemail.php.
The problem is that I want the status message be popped up in the same window say contact.html. It should not open a new window which says:
"Thank you for contacting us. As early as possible we will contact you"
It comes in a new blank window with this message ^^^^.
header('Content-type: application/json');
$status = ("Thank you for contacting us. As early as possible we will contact you"
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$product_id = #trim(stripslashes($_POST['product_id']));
$phonenumber = #trim(stripslashes($_POST['phonenumber']));
$quantity = #trim(stripslashes($_POST['quantity']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'xyz#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Product Id: ' . $product_id . "\n\n" . 'Contact Number: ' . $phonenumber . "\n\n" .'Quantity: ' . $quantity . "\n\n" .'Subject: ' . $subject . "\n\n" . 'Complete Address: ' . $message ."\n\n";
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
I am not sure why you are doing jSON, but if jSON not required:
<?php
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$product_id = #trim(stripslashes($_POST['product_id']));
$phonenumber = #trim(stripslashes($_POST['phonenumber']));
$quantity = #trim(stripslashes($_POST['quantity']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'xyz#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Product Id: ' . $product_id . "\n\n" . 'Contact Number: ' . $phonenumber . "\n\n" .'Quantity: ' . $quantity . "\n\n" .'Subject: ' . $subject . "\n\n" . 'Complete Address: ' . $message ."\n\n";
// If main processes successfully, then continue with message
if(#mail($email_to, $subject, $body, 'From: <'.$email_from.'>')) { ?>
<!-- Just write normally -->
Thank you for contacting us. As early as possible we will contact you.
<?php die; } ?>
I want to add "This message came from etcetc.com" in the e-mail form in the e-mail body itself. Hope this make sense..
my sent_email.php
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
Like this:
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = "This message came from etcetc.com \r\n".$_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
All you need to do is add your text to the end of $message.
Change:
$message = $_POST['message'];
to
$message = $_POST['message'] . "\n\nThis message came from etcetc.com";
Add message before and after as you wish by
$message = "This is before posted message\r\n";
$message .= $_POST["message"];
$message .= "\r\nthis is after the posted message";