Jump to content

sender's email address


dianag

Recommended Posts

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 ('[email protected]', $_POST['subject'], $_POST['body'], $_POST['from']);

?>

Link to comment
https://forums.phpfreaks.com/topic/66913-senders-email-address/
Share on other sites

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      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Link to comment
https://forums.phpfreaks.com/topic/66913-senders-email-address/#findComment-335492
Share on other sites

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: [email protected]'

 

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 = '[email protected]';
$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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/66913-senders-email-address/#findComment-335740
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.