通过电子邮件发送PHP脚本的表单

通过电子邮件发送PHP脚本的表单,php,forms,server-side,Php,Forms,Server Side,我在网上找到了这个简单的脚本,但它不起作用。我错过了什么 <?php if(!isset($_POST['submit'])) { //This page should not be accessed directly. Need to submit the form. echo "error; you need to submit the form!"; } $name = $_POST['name']; $visitor_email = $_POST['email'];

我在网上找到了这个简单的脚本,但它不起作用。我错过了什么

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];


$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message".

$to = "myemail@domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');
} catch(Exception $e){
    //problem, redirect to sorry page
    header('Location: sorry.html');
}       
?> 

我总是被重定向到感谢页面,但我没有收到任何电子邮件。
所有HTML内容都正确。

如果您在本地主机上,则无法发送电子邮件。只有当您的代码联机时,才会发送信息

您可以尝试一种更简单的方法,称为PHPmailer。 举个例子,

  require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
回显“已发送消息!”; }

Php mailer通过调用方法和属性使编码器变得更容易,而且将图像嵌入邮件也非常容易


但是,如果您在本地主机上,则无法发送电子邮件。

您是否检查了电子邮件中的垃圾邮件过滤器?

假设您有一个类似于以下表单的命名字段:(输入字段必须命名)

点应该是分号
例如:

$email_body = "You have received a new message from $name.\n".
"\n\n $message";
在测试中,我键入的消息直到变为分号后才通过

这一行
echo“错误;您需要提交表单!”
应该是
die()
指令才能停止执行

例如:
die(“错误,您需要提交表单”)
或者您可以使用
退出在您的
echo

例如:

echo "error; you need to submit the form!";
exit;
PHP(handler.PHP) 使用上面所示的表格在我这边进行测试和工作

<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
// echo "Error. You need to submit the form.";
// exit;
    die("Error. You need to submit the form.");
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message";

$to = "myemail@domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
// header('Location: thank-you.html');

echo "Success"; // My echo test

} catch(Exception $e){
    //problem, redirect to sorry page
// header('Location: sorry.html');

echo "Sorry"; // My echo test
}       
?>


这应该是一条评论。如果您没有使用知名的电子邮件服务发送邮件,接收端的电子邮件服务提供商可能会阻止它(对不起,我不知道正确的术语)。我曾经有过这个问题。将邮件发送到某个政府帐户不起作用,但将其发送到G-mail起了作用。我正在发送到gmail,同时我也输入了一个gmail地址作为发件人。@MinaHany:我想知道gmail是否愚蠢到可以说“发件人”whoever@gmail.com?必须合法!别介意它不是源于我们的服务器是的,我确实意识到了这一点,而且我不在本地主机上。请告诉我,当
mail()
无法发送邮件时,这个PHPMailer将如何发送邮件?它只是一个替代方案,值得尝试!您仍然可以直接访问该文件,唯一要做的是
echo
出错,但其余的将被处理。做一个
die('msg')
以确保无法访问脚本。并将
if
作为
if(!$\u POST)
谢谢!我很惊讶用点而不是分号没有编译错误。我将此标记为已接受,但就我而言,我认为我使用的(免费)主机不支持使用来自PHP的sendmail,因此它仍然不适用于我。@MinaHany非常欢迎您。大约两周前,另一个人也有类似的问题,他们的免费主机没有提供
mail()
@MinaHany。我建议你调查一下他们提供的
mail
,我将其用于测试目的。它们是免费的,而且还提供MySQLi/PDO,这也是我使用它们的另一个原因。谢谢。我会调查的!
echo "error; you need to submit the form!";
exit;
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
// echo "Error. You need to submit the form.";
// exit;
    die("Error. You need to submit the form.");
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message";

$to = "myemail@domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
// header('Location: thank-you.html');

echo "Success"; // My echo test

} catch(Exception $e){
    //problem, redirect to sorry page
// header('Location: sorry.html');

echo "Sorry"; // My echo test
}       
?>