2011-05-18 48 views
1

联系表格工作得很好,但我不知道如何设置“回复邮件”。 PHP代码如下:PHP表单 - 问题与回复电子邮件

<?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: $email\nMessage: $message\n", 
"From: $name <[email protected]>"); 
?> 

我试图做的是取代“[email protected]”与$电子邮件,但由于某种原因,它崩溃,从不发送任何东西。

+0

当它“崩溃”是一个错误产生? – David 2011-05-18 18:33:11

+0

可能因为您没有为'mail()正确指定参数而失败请参阅文档:http://us.php.net/manual/en/function.mail.php – 2011-05-18 18:35:37

回答

4

它只是您在邮件标题块中缺少的Reply-to: [email protected]标题?此外,看起来你错过了mail()函数的第一个参数,它应该是它发送到的地址。

Reply-to标头添加到mail()的第三个参数中。

// Send Message 
mail($to_address, "Message from $name", 
    // Message 
    "Name: $name\nEmail: $email\nMessage: $message\n", 
    // Additional headers 
    "From: $name <[email protected]>\r\nReply-to: [email protected]" 
); 

编辑我错过了这个问题一个逗号,并认为整个块是消息,包括名称&。上面编辑。我看到你已经有一个头块。

+0

我会试一试。 – Renan 2011-05-18 18:38:41

+0

您应该用'\ r \ n'分隔标题。 – dtbarne 2011-05-18 18:39:12

+0

我做了更改,并用$ email替换了[email protected],但仍在回复字段中显示<[email protected]>。 – Renan 2011-05-18 18:45:07

0

您没有使用邮件功能的正确参数。看看在documentation

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

在你的情况,这将是:

mail($to, 
$subject, 
$message, 
"From: $name <[email protected]>"); 

假设你给了它一个$来(这是指谁发送电子邮件)和$主题(电子邮件的主题)。

0

把这个片断:

<?php 
    //define the receiver of the email 
    $to = '[email protected]'; 
    //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: [email protected]\r\nReply-To: [email protected]"; 
    //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"; 
    ?> 

在代码中,你错过了第一个参数,女巫应该是谁。