0

I try to implement PHPMailer on our school project system and i don't have idea how it work, just follow it from tutorial, but it can send email and it not also resulting error.

Please help my find the reason why this is not working on me and solve the error.

this is my code from tutorial:

<?php 
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//include ('includes/connection.php');
require 'PHPMailer/vendor/autoload.php';

$email  = 'recipientemail@gmail.com';


/****************START EMAIL************/
$subject = "Try System";
$message = "<strong>Try:</strong> try try<br>
            success success success!";
//Email start
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = true;                                       // Enable verbose debug output
    //$mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'puvonlinesystem@gmail.com';                     // SMTP username
    $mail->Password   = 'puv12345';  
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;                             // SMTP password
    $mail->SMTPSecure = 'tls';//'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;//587;                                    // TCP port to connect to
    
    //Recipients
    $mail->setFrom('myemail@gmail.com');
    $mail->addAddress($email);
    //$mail->addCC('example@gmail.com');
    

    // Attachments
    //$mail->addStringAttachment($pdf,'resconfirmation.pdf');
   // Add attachments
   

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->SMTPOptions = array(
                                'ssl' => array(
                                    'verify_peer' => false,
                                    'verify_peer_name' => false,
                                    'allow_self_signed' => true
                                )
                            );
    $mail->send();

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

//Email stop
/****************STOP EMAIL************/
?>

And this is the result on my page:

Sending with mail()
Sendmail path: /usr/sbin/sendmail -t -i
Envelope sender: myemail@gmail.com
To: recipientemail@gmail.com
Subject: Try System
Headers: Date: Thu, 29 Apr 2021 04:43:38 -0400From: puvonlinesystem@gmail.comMessage-ID: <X2yzXw5J0mmkEBVXPtwf92LmQtV8vixdTVdDczcZag@puvonlinesystem.ml>X-Mailer: PHPMailer 6.3.0 (https://github.com/PHPMailer/PHPMailer)MIME-Version: 1.0Content-Type: text/html; charset=iso-8859-1
Result: true
  • 3
    It appears to have sent the email to the local mailserver ok. But that doesn't guarantee it will be received. The big answer here: [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) covers pretty much everything that could possibly go wrong, and how to investigate it. Personally if you're wanting to use Gmail as the From address then you'd be better to send via Gmail's smtp servers rather than a local mailserver, it's far less likely to end up looking like spam – ADyson Apr 29 '21 at 08:51
  • Or you could use the Gmail web API (and thus avoid getting entangled in any problems related to email protocols at all) – ADyson Apr 29 '21 at 08:55
  • Don't disable TLS certificate verification, especially if you can't explain why you think you need to. – Synchro Apr 29 '21 at 11:29
  • P.s. just noticed something - you're going to need to uncomment the `$mail->isSMTP()` line before it will use any of those SMTP settings you've provided. Right now it's trying to use the local sendmail, as shown by the debug information – ADyson Apr 29 '21 at 21:15

0 Answers0