Error sending mail with phpmailer, connect () failed [duplicate]

0

I have installed PHP v5.6.25 (I use the 64x version) and wampserver v3.0.6, Apache 2.4.23.

I have made some port modifications since I have followed several tutorials on YouTube, but I do not remember which lines I have modified on a regular basis.

File php.ini www.dropbox.com/s/vnoxv38qqa67oez/php.ini?dl=0

I am trying to make a mailing system using phpmailer, using as host "smtp.gmail.com" I have enabled access to less secure applications from my gmail account (myaccount.google.com/lesssecureapps).

I was following the following tutorial: "Sending mail by form with PHPMailer" (youtu.be/t4CZa-kHX5E) I downloaded the files included in the description (PHPMailer) and (Program code) but I could not make it work .

My PHP file is as follows:

<?php
require('PHPMailer/PHPMailerAutoload.php');

$oMail = new PHPMailer();
$oMail->isSMTP();
$oMail->Host = 'smtp.gmail.com';
$oMail->Username = '********@gmail.com'; //Confidencial
$oMail->Password = '******************'; //Confidencial;

$oMail->SMTPAuth = true;
$oMail->SMTPSecure = 'ssl';
$oMail->Port = 465; // 587 StartTLS - 465 ssl
$oMail->CharSet = "UTF-8";

/* Envio de mensaje */
$oMail->From = '*******@gmail.com'; //Remitente de GMAIL
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$oMail->addAddress($email); //Destinatario

$oMail->Subject = filter_var($_POST['asunto'], FILTER_SANITIZE_STRING); //asunto
$oMail->Body = filter_var($_POST['mensaje'], FILTER_SANITIZE_STRING); //contenido

/***/
if($oMail->send() == false){
    echo "No se pudo enviar email";
    echo $oMail->ErrorInfo;
    } else {
    echo "Mensaje enviado";
}?>

throws the following message:

If using the Gmail host service 'smtp.gmail.com' with security type ssl, according to this www.arclab.com/en/kb/email/how-to-enable-imap-pop3-smtp -gmail-account.html should use port 465 ... in this case should I make any changes to my php.ini file?

    
asked by Fede Lorca 04.04.2017 в 21:51
source

2 answers

0

How to solve this problem?

The best solution would be to have a valid certificate.

But when this is not possible, you can add the following to our code:

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

Another possible solution given by the creators of PHP Mailer is to create our own custom class, of which they show a example on this link .

    
answered by 05.04.2017 / 16:50
source
1

The problem as Yoel says is the port or the address you use to authenticate yourself.

mailtrap.io

This is a testing service, where it is like sending you email, you can occupy ports 25 or 465 or 2525

where your usuario and contraseña are what the service gives you, and they are random data. (It will never be an email or password that you establish)

SMTP
Host:   smtp.mailtrap.io
Port:   25 or 465 or 2525
Username:   3482SoYuNaPruEb4
Password:   AASoYUnP4ssW0RDPruEb4
Auth:   PLAIN, LOGIN and CRAM-MD5
TLS:    Optional

For this case, your code would look like this:

$oMail = new PHPMailer();

$oMail->isSMTP();
$oMail->Host = 'mailtrap.io';
$oMail->Username = '348264b5be11a85ac';  //IMPORTANTE
$oMail->Password = 'cc1b5b874803f7';  //IMPORTANTE

$oMail->SMTPAuth = true;
$oMail->SMTPSecure = 'tls';
$oMail->Port = 25;

$oMail->From = '[email protected]'; //Remitente
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$oMail->addAddress($email); //Destinatario

$oMail->Subject = filter_var($_POST['asunto'], FILTER_SANITIZE_STRING); //asunto
$oMail->Body = filter_var($_POST['mensaje'], FILTER_SANITIZE_STRING); //contenido

Gmail

For GMail's TLS it is for port 587 and you must have enabled the option "allow non-secure applications for sending email" in your security settings.

  

Gmail is not a service for mailing list and only allows a quantity   Limited daily mail. The abuse of the service can cause the account to be blocked.

So the data would look like this:

$oMail = new PHPMailer();
$oMail->isSMTP();
$oMail->Host = 'smtp.gmail.com';
$oMail->Username = '[email protected]';
$oMail->Password = 'abcd1234*';

$oMail->SMTPAuth = true;
$oMail->SMTPSecure = 'tls';
$oMail->Port = 587;  //IMPORTANTE

$oMail->From = '[email protected]'; //Remitente de GMAIL
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$oMail->addAddress($email); //Destinatario

$oMail->Subject = filter_var($_POST['asunto'], FILTER_SANITIZE_STRING); //asunto
$oMail->Body = filter_var($_POST['mensaje'], FILTER_SANITIZE_STRING); //contenido

In the end no matter which option you choose:

if($oMail->send() == false){
    echo "No se pudo enviar email";
    echo $oMail->ErrorInfo;
} else {
    echo "Mensaje enviado";
}
    
answered by 04.04.2017 в 22:49