PHPMailer 发送电子邮件至收件人电子邮件

时间:2021-04-01 00:01:41

标签: php phpmailer

几天前,我开始着手我在工作中分配的第一个项目。今天,我在 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}";
        }
    }
    
}

?>

我一直试图自己解决这个问题,但我想我需要帮助。

1 个答案:

答案 0 :(得分:1)

您试图欺骗/伪造电子邮件发件人,而 gmail 只会忽略您。不要那样做。按照the examples provided with PHPMailer:使用您自己的地址作为发件人和收件人地址,并使用提交者的地址作为回复地址:

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

不要忘记对此进行一些错误检查。