Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php发送电子邮件时出错_Php_Html_Email - Fatal编程技术网

使用php发送电子邮件时出错

使用php发送电子邮件时出错,php,html,email,Php,Html,Email,因此,有一个单一的表单,它只获取该人的电子邮件,并将其发送到我指定的电子邮件地址。但是我想我写代码的方式是错误的,因为我会出错。我见过其他stackoverflow问题,但我不确定这为什么不起作用。 这是html格式的表单 <form style="margin-bottom:50px;" name="contactform" action="contact-form-handler.php" class="news-letter "method="post"> <div

因此,有一个单一的表单,它只获取该人的电子邮件,并将其发送到我指定的电子邮件地址。但是我想我写代码的方式是错误的,因为我会出错。我见过其他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');
}

?>

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

第3行应为:

if (empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}
1.)您遇到的第一个问题是,您通过post从表单输入发送电子邮件地址,但您没有使用它

2.)收件人和发件人变量具有相同的电子邮件地址masnadhossain@live.com这造成了冲突

3.)检查电子邮件地址有效性的预匹配错误。我已经在下面重新编写了您的代码

如果您是通过表单输入向用户发送电子邮件(如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>";

 }
?> 

“因为我有错误。”请编辑您的问题以包含错误。@Epodax我刚收到一个服务器错误500@MasnadNihit:查找日志文件,或在php中启用php错误。ini@PierreEmmanuelLallemant问题是错误已经被启用了,但它似乎无法捕获错误。请使用
try{}catch(){}
并在此处报告错误,谢谢!!!我根本没有注意到!!!非常感谢!我解决了我的问题,我错过了第三行的if语句