20

I'm not even sure this is possible, however what I'm trying to do is as follows. I have an HTML form that generates and email using a PHP script. What I want is to recieve emails from this form to [email protected], then I want the from address to appear as one of the fields in the form.

I have had a look around and found some useful information on this site. I'm not sure whether sendmail_from can be used in this situation, or if it does what I'm asking.

Is this possible, if so how?

3 Answers 3

36

See this page on the same site (example 2): http://www.w3schools.com/php/func_mail_mail.asp

You will have to set the headers of the message to include the From and other stuff like CC or BCC:

<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]\r\n";

mail($to,$subject,$txt,$headers);
?>

Note that you have to separate the headers with a newline sequence "\r\n".

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

2 Comments

Just a note if it is a dynamic field from a form just replace [email protected] with a $variable also you can use this format also $username<$email>.
I'd of included the official manual on PHP's mail() php.net/manual/en/function.mail.php also/instead.
4

For a php mail script I have used this for contact forms:

$from = $_POST["from"];  //posted from the form.

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

I have not used a sendmail_from in my php before, only a simple variable.

I hope this helps!

2 Comments

You really should sanitize that $_POST["from"] variable before pass it to the mail() function. A malicious user could inject any number of other headers using your method.
Furthermore it will most likely end up in the spam folder on most mail servers systems since From does not match the sending domain. I would leave From set to a local email on the server and instead set a sanitized Reply-To header.
1

Any reason you cant do it in the email headers?

$to      = '[email protected]';
$subject = 'my subject';
$message = 'hello';
$headers = 'From: '.$email_from_form. "\r\n";

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

Comments

Your Answer

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