Search code examples
phpwordpressemailphpmailerplesk

using phpmailer with plesk smtp


I'm trying to setup PHP mailer on my VPS with Plesk SMTP.

The PHP code

require("path_to_phpmailer");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);                            
try {
    //Server settings
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 2; 
    $mail->CharSet  = 'UTF-8';
    $mail->Host = 'localhost';
    $mail->Port = 25;
    $mail->SMTPSecure = 'tls'; 
    $mail->SMTPOptions = array (
    'ssl' => array(
        'verify_peer'  => true,
        'verify_depth' => 3,
        'allow_self_signed' => true,
        'peer_name' => 'Plesk',
        )
    );
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'somepassword';
    $mail->setFrom('[email protected]', 'Name Surname');
    $mail->addAddress('[email protected]', 'Name Surname');
    $mail->Subject = 'Email subject';
    $mail->msgHTML('Email content with <strong>html</strong>');
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }

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

[email protected] is the email that i created in my ples dashboard and somepassword is its password. However running the script gives the following output:

2018-04-14 09:27:10 SERVER -> CLIENT: 220 myserver.net ESMTP 
Postfix (Ubuntu)
2018-04-14 09:27:10 CLIENT -> SERVER: EHLO mywebsite.com
2018-04-14 09:27:10 SERVER -> CLIENT: 250-myserver.net250- 
PIPELINING250-SIZE 10240000250-ETRN250-STARTTLS250-AUTH DIGEST-MD5 CRAM-MD5 
PLAIN LOGIN250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT IDENT250- 
ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2018-04-14 09:27:10 CLIENT -> SERVER: STARTTLS
2018-04-14 09:27:10 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-04-14 09:27:10 CLIENT -> SERVER: QUIT
2018-04-14 09:27:10 SERVER -> CLIENT: 
2018-04-14 09:27:10 SMTP ERROR: QUIT command failed: 
SMTP connect() failed. 
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. 
https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingMessage has been 
sent

Did I understand something wrong?

Thank you in advance!


Solution

  • Check if is a problem with ssl, and try again

    $mail->SMTPOptions = array(
       'ssl' => array(
       'verify_peer' => false,
       'verify_peer_name' => false,
       'allow_self_signed' => true
      )
    );
    

    if it still has an error

    $mail->SMTPDebug = 3;
    

    https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging