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 ('info@mdlinvestmentsonline.com', $_POST['subject'], $_POST['body'], $_POST['from']);

?>

Link to comment
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      = '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);
?> 

Link to comment
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: 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";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.