[英]I am trying to send an email to myself when an error happens from filling out the php form
I am trying to send an email to myself when an error happens from filling out the php form instead of doing Print="Sorry an error happened" 我尝试通过填写php表单发生错误时向自己发送电子邮件,而不是执行Print =“抱歉,发生错误”
$error = "ERROR HAPPENED".$order."\r\n";
$error_email = mail($to, $subject, $error, $headers2);
$mail = @mail($email, $subject, $confirmation, $headers2);
if ($mail) {
header("Location: http://www.test.com" );
} else ($error_email);
You're not using brackets after the else. 您没有在else之后使用方括号。
if($mail)
{
header("Location: http://www.test.com");
}
else
{
mail($to, $subject, $error, $headers2);
echo $error;
}
And you were never using echo $error so the error couldn't be printed. 而且您从未使用过echo $ error,因此无法打印错误。
Your code is a little unclear - and it helps if you ask a question... 您的代码有点不清楚-如果您提出问题,它会有所帮助...
However, I'd suggest using a logging framework for this, such as log4php ; 但是,我建议为此使用日志记录框架,例如log4php ; this includes an email option, so errors can get mailed to you, or you can write them to disk, or the OS log file.
其中包括一个电子邮件选项,因此错误可以通过电子邮件发送给您,或者您可以将其写入磁盘或操作系统日志文件中。
Please be aware that email is totally insecure - it's easy to intercept them, and they are not encrypted. 请注意,电子邮件是完全不安全的-容易拦截它们,并且未加密。 I hope you're not sending sensitive data in the $order variable.
希望您不要在$ order变量中发送敏感数据。
Finally, your code is probably not doing what you want it to. 最后,您的代码可能没有按照您想要的去做。
$error = "ERROR HAPPENED".$order."\r\n";
Build the error string (assuming $order is nicely printable). 构建错误字符串(假设$ order可以很好地打印)。
$error_email = mail($to, $subject, $error, $headers2);
Send an email to $to. 发送电子邮件至$ to。 $error_email is now a boolean value.
$ error_email现在是布尔值。
$mail = @mail($email, $subject, $confirmation, $headers2);
Send an email to $email. 发送电子邮件至$ email。 Suppress errors.
抑制错误。 $mail is a boolean.
$ mail是一个布尔值。
if ($mail) {
header("Location: http://www.test.com" );
if the mail was sent successfully (to $email), redirect the user. 如果邮件发送成功(发送到$ email),请重定向用户。
} else ($error_email);
If the mail was not sent successfully, not sure what you're trying to do... 如果邮件发送失败,则不确定您要执行的操作...
It's not a good idea to tie your application logic to the random "did I manage to send an email" success. 将您的应用程序逻辑与随机的“我设法发送电子邮件”成功绑定不是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.