【问题标题】:Sending email error using php [duplicate]使用php发送电子邮件错误[重复]
【发布时间】:2015-12-28 07:45:52
【问题描述】:

所以有一个表格,它只获取此人的电子邮件并将其发送到我分配的电子邮件地址。但是我想我以错误的方式编写了代码,因为我遇到了错误。我见过其他 stackoverflow 问题,但我不确定为什么这不起作用。 这是html中的表格。

<form style="margin-bottom:50px;" name="contactform" action="contact-form-handler.php" class="news-letter "method="post">
  <div class="subscribe-hide">
    <input type="text" name="email" class="form-control"  placeholder="Email Address"  >
    <button  type="submit"  class="btn"><i class="fa fa-envelope"></i></button>
  </div><!-- /.subscribe-hide -->
</form><!-- /.news-letter -->

这是另一个文件中的 PHP 代码。

    <?php
$errors = '';
$myemail = 'masnadhossain@live.com';
   empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}

$email_address = $_POST['email'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission";
$email_body = "You have received a new message. ".
" Here are the details:".
"Email: $email_address\n ";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}

?>

我得到的错误是服务器错误 500

【问题讨论】:

  • “因为我遇到了错误。”请编辑您的问题以包含错误。
  • @Epodax 我刚刚收到服务器错误 500
  • @Masnad Nihit : 找到日志文件,或者在 php.ini 中启用 php 错误
  • @PierreEmmanuelLallemant 问题是错误已经启用,但似乎无法捕获错误。
  • 使用try {} catch () {}php.net/manual/en/language.exceptions.php并在此报错

标签: php html email


【解决方案1】:

您的代码有语法错误请检查。

第 3 行应该是:

if (empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}

【讨论】:

  • 谢谢!!!我根本没有注意到!!!
【解决方案2】:

1.) 您遇到的第一个问题是您通过邮寄从表单输入中发送电子邮件地址,但您没有使用它。

2.) To 和 From 变量具有相同的电子邮件地址 masnadhossain@live.com,这会造成冲突

3.) 检查电子邮件地址有效性的 Preg_Match 错误。我在下面重写了你的代码。

如果您从表单输入中发布用户电子邮件(例如 gmail、yahoomail 等),此代码将助您一臂之力。

如果您想在代码中初始化电子邮件,只需更改

$email_address = strip_tags($_POST['email']);

可能是

$email_address = 'user1@gmail.com';

最后,From 变量必须是指向您网站地址的电子邮件地址。在这种情况下,我认为是 masnadhossain@live.com

此代码正在运行。如果能解决您的问题,请标记为正确答案....

<?php


//Users email address coming from form input eg. gmail,yahoomail etc.

$email_address = strip_tags($_POST['email']);

//validate the email address

$email_val= filter_var($email_address, FILTER_VALIDATE_EMAIL);
if (!$email_val){
echo "<font color=red><b>Invalid Email Address</b></font>";
exit();
}



$to=$email_val;
$subject = "Contact form submission";
$message = "Here is the message;

// set the from variable to any email pointing to your website

$from = "masnadhossain@live.com";
$headers = "From:" . $from;
$sent=mail($to,$subject,$message,$headers);
 if($sent)  {

print "<br><font color=green><b>Your mail was sent Successfully</b></font>";
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');

 } else  {
print "<br><font color=orange><b>We encountered an error sending your mail.</b></font>";

 }
?> 

【讨论】:

  • 非常感谢!我发现了我的问题,我错过了第 3 行中的 if 语句
猜你喜欢
  • 2015-12-10
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多