开发者

PHP Form - Problem with reply e-mail

The contact form is working just fine but I can't figure how to setup the "reply mail". The PHP code is as follows:

<?php
// Get Data 
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);

// Send Message
mail( "Message from $name",
"Name: $name\nEmail: $em开发者_StackOverflow社区ail\nMessage: $message\n",
"From: $name <forms@example.net>" );
?>

What I tried to do is replace "forms@example.com" with $email but for some reason it crashes and never sends anything.


Is it just the Reply-to: reply@example.com header you're missing in your mail headers block? Also, looks like you're missing the first parameter to the mail() function, which should be the address it's sent to.

Add the Reply-to header into the third parameter to mail().

// Send Message
mail($to_address, "Message from $name",
  // Message
  "Name: $name\nEmail: $email\nMessage: $message\n",
  // Additional headers
  "From: $name <forms@example.net>\r\nReply-to: reply@example.com"
);

EDIT I missed a comma in the question and thought the whole block was the message, including name & from. Edited above. I see you already had a header block.


You aren't using the correct parameters for the mail function. Take a look at the documentation

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

In your case, it would be:

mail( $to,
$subject,
$message,
"From: $name <forms@example.net>" );

Assuming that you gave it a $to (which denotes who to send the email to) and a $subject (the subject of the email).


Take this snippet:

 <?php
    //define the receiver of the email
    $to = 'youraddress@example.com';
    //define the subject of the email
    $subject = 'Test email';
    //define the message to be sent. Each line should be separated with \n
    $message = "Hello World!\n\nThis is my first mail.";
    //define the headers we want passed. Note that they are separated with \r\n
    $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
    //send the email
    $mail_sent = @mail( $to, $subject, $message, $headers );
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent ? "Mail sent" : "Mail failed";
    ?>

In your code, you missed the first argument, witch should be to who.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜