PHP mail()不发送(Hostinger)

时间:2016-11-24 18:29:32

标签: php email

所以我正在进行注册/登录/你知道我在说什么系统,我需要验证用户的电子邮件地址,以防他们忘记密码。为此,我为他们分配了一个随机的PIN,通过电子邮件发送给他们。然后,他们会在帐户激活页面中输入该PIN,并且他们的帐户会变为有效状态。

现在,我的问题是电子邮件不发送。 我在Hostinger上使用PHP 5.5,我的MX记录没有设置,但是从我读过的关于这个的问题来看,并不是导致问题的原因。这是代码:

<?php
// connect to database, I'll omit the details because no one cares

$pin=rand(1000,9999);
$email=$_POST['email'];
$rawUser=$_POST['user'];
$user=// hash username but I'm not gonna tell you how because safety reasons
$rawPassA=$_POST['pass1'];
$rawPassB=$_POST['pass2'];
if($rawPassA==$rawPassB){
    // hash password, again I'm not gonna tell you how
} else{
    echo "<script type='text/javascript'> 
        window.location.href = 'http://komodokrew.ml/error/login/pass_no_match';
        </script>";
};
$first=$_POST['first'];

$checkForUserQuery='SELECT count(*) FROM tableNameThatImNotGonnaTellYouBecauseSqlInjection WHERE user = \'' . $user . '\';';
$checkForUser=mysql_query($checkForUserQuery);
$checkForUserA=mysql_fetch_row($checkForUser);
$checkForUserF=$checkForUserA[0];

if($checkForUserF==0){
    $query='INSERT INTO tableNameThatImNotGonnaTellYouBecauseSqlInjection (`user`,`first_name`,`pin`,`password`,`email`,`active`) VALUES (\'' . $user . '\',\'' . $first . '\',' . $pin . ',\'' . $pass . '\',\'' . $email . '\',1);';
    mysql_query($query);

    $subject='KomoDoKrew - account activation';
    $message='You have recently registered an account on KomoDoKrew, under the username ' . $rawUser . '. Please activate your account by visiting komodokrew.ml/activate and entering the following PIN code, your username, and your password. PIN code: ' . $pin . '. Thank you for registering an account with KomoDoKrew! Make sure to join the community at komodokrew.ml/forum.';
            $from='admin@komodokrew.ml';
            $headers='From:' . $from;
    mail($email,$subject,$message,$headers);

    echo "<script type='text/javascript'> 
        window.location.href = 'http://komodokrew.ml/success/login/register';
        </script>";
} else{
    echo "<script type='text/javascript'> 
        window.location.href = 'http://komodokrew.ml/error/login/user_exists';
        </script>";
};
mysql_close();
?>

另外,是的,我知道我很容易受到SQL注入,我正在研究预备语句和PDO,是的我知道mysql_ *已经过时了,我正在研究新的PDO,好的?但即使他们用SQL注入了我,它会在注册之后重定向它们,并且密码和用户名会被变化的盐进行数十万次的散列,所以我现在不会太担心。

这是在php_mail.log中发送的典型日志:

[23-Nov-2016 22:20:28 UTC] mail() on [/home/u964873932/public_html/register/register.php:40]: To: <myEmailAddressThatImNotGonnaTellYou> -- Headers: From:admin@komodokrew.ml

PHP无法获取POST不是问题,因为用户的电子邮件地址已成功输入数据库。

谢谢!

1 个答案:

答案 0 :(得分:1)

我建议先做两件事: 1.启用错误日志:

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

2。并更改发送电子邮件的行

$status = mail($email,$subject,$message,$headers);

 if($status)
   { 
    echo '<p>Your mail has been sent!</p>';
    } else { 
     echo '<p>Something went wrong, Please try again!</p>'; 
  }

然后看看如果没有错误你是否有任何错误然后按照这个答案:PHP mail form doesn't complete sending e-mail