1

Can someone please tell me how to fix this script so the form info is sent to the correct email (i.e., my email....lets assume my email is [email protected]). I would like the email to show the user's email addrress who sent to to me as well.

<?php
$name = $_POST['fldName'];
$email = $_POST['fldEmail'];
$phone = $_POST['fldPhone'];
$comments = $_POST['fldComments'];
$isFormValid = false; 

if (strlen($name) && strlen($email) && strlen($phone) && strlen($comments)) $isFormValid = true; 

if ($isFormValid) 

{ 


$to      = '[email protected]'; 
$subject = 'Contact Us Form Comments'; 

$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Comments: '.$comments; 

$headers = 'From: [email protected]' . "\r\n" 
         . 'Reply-To: [email protected]' . "\r\n"; 

mail($to, $subject, $message, $headers);

header("location: thankyou.html");

} 
else 
{ 

echo "Please fill in the required fields"; 

} 

?>
0

3 Answers 3

1

instead of using native PHP mail() to do everything by yourself, it is better off you use some useful library like swift mailer, which makes it very easier for you to send mail without having to worry about the inner workings.

for example to send a mail in swift mailer all you need to do is.

$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password');
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself');
// Send the message
$result = $mailer->send($message);

you can find more information about swift mailer in there official website http://swiftmailer.org/

Sign up to request clarification or add additional context in comments.

Comments

1

This $headers = 'From: [email protected]' . "\r\n" will display who sent you the email. It's already there in your script. You don't need to add anything extra

1 Comment

That email that is currently there is ours. I need it to be the person who filled out the form. So how do i add their email address from the form?
0

Change out the $headers variable to use the sender's email address instead of your own.

3 Comments

I dont know the user's email address, as its real time when they are filling out the form. Shouldn't it be a vaiable or something?
maybe?????? but where do i put it? In the code I posted in my question, how can i be sure that the email i receive has the person's email address in the correct spot (NOT MINE), so when i reply to the email, the email has their address in the TO field, and not mine
See where you put your email address in the From and Reply-To? Just substitute those with the sender's address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.