PHP SMTP邮件脚本无法正常工作

时间:2018-06-16 10:55:05

标签: php mysql

我正在编写一个php smtp邮件脚本,以便在用户丢失密码时向其发送邮件。然而,它不起作用。

处理器始终返回:

  

无法发送消息......

我已尝试使用Gmail smtp并获得相同的结果。

你能帮忙吗?有两个文件,表单和处理器。

以下是代码:

形式:

<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'>
<link rel = 'stylesheet' href = 'style.css' type = 'text/css'>
<title>Reset Password</title>
</head>
<?php
$db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
$sql = $db->query('select id from posts order by id desc');
$row = $sql->fetch(PDO::FETCH_ASSOC);
echo "<a href = 'browse.php?page=".$row['id']."'>Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = 'index.php'>Home</a>";
echo "<hr>";
echo '<form name = "forgot-pass" action = "reset-pass-notif.php" method = "post">
<label><em>Input your registered email address</em></label><br>
<input type = "text" name = "email"><br>
<em>Enter Image Text:</em><img src="captcha.php" /><br />
<input name="captcha" type="text" /><br>
<input type = "submit" name = "submit" value = "Send Reset-Password Link">
</form>
<hr>
<a href = "browse.php?page='.$row["id"].'">Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = "index.php">Home</a>';
?>

</html>

处理器:

<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'>
<link rel = 'stylesheet' href = 'style.css' type = 'text/css'>
<title>Reset Password Notification</title>
</head>
<?php
error_reporting(E_ALL);
session_start();
$db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
$sql0 = $db->query('select id from posts order by id desc limit 1');
$row0 = $sql0->fetch(PDO::FETCH_ASSOC);
echo '<a href = "browse.php?page='.$row0['id'].'">Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = "index.php">Home</a>';
echo '<hr>';
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$sql = $db->prepare('select * from users where email = :email');
$sql->bindParam(':email', $email);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$row = $sql->fetch();
$username = $row['username'];
$user_check = $row['user_hash'];
$subject = 'Reset your Password';
$link = '<a href = "http://example.com/reset-pass-land.php?username='.$row['username'].'&user_check='.$row['user_hash'].'">http://bquotes.xyz/reset-pass-land.php?username='.$row['username'].'&user_check='.$row['user_hash'].'</a>';
$message = 'Reset your password here: '.$link.' If you cannot click on the link, copy it and paste in your browser address bar';
$sql = $db->prepare('select email from users where email = :email');
$sql->bindParam(':email', $email);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$row = $sql->fetch();

//require_once ('class.phpmailer.php');
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'mail.domain.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@domain.com';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
//$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                  // TCP port to connect to
$mail->setFrom('user@domain.com', 'BQuotes Webmaster');
//$mail->addAddress($email);     // Add a recipient
/*$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); */   // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body    = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

/*if(!$mail->send()) {
    //echo '<em>Message could not be sent. Mailer Error:</em> ', "<em>", $mail->ErrorInfo, "</em>";
      echo '<span class = "red"><em>Message could not be sent. You have one or more invalid fields.</em></span>';
} else {
    echo '<span class = "green"><em>Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.</em></span>';
}*/

if (($email)&&($username)&&!empty($_POST['captcha'])&&($_POST['captcha']==$_SESSION['code'])&&($mail->send())){
echo '<span class = "green"><em>Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.</em></span>';
}

else {echo '<span class = "red"><em>Message could not be sent. You have one or more invalid fields.</em></span>';
}

echo "<hr>";
echo '<a href = "browse.php?page='.$row0['id'].'">Browse</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href = "index.php">Home</a>';
?>
</html>

谢谢!

3 个答案:

答案 0 :(得分:1)

PHPMailer的正确设置:

<?php

			$mail = new PHPMailer;
			$mail->IsSMTP();
			$mail->SMTPAuth = true;
			$mail->Username = "user@domain.com";
			$mail->Password = "xxxx";
			$mail->SMTPSecure = "ssl";  
			$mail->Host = "smtp.domain.com";
			$mail->Port = "465";
			$mail->setFrom('from@domain.com', 'John Doe');
			$mail->addReplyTo('from@domain.com', 'John Doe');
			$mail->AddAddress("mailto@otherdomain.com", "Jane Smith");
			$mail->CharSet = 'UTF-8';
			$mail->IsHTML(true);
			$mail->Subject = 'Bla bla or from DB';
			$mail->Body    = 'Here come the content...';
			if (!$mail->send()) {
				echo 'Error: ' . $mail->ErrorInfo;
				exit;
			}

?>

答案 1 :(得分:1)

请使用此代码。确保您已从邮件帐户中获得了要发送邮件的许可。

我的帐户 - &gt;登录&amp; security-&gt;允许安全性较低的应用:关闭

        $mail->CharSet = 'UTF-8';
        $mail->SMTPDebug = false;                                 
        $mail->isSMTP();                                      
        $mail->Host = 'smtp.live.com';                         
        $mail->SMTPAuth = true;                               
        $mail->Username = 'mail@gmail.com';                 
        $mail->Password = 'Password';                           
        $mail->SMTPSecure = 'tls';                            
        $mail->Port = 587;                                    
        $mail->setFrom($details['from'], $details['from']);
        if(is_array($details['to'])){
            foreach ($details['to'] as $key => $value) {
                $mail->addAddress($value['email'], $value['name']);
            }
        }else{
            $mail->addAddress($details['to'], isset($details['name'])?:$details['to']);
        }

        $mail->isHTML(true);
        $mail->Subject =$details['subject'];
        $mail->Body    =$details['body'];
        $mail->send();

答案 2 :(得分:0)

你在localhost上运行吗?尝试推出防病毒软件一段时间。防病毒软件往往会阻止从localhost发送电子邮件