If I use the following code, then the form works fine and I am getting form submissions to my email.


if($_POST["submit"]) {
    $subject="New request";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $mailBody = "<b>Name:</b> $name_title $sender\n
                 <b>Email:</b> $senderEmail\n\n
                 <b>Message:</b> $message";

    $mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

if ($mail_sent) {
    <p>Mail sent</p>
<?php } ?>

If I change the $mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"); TO $mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>", $headers);, then the form stops working. What is the issue?

  • This will break in WordPress anyway, because you have to use wp_mail() to give plugins for server setups a chance. – fuxia Sep 2 '14 at 11:24

From: is just another mail header. You need to concatenate this with your current $headers and pass it as one argument to mail():

$headers  = "From: $sender <$senderEmail>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

// la de da

mail( $recipient, $subject, $mailBody, $headers );
  • any how it says "unknown sender" tinypic.com/r/2h869g6/8 – Riffaz Starr Sep 2 '14 at 12:32
  • how can I change that to sender name? – Riffaz Starr Sep 2 '14 at 12:36
  • This seems like a server config issue - PHP mail is notoriously variable. You'll have much better luck @ StackOverflow or ServerFault. – TheDeadMedic Sep 2 '14 at 12:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.