0

对不起,伙计们,但我想我有一个小问题(很容易解决),但我自己没有得到解决方案。你能帮帮我吗?

我面临的问题是: - PHP 脚本(由 PHP 邮件程序处理)正在发送电子邮件,但不是人们在表单中填写的信息。

这是我使用的 php 脚本:

<?php
$to = "user@domain.com"; 
$subject = "Request";
$message = "<h1>Request</h1>";
$message .= "<strong>street</strong>: $_POST['street']";
$message .= "<strong>store</strong>: $_POST['store']";
$headers  = "From: request@domain.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$sent = mail($to, $subject, $message, $headers);
if( $sent = mail($to, $subject, $message, $headers) ){ echo "SENT"; } else { echo "There    
was a problem"; }
?>

提前致谢!

4

2 回答 2

2
<?php
$to = "user@domain.com"; 
$subject = "Request";
$message = "<h1>Request</h1>";
$message .= "<strong>street</strong>:".$_POST['street'];
$message .= "<strong>store</strong>:".$_POST['store'];
$headers  = "From: request@domain.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$sent = mail($to, $subject, $message, $headers);
if( $sent = mail($to, $subject, $message, $headers) ){ echo "SENT"; } else { echo "There    
was a problem"; }
?>
于 2013-09-17T11:49:11.727 回答
2

您的代码存在一些问题。

您的标头$headers .= "MIME-Version: 1.0\r\n";格式不正确并添加1.0 etc.From:因此导致1.0 <request@domain.commime-version>

此外,您的mail()函数被执行了两次,因此同时发送了 2 封电子邮件。

mail()我通过删除$sent = mail($to, $subject, $message, $headers);和修改if条件修复了您的标题,包括您的标题。

假设这是您用于我用来测试它的 HTML 表单的内容:

<form method="POST" action="mail_handler.php">
<p>
  <label>Street:<br>
    <input name="street" type="text">
  </label>
</p>
<p>
  <label>Store:<br>
    <input name="store" type="text">
  </label>
</p>

<input name="submit" type="submit" value="Submit" />
</form>

mail_handler.php

<?php
$to = "user@domain.com";
$subject = "Request";
$message = "<h1>Request</h1>";
$message .= "<strong>street</strong>:".$_POST['street'];
$message .= "<br>";
$message .= "<strong>store</strong>:".$_POST['store'];

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: user@domain.com" . "\r\n" .
            "Reply-To: user@domain.com" . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers) ){
echo "SENT"; } else { echo "There was a problem"; }
?>

有关标头和mail()函数的更多信息,请访问 PHP.net 网站,网址为:http: //php.net/manual/en/function.mail.php

于 2013-09-17T12:45:14.817 回答