Display users email in from field

the mail() function

Moderators: macek, egami, gesf

rashriaz
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Mar 09, 2012 6:39 am

Display users email in from field

Postby rashriaz » Fri Mar 09, 2012 6:44 am

Hi

I have a contact from which is working fine but when I receive an email from it, it displays from support@ukhosting.net (which is my hosting company). How can I get it so that it displays from the email address which is entered on the contact form.

My code is

Code: Select all

<?php
/*** ---------- CHANGE THIS TO YOUR EMAIL ADDRESS ------------- ***/
$MAIL_TO = myemail@mydomain.co.uk';

/*** ---------------------------------------------------------- ***/

$subject = "Web Site Contact Us :: GG";
$time = date('d-m-Y H:i');

$fname = $_POST['fname-field'];
$lname = $_POST['lname-field'];
$mail = $_POST['mail-field'];
$telephone = $_POST['telephone-field'];
$telephone2 = $_POST['telephone2-field'];
$company = $_POST['company-field'];
$message = $_POST['message-field'];

$name = $fname . ' ' . $lname;

$body = "Website Enquiry Sent: $time\n\n";
$body .= "Name: $name\nEmail: $mail\nTelephone: $telephone\nAlt Telephone: $telephone2\nCompany: $company\n";
$body .= "Message: $message\n";

@mail($MAIL_TO, $subject, $body);
echo 'sent!';
?>

rupali
New php-forum User
New php-forum User
Posts: 140
Joined: Thu Oct 13, 2011 10:32 pm
Location: Chandigarh, INDIA

Re: Display users email in from field

Postby rupali » Fri Mar 09, 2012 6:35 pm

hello,
you have missed the header section in the mail function, where we can mention the sender's email id
so add these lines to your code :

$headers = 'From: <sender's email id>' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

and also mention this variable in the mail function as :
mail($MAIL_TO, $subject, $body, $headers);


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 2 guests