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;
}
} ?>
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;
});
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;
?>
I probably didn't word the title too well.
My web form is working in every other way except when I receive a submission from the web form, it says the sender is Apache, when I would prefer it to say either the user's name or the name of the site the form has been submitted from. Here is my code:
<?php
if(empty($_POST['submit']))
{
echo "Form is not submitted!";
exit;
}
if(empty($_POST["name"]) ||
empty($_POST["email"]))
{
echo "Please complete required fields";
exit;
}
if(isset($_POST['submit'])){
$to = "ncrbrts#live.com";
$from = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$subject = "Form Submission";
$subject2 = "Copy of Your Form Submission";
$message = $_POST['comment'] . "\n " . "From:" . " " . $_POST['name'] .
"\n " . "Telephone:" . " " . $_POST['phone'] . "\n " . :Email:" . " " . $_POST
['email'];
$message2 = "Thank you for your enquiry." . "\n " .
"Here is a copy of your enquiry for your records: " . "\n " . $_POST['comment']
. "\n " . "A member of our team will contact you shortly to discuss your
requirements.";
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $headers, $message);
mail($from, $subject2, $message2, $headers2);
}
header('Location: thank-you.html');
?>
I have attempted to change the from variable to say anything else and then just posting the email address in the user's message so I could at least get the information that way. That broke it somewhat! Any help on this would be much appreciated :)
hi I am Unable to comment so writing in Answer section:
if(empty($_POST['submit']))
{
echo "Form is not submitted!";
exit;
what is this for.?
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";
I have created a contact form and I would like for the details be sent to my inbox once the submit button is clicked. Clicking the submit button opens up a new mail page (see getfreepack.org). I have been advised I need a PHP script. I copied one off the web but no too sure how to use it to my advantage. I would appreciate your help.
$Name= $_REQUEST["Name"];
$Email = $_REQUEST["Email"];
$Phone = $_REQUEST["Phone"];
$JobTitle = $_REQUEST["Jobtitle"];
$KeySkills = $_REQUEST["Keyskills"];
$Attachment = $_REQUEST["Attachment"];
$to = "my email address#mail.com";
$subject = "Contact Form";
$message = "Name: " . $Name $message .= "\Email: " . $Email: $message .= "\n Phone: " . $ Phone;
$message .= "\nSubject: " . $Subject; $message .= "\nCover Letter: " . $CoverLetter;
$message .= "\n Attachment: " . $Attachment; $message .= "\nIP Adress: " . $_SERVER['REMOTE_ADDR'];
$message .= "\nBrowser: " . $_SERVER['HTTP_USER_AGENT'];
$message .= "\n\nMessage: " . $theMessage;
$headers = "From: $theEmail";
$headers .= "\nReply-To: $theEmail";
$sentOk = mail($to,$subject,$message,$headers);
echo "sentOk=" . $sentOk;
A very basic one would be :
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
If you want to go one step more check out the manual : http://php.net/manual/en/function.mail.php
Cheers
You don't need the php to get a form to send to your email address make the form tag like this:
>>action="mailto:youremailaddress#whatever.com">
Input
Input