4
Hello,
I am trying to email using Phpmailer but am having some errors. First errors with the SMTP class not located. I found that I need to create a require for aruqivo Phpmailerautoload.php in my code. After this, problems such as "SMTP connect failed" started to occur. I checked the git manual, and the list of bugs that might be but nothing fixed my problem. This is my code:
<?php
$txtName = "Bruno";
$txtAs = "As";
$txtEmail = "Text mail";
$txtMensage = "Text Body";
$mensageBody = "<b>Name:</b> ".$txtName." <br><b>As:</b> ".$txtAs."<br><b>Message:</b> ".$txtMensage;
require 'phpmailer/PHPMailerAutoload.php';
require 'phpmailer/class.smtp.php';
function smtpmailer($to, $from, $nameDes, $as, $body) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = true;
$mail->Username = '[email protected]';
$mail->Password = 'pass';
$mail->SetFrom($from, $nameDes);
$mail->Subject = $as;
$mail->Body = $body;
$mail->AddAddress($to);
$mail->IsHTML(true);
if(!$mail->Send()) {
$error = "<font color='red'><b>Mail error: </b></font>".$mail->ErrorInfo;
return false;
} else {
$error = "<font color='blue'><b>Mensagem enviada com Sucesso!</b></font>";
return true;
}
}
if (smtpmailer('[email protected]', '[email protected]', $txtName, $txtAs, $mensageBody)) {
Header("location: sucesso.php");
}
if (!empty($error)) echo $error;
?>
Below are images of the project tree and the error that is happening:
Look forward to!
I used Telnet to see the smtp and returned this: 220 smtp.gmail.com ESMTP h25sm15994499qtc.38 - gsmtp
Just one remark, no longer use password connection with Google SMTP. You even have the option to enable "less secure apps" in the account panel to allow this, but modern applications must use XOAUTH2, with individual permission per application (it would have been much simpler if Google had allowed individual passwords per service, but opted for the proprietary path to have more control over the ecosystem).
– Bacco
Got it, but how can I configure the code to use this type of authentication? Is there any way? I noticed that has the port 25 and the port 465 also, but I tried the telnet in both but does not connect.
– Vinícius Biavatti
See if you have more details on $mail->errorMessage();, or some kind of debug, to see the exact cause of the error. The ports are 587 for TLS, 465 for SSL and 25 "dry".
– Bacco
see if using
$mail = new PHPMailer( true );
debug help (I don’t know if this parameter still works)– Bacco
I put true as parameter and added $mail->Smtpdebug = 2; I got the following message:
– Vinícius Biavatti
2016-09-20 14:11:08 SMTP ERROR: failed to connect to server: (0) 2016-09-20 14:11:08 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Fatal error: Uncaught Exception 'phpmailerException' with message 'SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting' in C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php:1487 Stack trace: #0 C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php(1323): Phpmailer->smtpSend('Date: Tue, 20 S...', '<Strong>This is...') #1
– Vinícius Biavatti
C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php(1203): Phpmailer->postSend() #2 C: xampp htdocs Cucamanager Ws usuario sendemail.php(34): Phpmailer->send() #3 {main} thrown in C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php on line 1487
– Vinícius Biavatti
Yeah, it still worked. Better to go back to the way you were and try to figure out what the problem is that keeps you from connecting even. Might be some silly little thing. This test you did with telnet was on the same machine that runs PHP?
– Bacco
Yes, it was on the same machine. I am in the company network, but here is released access to gmail, I do not know if this can be the problem.
– Vinícius Biavatti