-3

I am using PHPmailer for my contact form and when I open my website with XAMPP it works, but when I upload the website to online server (filezilla) the contact form display an error.

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';


$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';

function clean_text($string)
{
 $string = trim($string);
 $string = stripslashes($string);
 $string = htmlspecialchars($string);
 return $string;
}

if(isset($_POST["submit"]))
{
 if(empty($_POST["name"]))
 {
  $error .= '<p><label class="text-danger">*Por Favor Insira o seu Nome</label></p>';
 }
 else
 {
  $name = clean_text($_POST["name"]);
  if(!preg_match("/^[a-zA-Z ]*$/",$name))
  {
   $error .= '<p><label class="text-danger">Apenas letras e espaços em branco sao permitidos</label></p>';
  }
 }
 if(empty($_POST["email"]))
 {
  $error .= '<p><label class="text-danger">*Por Favor Insira o seu Email</label></p>';
 }
 else
 {
  $email = clean_text($_POST["email"]);
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
   $error .= '<p><label class="text-danger">Formato de email inválido</label></p>';
  }
 }
 if(empty($_POST["subject"]))
 {
  $error .= '<p><label class="text-danger">*Por Favor Insira o seu Assunto</label></p>';
 }
 else
 {
  $subject = clean_text($_POST["subject"]);
 }
 if(empty($_POST["message"]))
 {
  $error .= '<p><label class="text-danger">Por Favor Insira a sua Mensagem</label></p>';
 }
 else
 {
  $message = clean_text($_POST["message"]);
 }
 if($error == '')
 {


$mail = new PHPMailer(true);
  $mail->CharSet = "UTF-8";

  $mail->IsSMTP();        

  $mail->Host = 'smtp.sapo.pt';  
  $mail->Port = 25;      
  $mail->SMTPAuth = true;      
  $mail->Username = '...@sapo.pt';

  $mail->Password = '*****';  

  $mail->SMTPSecure = 'tls';     
  $mail->From = $_POST["email"];    
  $mail->FromName = $_POST["name"];   
  $mail->addAddress('....@sapo.pt ', 'namecompany');

  $mail->WordWrap = 50;       
  $mail->IsHTML(true);          
  $mail->Subject = $_POST["subject"];   



  if($mail->Send())        
  {
   $error = '<label class="text-success">Obrigado por entrar em contacto connosco!</label>';
  }
  else
  {
   $error = '<label class="text-danger">There is an Error</label>';
  }
  $name = '';
  $email = '';
  $subject = '';
  $message = '';
 }
}

ERROR DISPLAYED:

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php:1775 Stack trace: #0 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php(1516): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 12 S...', '\r\n\r\n ...') #1 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend() #2 /home1/carvalho/public_html/contact2.php(119): PHPMailer\PHPMailer\PHPMailer->send() #3 {main} thrown in /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 1775

tehcpu
  • 934
  • 1
  • 11
  • 23
  • Check with the hosting company if they allow you to send emails from other SMTP-servers directly. There are some big hosting companies that doesn't allow it so you must send it through their SMTP-relay. – M. Eriksson Sep 12 '19 at 11:53
  • Indeed it's most likely that your hosting provider blocks outbound SMTP. Diagnosing this is covered in many questions on here, and also in great depth in the troubleshooting guide linked from the error message. – Synchro Sep 12 '19 at 13:25
  • Separately from the problem you describe, don't do this: `$mail->From = $_POST["email"];`; it's forgery and will result in your messages being bounced or spam filtered. Base your code on [the contact form example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/contactform.phps) to avoid this kind of problem. – Synchro Sep 12 '19 at 13:49
  • @Synchro PHPMailer is considered not safe? Because i notice that the problem was because the www.Sapo.pt blocked my email for consider i was having suspicious actions. That was why this wasn´t working.... – DiogoCarvalho Sep 12 '19 at 14:41
  • PHPMailer is fine, but it can certainly be used in ways that are unsafe. Allowing submitters to forge arbitrary from addresses would qualify as suspicious and is grounds for blocking, but that's your code, not PHPMailer, and is why I pointed you at the contact form example, which doesn't make that mistake. – Synchro Sep 12 '19 at 14:45
  • @Synchro but i think i don´t have anything wrong in my code that make him suspicious, or have i? – DiogoCarvalho Sep 12 '19 at 15:20
  • Yes, you do: `$mail->From = $_POST["email"];` is enough to make messages fail simple checks that could cause messages to be classed as suspicious, as I said earlier. – Synchro Sep 12 '19 at 15:24

2 Answers2

-1

Your SMTP credentials are different on the production server. You just need to get valid credentials

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • The error looks more like a connection issue than a credential issue. And if their using a third party mail service/smtp, I don't see why the credentials would need to be different? – M. Eriksson Sep 12 '19 at 11:56
  • A valid SMTP host is also part of the connection credentials, obviously. But why would you want to connect your dev environment to the production email server? That's a bit nuts if you ask me. – delboy1978uk Sep 12 '19 at 12:40
  • Now don´t even work on XAMPP, and just yesterday was working – DiogoCarvalho Sep 12 '19 at 12:53
  • can you ping the server If so, can you telnet it on the correct port? – delboy1978uk Sep 12 '19 at 13:59
-1

If you are sure that the credentials are correct, check the PORT and SMTPsecure, because: port 25 does not require ssl / tls support port 465 for ssl port 587 for tls

Fix your code like this:

$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";

$mail->IsSMTP();        

$mail->Host = 'smtp.sapo.pt';  
$mail->Port = 587; // this for "tls" connection  
$mail->SMTPAuth = true;      
$mail->Username = '...@sapo.pt';

$mail->Password = '*****';  

$mail->SMTPSecure = 'tls'; // this require 587    
$mail->From = $_POST["email"];    
$mail->FromName = $_POST["name"];   
$mail->addAddress('....@sapo.pt ', 'namecompany');

$mail->WordWrap = 50;       
$mail->IsHTML(true);          
$mail->Subject = $_POST["subject"];
Stefano Pascazi
  • 363
  • 1
  • 11
  • i already tried everything...ports (465, 587, 26, 25, 21) and ssl and tls – DiogoCarvalho Sep 12 '19 at 12:46
  • First of all. PHPMailer isn't a good idea to use, because is no longer supporter. Recente update cause some problem. I switched with Zend-mail with SMTP support for best performance. For the second, try to remove smtp connection, and check it if email send. After that, control if hosting have a firewall active or something like that. – Stefano Pascazi Sep 12 '19 at 12:52
  • Excuse me? PHPMailer is still very much supported, by me. If you have a problem, create an issue on the bug tracker. – Synchro Sep 12 '19 at 13:23
  • @Synchro you see any solution for this? – DiogoCarvalho Sep 12 '19 at 13:41
  • Yes - by reading [the other questions the same as this one](https://stackoverflow.com/search?q=phpmailer+smtp+connect%28%29+failed), and [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#smtp-error-could-not-connect-to-smtp-host) that tells you exactly how to diagnose it. – Synchro Sep 12 '19 at 13:45
  • Regarding this answer, STARTTLS (what you get when you set `SMTPSecure = 'tls'`) generally works fine on port 25; changing it to port 587 will make no practical difference. The same is not true of `'ssl'` mode on port 465. – Synchro Sep 12 '19 at 15:26