PHPMailer 发送邮件到收件人邮箱

PHPMailer send email to receiver email

几天前,我开始着手工作中分配给我的第一个项目。今天,我在 PHPMailer 上遇到了一个错误。本质上,当它 应该发送使用 $_POST 获取的电子邮件时,它却没有。相反,它 将电子邮件发送到应该接收它的人(电子邮件)

这是我的代码。

index.php:

TOP PART:
 <?php include 'db.php';?>
 <?php include 'function.php';?>
 <?php 
    if(isset($_POST['subcon'])){
    
    $sender = new SendData();
    $sender->SendEmails($_POST['email'],$_POST['message']);
    
    }
 ?>

FORM PART:

    <div class="container-fluid">
        <main class="form-signin">
          <form method='post' action='index.php'>
            <h1 class="mb-3 contact-text">Contact Me</h1>
            <div class="form-floating mb-3">
              <input type="text" class="form-control" name='usrn' id="floatingPassword" placeholder="Username">
              <label for="floatingPassword" name='usrn' id='username-text'>Username</label>
            </div>
            <div class="form-floating mb-3">
              <input type="email" name='email' class="form-control" id="floatingInput" placeholder="name@example.com">
              <label for="floatingInput" id='email-text'>Email address</label>
            </div>
            <div class="md-form mb-3">
            <textarea id="form7" class="md-textarea form-control" name='message' rows="10" noresize </textarea>
            </div>
            <button class="w-100 btn btn-lg btn-text btn-outline-dark mb-2" name='subcon' type="submit">Sign in</button>
          </form>
         </main>
       </div>

db.php:

$server = 'localhost';
$user = 'root';
$pass = '';
$database = 'ipapp';

$conn = mysqli_connect($server,$user,$pass,$database);

if(!$conn){
    echo '<script>alert("Connection to the server has failed!");</script>';
}

function.php:

<?php include "db.php"; ?>
<?php 

/*
========================|
==>  Requires & Use  <==|
========================|
*/

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../third-party/vendor/autoload.php';


/*
========================|
=>      Classes      <==|
========================|
*/

class SendData{

    public function SendEmails(){
    
        /*
        ========================|
        ==> PUBLIC VARIABLES    |
        ========================|
        */
        $receiver ='gimmy.none@gmail.com';
        $sender = $_POST['email'];
        $username = $_POST['usrn'];
    
        $mail = new PHPMailer(true);
        try {
            $mail->SMTPDebug        = 1;                
            $mail->isSMTP();                                            
            $mail->Host             = 'smtp.googlemail.com';                     
            $mail->SMTPAuth         = true;                                   
            $mail->Username         = '';                     
            $mail->Password         = 'APP_PASSWORD';                               
            $mail->SMTPSecure       = "tls";
            $mail->Port             = 587;

            
            $mail->setFrom($sender);
            $mail->addAddress($receiver);     //Who is going to receive it


            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = 'Here is the subject';
            $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }
    
}

?>

我一直在尝试自己解决它,但我想我需要帮助。

您正在尝试 spoof/forge 电子邮件发件人,而 gmail 将忽略您。只是不要那样做。关注the examples provided with PHPMailer:发件人地址和收件人地址都使用自己的地址,回复地址使用提交者的地址:

$mail->addAddress($receiver);
$mail->setFrom($receiver, $_POST['username']);
$mail->addReplyTo($_POST['email']);

别忘了对此进行一些错误检查。