Riesbeck
Riesbeck

Reputation: 15

php mail with sender

In this mailform everything things work except there is no sender and my mail is going straight to the trash! is it because theres no sender on this mailform? And I want to se the information from the sender so a wonder where do I fill in the code from sender?

<?php
/* Set e-mail recipient */
$myemail = "[email protected]";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */


$message = "

Someone has sent you a message from xxxxxxx.com:

Name: $name
Email: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: http://www.xxxxxxx.com/confirmation.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>

  <body>

    <p>Please correct the following error:</p>
    <strong><?php echo $myError; ?></strong>
    <p>Hit the back button and try again</p>

  </body>
</html>
<?php
exit();
}
?>

Upvotes: 1

Views: 939

Answers (1)

Will
Will

Reputation: 1933

You can add in the sender's email address using the "additional headers" argument to mail:

mail($myemail, $subject, $message, "From: [email protected]");

Edit: in your case, I think you need to pass in the $email variable defined earlier in your code. That will show the email as coming from the email address that was entered in the form.

mail($myemail, $subject, $message, "From: " . $email);

See: http://php.net/manual/en/function.mail.php

Upvotes: 2

Related Questions