如何在此PHP表单中添加SMTP?

时间:2016-08-28 14:53:26

标签: php smtp phpmailer password-recovery

拥有PHPmailer 库。这是一个密码提醒代码,当您将电子邮件添加到输入时,它会将您忘记的密码发送到您的电子邮箱。此代码仅通过php邮件发送,但由于我的主机不允许,只有选项才能使用SMTP,对吧?所以这是php代码的形式:

   <div class="tab-pane fade <?php
if ($tab == 'ForgotPass') {
    echo 'active in';
}
?>" id="forgotpass">
                            <form role="form2" action="index?tab=ForgotPass" method="POST">
                                <fieldset>
<?php
@$_SESSION['email'] = $_POST['email'];
?>
                                    <hr class="colorgraph">
                                    <div class="form-group">
                                        <input type="email" name="email" maxlength="64" id="email" class="form-control input-lg" placeholder="El. paštas" value="<?= $_SESSION['email'] ?>" required>

                                    </div>
                                    <div class="row">
                                        <div class="col-xs-6 col-sm-6 col-md-6">

                                            <input type="submit" name="forgot" class="btn btn-lg btn-success btn-block" value="send">
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
<?php


if (isset($_POST['forgot'])) {
    $email = $_POST['email'];   

    if (empty($email)) {
        echo errorbox("Please enter email.");

    } else {
        $check = mysql_query("SELECT * FROM users WHERE email='$email'");
        $row2  = mysql_fetch_assoc($check);
        if (mysql_num_rows($check) > 0) {
            $query   = mysql_query("SELECT * FROM settings");
            $row     = mysql_fetch_assoc($query);
            $subject = 'Forgot Password - ' . $row['title'] . '';
            $message = '
                    <center>
                    <a href="' . $row['url'] . '" title="Visit ' . $row['url'] . '" target="_blank">
                    <h1><img src="' . $row['url'] . '' . $row['logo'] . '" title="' . $row['title'] . '"> ' . $row['title'] . '</h1>
                    </a><br />

                    <b>Registration details:</b><br />
                    Username: ' . $row2['username'] . '<b></b><br />
                    Password: ' . $row2['password'] . '<b></b><br />
                    </center>
                    ';
            $headers = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
            $headers .= 'To: ' . $email . ' <' . $email . '>' . "\r\n";
            $headers .= 'From: ' . $row['email'] . ' <' . $row['email'] . '>' . "\r\n";
            @mail($to, $subject, $message, $headers);
            echo okbox("We have just sent you your forgotten data from your account on the entered E-Mail Address");
        } else {
            echo errorbox("There is no player with such email.");

      }
    }
}
?>

我也有这个 SMTP 配置的代码,它只是发送一条消息来测试电子邮件,我已经测试过,它有效,它确实发送了消息,现在我必须以某种方式使它成为它将密码发送到用户电子邮件:

<?php

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->Host = "mail.website.com";

$mail->SMTPAuth = true;
//$mail->SMTPSecure = "ssl";
$mail->Port = 25;
$mail->Username = "support@website.com";
$mail->Password = "emailpass";

$mail->From = "support@website.com";
$mail->FromName = "Test User";
$mail->AddAddress("emailthatgetsmessage@gmail.com");
//$mail->AddReplyTo("mail@mail.com");

$mail->IsHTML(true);

$mail->Subject = "Test message from server via SMTP Authentication";
$mail->Body = "Test Mail sent via SMTP Authentication";
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "Message has been sent";

?>

0 个答案:

没有答案