83ingD33p
83ingD33p

Reputation: 15

How do I include sender's name and not just the email address while emailing message from user input form

I am trying to take inputs from a form on my website using a simple PHP script as given below:

<?php 
$toemail = '[email protected]';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
    echo 'Your email was sent successfully.';
} else {
    echo 'There was a problem sending your email.';
}
?>

This worked perfectly. Well, almost. The email I receive does not include the sender's name. I would want to see a normal email response (with name of the sender in the name column of inbox and I should be able to see the email address of the sender when I open the mail.)

So I made a few changes after looking up some threads like this:

<?php 

$toemail = '[email protected]';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$header = 'From: '.$email_from;


if(mail($toemail, 'Subject', $message, $header)) {
    echo 'Your email was sent successfully.';
} else {


echo 'There was a problem sending your email.';
    }

?>

Email response is still same. What do I need to change ?

Upvotes: 0

Views: 1407

Answers (2)

Jouke
Jouke

Reputation: 469

<?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);
?>

Quoted from http://php.net/manual/en/function.mail.php

Upvotes: 0

PalDev
PalDev

Reputation: 564

Try this format, note the spaces and the \r\n's, change your variables accordingly:

$email_headers  = 'MIME-Version: 1.0' . "\r\n";
$email_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_headers .='From: YOURNAME <[email protected]>' . "\r\n";
$email_headers .='Reply-To: [email protected]' . "\r\n";

mail($recipient_address, $email_subject, $email_body, $email_headers);

Upvotes: 1

Related Questions