Home  >  Q&A  >  body text

PHP mailer not working: no error log, message says sent but not received

I don't receive any error logs in my webserver's php error log. I've tried several different SMTP servers (other email providers I know work as well). This 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>

This is my mailfunction.php that calls my mailer function outside of the public html.

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

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

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

?>

This is my email function.

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

//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 PHPMailerPHPMailerPHPMailer();
            $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 were sent. What could be wrong here? Thanks.

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

P粉593118425P粉593118425367 days ago746

reply all(1)I'll reply

  • P粉030479054

    P粉0304790542023-11-11 10:28:42

    I don't find any problem with your code. This error may be caused by incorrect password settings. Well, the authorization procedure might be incorrect. When I encountered the same error, I determined that this was the problem and solved it. Can you try watching this video to resolve the issue? (Watching until 5:20 will be enough to solve your problem.)

    reply
    0
  • Cancelreply