【问题标题】:phpMailer - Why is this not working?phpMailer - 为什么这不起作用?
【发布时间】:2023-03-16 11:39:01
【问题描述】:
<?php

session_start();

include 'sign_up.php';

$_SESSION['email'] = $_POST['email'];

if (isset($_POST['btn_signup'])) {

    $email = $mysqli->escape_string($_POST['email']);
    $email = trim($_POST['email']);
    $email = strip_tags($email);
    $email = htmlspecialchars($email);

    $name = $mysqli->escape_string($_POST['username']);
    $name = trim($_POST['username']);
    $name = strip_tags($name);
    $name = htmlspecialchars($name);

    $pass = $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
    $pass = trim($_POST['password']);
    $pass = strip_tags($pass);
    $pass = htmlspecialchars($pass);

    $hash = $mysqli->escape_string( md5( rand(0,1000) ) );

    $sql = "INSERT INTO users (email, username, password, hash) VALUES ('$email', '$name', '$pass', '$hash')";
    $result = $mysqli->query($sql);

    require_once './PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 1;

    $mail->isSMTP();                            // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';             // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                     // Enable SMTP authentication
    $mail->Username = 'myGmail@gmail.com'; // SMTP username
    $mail->Password = 'myPassword';            // SMTP password
    $mail->SMTPSecure = 'tls';                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                          // TCP port to connect to

    $mail->setFrom('myGmail@gmail.com', 'SpreAd Creator');
    $mail->addAddress($_POST['email']);   // Add a recipient
    //$mail->addReplyTo('info@gmail.com', 'Info');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    $mail->isHTML(true);  // Set email format to HTML

    $bodyContent = '<h1>How to Send Email using PHP in Localhost</h1>';
    $bodyContent .= '<p>This is the HTML email sent from localhost using PHP</p>';

    $mail->Subject = 'Email from Localhost';
    $mail->Body    = $bodyContent;

    header("location: other_page.php");
}?>

当用户单击提交按钮if (isset($_POST['btn_signup'])) {... 时,它应该向他的电子邮件发送确认。我不明白为什么它不发送它。我正在使用 phpMailer 而不是 mail() 甚至不起作用的函数。我是不是忘记了什么?

【问题讨论】:

  • 您缺少实际发送电子邮件的部分。请参阅 Niklesh 的回答。
  • escape_string(password_hash($_POST['password'] 顺便说一句,这是个坏主意。如果你想知道为什么,问我,我会告诉你为什么以及下面的其余代码。
  • 你也让自己对 sql 注入敞开大门。
  • @Fred-ii- 为什么这是个坏主意?
  • 因为,当123'\abc&amp;等有效密码被认为是可能的注入并转换为其他内容时,会导致问题,进而在转义时失败。使用password_hash()password_verify() 对它们进行散列和验证时要考虑到这一点。使用strip_tags()htmlspecialchars() 弊大于利。顺便说一句,对防范 sql 注入完全没有帮助;准备好的语句就足够了。

标签: php login smtp gmail phpmailer


【解决方案1】:

我想你在标题之前终于忘记了这一点

 if (!$mail->send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
  echo "Message sent!";
 }

同时检查垃圾邮件文件夹

【讨论】:

  • 那么你有一个不同的问题 - 阅读 PHPMailer 故障排除指南。
猜你喜欢
  • 1970-01-01
  • 2012-11-17
  • 2011-10-13
  • 2016-07-06
  • 2011-11-21
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多