简体   繁体   中英

i'm trying to send emails using PHPmailer but i Get this error

i get this errors

2020-06-03 21:08:07 Connection: opening to ssl://smtp.gmail.com:25, timeout=300, options=array() 2020-06-03 21:08:07 Connection failed.

Error #2: stream_socket_client(): SSL operation failed with code 1.

OpenSSL Error messages:error:1408F10B:SSL routines:ssl3_get_record:wrong version number [C:\xampp\htdocs\Go\vendor\phpmailer\phpmailer\src\SMTP.php line 344]

2020-06-03 21:08:07 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [C:\xampp\htdocs\Go\vendor\phpmailer\phpmailer\src\SMTP.php line 344]

2020-06-03 21:08:07 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:25 (Unknown error) [C:\xampp\htdocs\Go\vendor\phpmailer\phpmailer\src\SMTP.php line 344]

2020-06-03 21:08:07 SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

<?php
require './vendor/autoload.php';


$send = new PHPMailer\PHPMailer\PHPMailer();

$send->SMTPDebug = 4;// Enable verbose debug output

$send->isSMTP();                                      // Set mailer to use SMTP
$send->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$send->SMTPAuth = true;                               // Enable SMTP authentication
$send->Username = 'abdelkbirkh2@gmail.com';                 // SMTP username
$send->Password = 'µµµµµµ';                           // SMTP password
$send->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$send->Port = 25;                                    // TCP port to connect to

$send->setFrom('abdelkbirk@gmail.com', 'Mailer');
$send->addAddress('abdo9@gmail.com', 'Joe User');     // Add a recipient
$send->addAddress('abdok79@gmail.com');               // Name is optional
$send->addReplyTo('abdelk2@gmail.com', 'Information');
$send->addCC('abdelkbir32@gmail.com');
$send->addBCC('abdelkbirk32@gmail.com');

// $send->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
// $send->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$send->isHTML(true);                                  // Set email format to HTML

$send->Subject = 'Here is the subject';
$send->Body    = 'This is the HTML message body <b>in bold!</b>';
$send->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$send->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $send->ErrorInfo;
} else {
    echo 'Message has been sent';
}

This is happening because you have a bad combination of encryption mode and port number.

You have set:

$send->SMTPSecure = 'ssl';
$send->Port = 25;

ssl mode for SMTPSecure is implicit TLS (also known as SMTPS), where the port you connect to will expect you to talk TLS straight away – but you are connecting to a port that is not configured to expect that, so it just won't work.

ssl mode will usually be used on port 465, but you can instead switch to STARTTLS mode (explicit TLS), which will work on port 587 for gmail:

$send->SMTPSecure = 'tls';
$send->Port = 587;

This exact issue is described in great detail in the PHPMailer troubleshooting guide the error message linked you to – the first thing you should always do when you have a problem like this is read the error message!

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