通过mail()函数从本地服务器发送电子邮件


Sending emails from local sever by mail() function

我正试图从xampp服务器发送电子邮件与下面的php代码。Php代码似乎工作得很好,因为它给出了所有的成功和错误消息,但我没有收到任何邮件的电子邮件我提供的形式。

<?php
if(isset($_POST['submit'])){
$email  = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {// Validate email address
    $message =  "Invalid email address please type a valid email!";
}
else
{
    $query = $con->prepare("SELECT username FROM members where email=:email");
    $query->execute(array(':email'=>$email));
    $row = $query->fetch(PDO::FETCH_ASSOC);
    if($query->rowCount() == 1) {
        $encrypt = md5(1290*3+$row['username']);
        $message = "Your password reset link has been send to your e-mail address. Check your mail and click on the link therein.";
        $to=$email;
        $subject="Password Recovery";
        $from = 'mydemo.com';
        $body='Hi, <br/> <br/>Your username is '.$row['username'].' <br><br>Click here to reset your password http://localhost/mydemo/reset.php?encrypt='.$encrypt.'   <br/> <br/>--<br>';
        $headers = "From: " . strip_tags($from) . "'r'n";
        $headers .= "Reply-To: ". strip_tags($from) . "'r'n";
        $headers .= "MIME-Version: 1.0'r'n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
        mail($to,$subject,$body,$headers);
    }
    else
    {
        $message = "Account not found please enter email you provided during sign up!";
    }
    }
    }
<form action="" method="POST">
<legend>Forgot your password!</legend>
<input type="email" name="email" id="email" required="required" maxlength="35" placeholder="Enter your email here..."/>
<input type="submit" name="submit" id="submit" value="Submit"/>
</form>
<div id="message"><?php echo $message; ?></div>

我还对C:'xampp'php'php.inic:'xampp'sendmail'sendmail.ini进行了以下更改,实际上我有两个php.ini开发和生产。我修改了php-ini-development

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = gmailid@gmail.com
sendmail_path = "'"C:'xampp'sendmail'sendmail.exe'" -t"
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=gmailid@gmail.com
auth_password=gmailpassword
force_sender=my-gmailid@gmail.com
if (!mail($to,$subject,$body,$headers)) {
   /*Here you can log error*/
   print_r(error_get_last());
}

你不知道它是否工作正常,因为你没有检查函数mail()的结果。添加一个if

if (false === mail($to,$subject,$body,$headers)) {
   $message = "Mail was not sent";
}
从手册:

如果邮件被成功接收,返回TRUE,否则返回FALSE。

重要的是要注意,仅仅因为邮件被接受交付,并不意味着邮件实际上会到达预定的目的地。