dianag Posted August 27, 2007 Share Posted August 27, 2007 Hello I'm VERY new to all of this. I have a site on GoDaddy and have made a contact form using php/html. It's working great, however the customer's email address shows up in the body and the "from address" is some automatic address generated in the process. How can I have the customer's email address be the from address when receiving their request? Here is the code from the 2 pages involved. Thanks in advance for your help. FORM CODE <form action="messagesent.php" method="post" name="frm_message" id="frm_message" lang="en"> <table width="500" border="0" cellspacing="0" cellpadding="5"> <tr> <td>Your Email Address: <input type="text" name="from" id="from" size="45" maxlength="50"/> <br /><br /> </td> </tr> <tr> <td>Subject: <input type="text" name="subject" id="subject" size="55" maxlength="50"/> <br /></td> </tr> </table> <br /> <textarea name="body" cols="50" rows="10" wrap="VIRTUAL" id="body"> Type your message here. </textarea> <br /> <br /> <input type="submit" onclick="MM_validateForm('emailaddress','','RisEmail','subject','','R','body','','R');return document.MM_returnValue" value="submit" /> </form> MESSAGE SENT PAGE <? mail ('info@mdlinvestmentsonline.com', $_POST['subject'], $_POST['body'], $_POST['from']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66913-senders-email-address/ Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 Have a closer look at the mail() function: http://no.php.net/manual/en/function.mail.php bool mail ( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters]] ) It allows you to specify additional headers which can include "reply to", "from" ect. Example 1108. Sending mail with extra headers. The addition of basic headers, telling the MUA the From and Reply-To addresses: <?php $to = 'nobody@example.com'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66913-senders-email-address/#findComment-335492 Share on other sites More sharing options...
dianag Posted August 27, 2007 Author Share Posted August 27, 2007 Thanks, I guess I need to do some background reading, the manual info makes little sense to me. But I will try. Thanks a ton for your time and the references. Quote Link to comment https://forums.phpfreaks.com/topic/66913-senders-email-address/#findComment-335710 Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 All there is to it is the option to specify additional headers. Two of the headers has their own dedicated argument in the mail() function - these are the to and subject header. They are passed directly as a function argument - mail($to, $subject, $message). Additional headers, which includes from, reply-to and many more, are passed as the 4th function argument. The From-header looks like this: 'From: some@email.com' If you want to specify more than one additional header, for example both from and reply-to they are separated by "\r\n". But in your case you can just use something like this: <?php $to = 'info@mdlinvestmentsonline.com'; $subject = $_POST['subject']; $body = $_POST['body']; //Make sure to validate the $_POST['from'] to avoid problems like exploits or just malfunction of your script $headers = 'From: $_POST['from']'; //Store the return value of the mail()-function as $sendmail $sendmail = mail($to, $subject, $body, $headers); //Check if $sendmail is true which means success or false which means failure if($sendmail) { echo "Mail sent successfully"; } else { echo "Failed to send mail"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66913-senders-email-address/#findComment-335740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.