1
I can not send mail using the PHPMailer lib. My site is hosted on dreamhost, 

with SMTP gmail I can send more properly to the configuration provided by the hosting the only return is: SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0) SMTP connect() failed. My code:

<?php

date_default_timezone_set('Etc/UTC');

require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$name       = trim(stripslashes($_POST['name']));   
$from       = trim(stripslashes($_POST['email']));
$subject    = trim(stripslashes($_POST['subject']));
$message    = trim(stripslashes($_POST['message']));

$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'mail.example.com.br';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'e-mail@example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Debugoutput = 'html';

$mail->isHTML(true);
$mail->setFrom($from, $name);
$mail->addReplyTo($from, $name);
$mail->addAddress('email@example.com');
$mail->Subject = $subject;
$mail->Body = $message;

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

`

1 Answers1

0

I managed to solve the problem, first replaces the e-mail sent to an e-mail sender@example.com own client, then tried some documentation of dreamhost, made the necessary changes, the code:

  date_default_timezone_set('Etc/UTC');

  require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';

  $subject    = trim(stripslashes($_POST['subject']));
  $name       = trim(stripslashes($_POST['name']));
  $from       = trim(stripslashes($_POST['email']));
  $body       = trim(stripslashes($_POST['message']));

  $mail = new PHPMailer;

  $mail->isSMTP();
  $mail->CharSet = 'UTF-8';
  $mail->SMTPDebug = 1;
  $mail->Host = 'mail.example.com.br';
  $mail->Port = 587;
  $mail->SMTPAuth = true;
  $mail->Username = 'sender@example.com.br';
  $mail->Password = 'password';
  $mail->Debugoutput = 'html';

  $mail->isHTML(true);
  $mail->setFrom('sender@dexample.com.br', 'DontAnswer');
  $mail->addReplyTo($from, $name);
  $mail->addAddress('contact@example.com.br');
  $mail->Subject = $subject;
  $mail->Body = $body;

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

In dreamhost documentation says it should be repeated sending e-mail in setFrom () method; after these changes the form worked perfectly.