Connection error with SMTP when sending email using PHPMailer [closed]

2

I'm trying to implement sending mail with the PHPMailer library , however I've brought you the following errors :

Edit: I am testing locally.

SMTP -> ERROR: Failed to connect to server: Uma tentativa de conexão falhou porque o componente conectado não respondeu corretamente após um período de tempo ou a conexão estabelecida falhou porque o host conectado não respondeu. (10060)
SMTP Error: Could not connect to SMTP host.

Edit 2: This is part of var_dump () a> of the object with error-related messages

 private 'language' => 
    array (size=13)
      'provide_address' => string 'You must provide at least one recipient email address.' (length=54)
      'mailer_not_supported' => string ' mailer is not supported.' (length=25)
      'execute' => string 'Could not execute: ' (length=19)
      'instantiate' => string 'Could not instantiate mail function.' (length=36)
      'authenticate' => string 'SMTP Error: Could not authenticate.' (length=35)
      'from_failed' => string 'The following From address failed: ' (length=35)
      'recipients_failed' => string 'SMTP Error: The following recipients failed: ' (length=45)
      'data_not_accepted' => string 'SMTP Error: Data not accepted.' (length=30)
      'connect_host' => string 'SMTP Error: Could not connect to SMTP host.' (length=43)
      'file_access' => string 'Could not access file: ' (length=23)
      'file_open' => string 'File Error: Could not open file: ' (length=33)
      'encoding' => string 'Unknown encoding: ' (length=18)
      'signing' => string 'Signing Error: ' (length=15)

Below is my shipping code:

$email = new PHPMailer();

$email->IsSMTP();
$email->SMTPSecure = "ssl"; // tbm já tentei tls
$email->Port = 587; // tbm já tentei 465 e tbm sem porta nenhuma
$email->SMTPDebug = 1;
$email->Host = "smtp.gmail.com";    
$email->SMTPAuth = true;
$email->Username = "[email protected]";
$email->Password = "minhaSenha";
$email->From = $para;

$email->SetLanguage("en", "../biblioteca/phpmailer/language/");
$email->CharSet = "UTF-8";
$email->FromName = $nome_remetente;
$email->Subject = $assunto;
$email->IsHtml(true);
$email->AddAddress($para);
$email->Body = $msg;

if(!$email->send()){
    //return false;
    die(var_dump($email->ErrorInfo));
} else {
    return true;
}
    
asked by anonymous 17.07.2014 / 14:54

4 answers

0

In the end, the problem was because of the company server that was blocking, in some way. The admin poked the chopsticks on the server and it worked.

    
31.08.2015 / 14:20
5

EDIT: Waiting for OP return after chat conversation

First the gmail port is 465 and not 587

For you to use PHP Mailer with gmail you should do it as follows

$mail= new PHPMailer;
$mail->IsSMTP();        // Ativar SMTP
$mail->SMTPDebug = false;       // Debugar: 1 = erros e mensagens, 2 = mensagens apenas
$mail->SMTPAuth = true;     // Autenticação ativada
$mail->SMTPSecure = 'ssl';  // SSL REQUERIDO pelo GMail
$mail->Host = 'smtp.gmail.com'; // SMTP utilizado
$mail->Port = 465; 
$mail->Username = 'seu email';
$mail->Password = 'sua senha';
$mail->SetFrom('seu email', 'seu nome(ou qualquer coisa que quiser)');
$mail->addAddress('email do destinatario','qualquer coisa que quiser');
$mail->Subject=("Assunto");
$mail->msgHTML("Sua mensagem");
if ($mail->send()){
    $ok = true;
}else{
    $ok = false;
}  

This is the code I use for smtp from gmail and it has always worked ...

    
17.07.2014 / 15:07
2

Since the end of last year (2014), Google requires a more secure authentication mechanism (XOAuth2) across multiple services. This includes Gmail.

This may be the error you are getting. To work, you must enable the option "Allow less secure apps to access your account."

Sign in to your account and go to: link

You can get more information about this at this link: link

    
30.08.2015 / 19:30
0

Change the port to 587 and the connection to ssl

<?php
  header('Content-Type: text/html; charset=UTF-8');

//======================================================================
// CONFIGURAÇÕES DE ENVIO
//======================================================================
      $dispatcher       = array(
        "smtp_prefix"   => "smtp", //Apenas o prefixo, ex: smtp, caso: smtp.dominio.com.br
        "port"          => "587",
        "subject"       => "assunto_email", //Assunto do e-mail
        "from"          => "[email protected]", //Email remetente
        "from_name"     => "seu_nome", //Nome do remetente
        "from_password" => "sua_senha", //Senha rementente
        "to"            => "email_destinario"); //Destinatario


//======================================================================
// CORPO DO EMAIL
//======================================================================

  $message = 'Essa mensagem é um email de teste, caso tenha recebido está tudo ok!';

//======================================================================
// [FIM] CORPO DO EMAIL
//======================================================================



  include_once("./phpmailer/class.phpmailer.php");
  $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->SMTPSecure = 'ssl';    // SSL REQUERIDO pelo GMail
  $mail->Host = "{$dispatcher["smtp_prefix"]}.".substr(strstr($dispatcher["from"], '@'), 1);
  $mail->SMTPDebug = 0; // 1 = erros e mensagens || 2 = apenas mensagens
  $mail->SMTPAuth = true;
  $mail->Port = $dispatcher["port"];
  $mail->Username = $dispatcher["from"];
  $mail->Password = $dispatcher["from_password"];

  $mail->SetFrom($dispatcher["from"], $dispatcher["from_name"]);
  $mail->Subject = $dispatcher["subject"];
  $mail->MsgHTML($message);
  $mail->AddAddress($dispatcher["to"]);
  $mail->CharSet="UTF-8";

  if(!$mail->Send()) {
    $mensagemRetorno = 'Erro ao enviar e-mail: '. print($mail->ErrorInfo);
  } else {
    //Exemplo resposta usando JavaScript
    $sucesso = '<script> document.write("E-mail enviado com sucesso!"); </script>';
    echo $sucesso;
  }

?>

Information about port 587:

  

Services like Gmail, Yahoo! and Hotmail will be affected? There will be changes only for those who use email reader programs, who uses   these services via webmail do not need to change the configuration. All   these e-mail providers already offer the submission service on the door   587 / TCP. Source: AntiSpam.BR

    
17.07.2014 / 15:37