简体   繁体   中英

PHP Mailer not working. No error logs, message says it gets sent, but never received

I receive no error logs in my php error log in my web server. I have tried multiple different SMTP servers (other email providers I know work). Here is my HTML form:

<form action="/mailfunction.php" method="post" id="contact-form">
      <div class="nameInput">
      <input id="boxes" type="text" id="fname" name="name" value="" class="formFormL" placeholder="Name" maxlength="50"></input>
      </div>
      <div class="emailInput">
      <input id="boxes" type="text" id="lname" name="email" value="" class="formFormR" placeholder="Email" maxlength="50"></input>
      </div>

      <div class="messageInput">
      <textarea id="boxes" id="fname" name="message" value="" class="formFormM" placeholder="Message" maxlength="1000"></textarea>
      </div>
      <div style="padding: 5px;">
      <button type="submit" value="Send" class="up" name="submit">Send</button>
      </div>
    </form>

Here is my mailfunction.php which calls my mailer function outside of public html.

<?php 
require '[REDACTED]/mailer.php';

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

mails($name, $email, $message);

?>

Here is my mailing function.

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

function mails($name, $email, $message) {

    if(isset($_POST['submit'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];

        try{
            $mail = new \PHPMailer\PHPMailer\PHPMailer();
            $mail->isSMTP();
            $mail->Host = 'smtp.gmail.com';
            $mail->SMTPAuth   = true; 
            $mail->Username   = '[REDACTED]'; 
            $mail->Password   = '[REDACTED]'; 
            $mail->SMTPSecure = 'SSL'; 
            $mail->Port       = 465;  

            $mail->setFrom('[REDACTED]');
            $mail->addAddress('[REDACTED]');

            $mail->isHTML(true);                                  
            $mail->Subject = 'Message Received (Contact Page)';
            $mail->Body    = '<h3>Name : $name <br>Email: $email <br>Message : $message</h3>';
            $mail->send();
            echo 'Message has been sent';
            } catch (Exception $e) {
                echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            }
    }
}

?>

No messages are sent through. What could be the issue here? Thank you.

ERROR: 2022-07-07 01:16:57 SMTP ERROR: Failed to connect to server: Connection refused (111) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I don't see a problem in your code. The error may be due to the incorrectly set password. Well, authorization procedures may be incorrect. When I got the same error, I determined that this was the problem and solved it. Can you try to fix the problem by watching this video. (Watching until 5:20 will be enough for your problem.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM