我正在测试PHP表单以在本地发送电子邮件。
我输入所有输入但是当我按下提交按钮时它总是返回" false"然后是错误消息。是因为我在本地工作而且没有任何邮件服务器,或者我的代码中有什么问题?
这里是代码:
<?php
if(isset($_POST['submit']))
{
if(empty($_POST['nome']) ||
empty($_POST['email']) ||
empty($_POST['motivo']) ||
empty($_POST['messaggio']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
// return false;
}
else
{
$nome = strip_tags(htmlspecialchars($_POST['nome']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$motivo = strip_tags(htmlspecialchars($_POST['motivo']));
$messaggio = strip_tags(htmlspecialchars($_POST['messaggio']));
// Create the email and send the message
$to = 'mirkocoppola80@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $nome";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio";
$headers = "From: mirkocoppola80@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
if (!@mail($to,$email_subject,$email_body,$headers))
{
// return true;
echo "<p>Email error</p>";
}
else
{
echo "<p>Email sent successfully!</p>";
}
}
}
?>
<form class="form-horizontal col-sm-6 col-sm-offset-3" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<div class="form-group">
<div class="row">
<label for="email" class="col-sm-12">Email</label>
</div>
<div class="row">
<div class="col-sm-12">
<input type="email" name="email" class="form-control" id="email" placeholder="Email">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="nome" class="col-sm-12">Nome</label>
</div>
<div class="row">
<div class="col-sm-12">
<input type="text" name="nome" class="form-control" id="nome" placeholder="Nome">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="motivo" class="col-sm-12">Motivo</label>
</div>
<div class="row">
<div class="col-sm-12">
<input type="text" name="motivo" class="form-control" id="motivo" placeholder="Motivo">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="messaggio" class="col-sm-12">Messaggio</label>
</div>
<div class="row">
<div class="col-sm-12">
<textarea class="form-control" name="messaggio" rows="5" id="messaggio" placeholder="Motivo">Inserisci il tuo messaggio...</textarea>
</div>
</div>
</div>
<div class="form-group">
<div class="">
<button type="submit" name="submit" class="btn btn-default">Invia</button>
</div>
</div>
</form>
任何帮助?
答案 0 :(得分:0)
是因为我在本地工作而且没有任何邮件服务器
是。
如果您没有运行本地邮件服务器,PHP的mail()
功能 将始终失败并返回false 。
This answer has some useful tips
您需要在计算机上设置邮件服务器才能使邮件功能正常工作。如果您使用的是Windows(我猜您来自使用WAMP),您可以设置Pegasus mail server。
其他选项包括使用包装类(如SwiftMailer或PHPMailer)并使用它们连接到另一个SMTP服务器,例如您的GMail帐户。即使您在自己的localhost路由上使用Pegasus邮件服务器,我仍然建议使用上面提到的两个类中的一个。它们为您提供了更大的灵活性,更安全。
连接到您的ISP SMTP服务器或GMail或其他最简单的路由。
要扩展上述内容,您还可以查看Mailhog或Mailcatcher以在本地捕获您的邮件并检查其内容。
答案 1 :(得分:0)
删除
中的“@”if(@mail($ to,$ email_subject,$ email_body,$ headers))
<?php
if(isset($_POST['submit']))
{
if(empty($_POST['nome']) ||
empty($_POST['email']) ||
empty($_POST['motivo']) ||
empty($_POST['messaggio']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
// return false;
}
else
{
$nome = strip_tags(htmlspecialchars($_POST['nome']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$motivo = strip_tags(htmlspecialchars($_POST['motivo']));
$messaggio = strip_tags(htmlspecialchars($_POST['messaggio']));
// Create the email and send the message
$to = 'mirkocoppola80@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $nome";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio";
$headers = "From: mirkocoppola80@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
if (mail($to,$email_subject,$email_body,$headers))
{
// return true;
echo "<p>Email error</p>";
}
else
{
echo "<p>Email sent successfully!</p>";
}
}
}
?>
它应该适用。