1

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

  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Tangentially Perpendicular Jul 06 '22 at 18:48
  • `SMTPSecure` should be `ssl`, lower case. Set `SMTPDebug = 2` and post the output in your question. – Synchro Jul 06 '22 at 20:29
  • `$mail = new PHPMailer(true)` is useless since it's outside the function and never gets used. – ADyson Jul 06 '22 at 21:17

1 Answers1

0

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.)

emrez
  • 316
  • 2
  • 11
  • Google no longer supports this. https://support.google.com/accounts/answer/6010255?hl=en – throwaway2908457 Jul 07 '22 at 04:06
  • Yes, that's why I posted the video. Now it is necessary to provide integration as in the video. You need to create it from **Security -> Signing in to Google -> App Passwords.** If the **App Passwords** section is not visible, you have to turn on 2-step verification. – emrez Jul 07 '22 at 16:41
  • I did this and still got the same error. – throwaway2908457 Jul 08 '22 at 04:30