Help with e-mail form with PHP Mailer

2

I'm having trouble sending messages through my hosted UOL Host website. They require sending through authenticated SMTP. I'm using PHP Mailer. The form worked but stopped working.

contact.php

<form name="sentMessage" id="contactForm" novalidate>
  <div class="row control-group">
      <div class="form-group col-xs-12 floating-label-form-group controls">
          <label>Nome</label>
          <input disabled type="text" class="form-control" placeholder="Nome" id="name" required data-validation-required-message="Por favor, digite seu nome.">
          <p class="help-block text-danger"></p>
      </div>
  </div>
  <div class="row control-group">
      <div class="form-group col-xs-12 floating-label-form-group controls">
          <label>Empresa</label>
          <input disabled type="text" class="form-control" placeholder="Empresa" id="empresa">
          <p class="help-block text-danger"></p>
      </div>
  </div>
  <div class="row control-group">
      <div class="form-group col-xs-12 floating-label-form-group controls">
          <label>Email</label>
          <input disabled type="email" class="form-control" placeholder="Email" id="email" required data-validation-required-message="Por favor, digite seu email.">
          <p class="help-block text-danger"></p>
      </div>
  </div>
  <div class="row control-group">
      <div class="form-group col-xs-12 floating-label-form-group controls">
          <label>DDD + Telefone</label>
          <input disabled type="tel" class="form-control" placeholder="DDD + Telefone" id="phone">
          <p class="help-block text-danger"></p>
      </div>
  </div>
  <div class="row control-group">
      <div class="form-group col-xs-12 floating-label-form-group controls">
          <label>Mensagem</label>
          <textarea disabled rows="5" class="form-control" placeholder="Mensagem" id="message" required data-validation-required-message="Por favor, escreva sua mensagem."></textarea>
          <p class="help-block text-danger"></p>
      </div>
  </div>
  <br>
  <div id="success"></div>
  <div class="row">
      <div class="form-group col-xs-12">
          <button disabled type="submit" class="btn btn-success btn-lg pull-right" name="enviar">Enviar</button>
          <button type="reset" class="btn btn-link btn-sm pull-left">Limpar formulário</button>
      </div>
  </div>
</form>

contact_me.php

<?php

$name           = $_POST['name'];
$empresa        = $_POST['empresa'];
$email_address  = $_POST['email'];
$phone          = $_POST['phone'];
$message        = $_POST['message'];

$user = '[email protected]';
$destino = '[email protected]';

require 'PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->setLanguage('pt');

// Define os dados do servidor e tipo de conexão
$mail->IsSMTP();
$mail->Host     = 'smtp.avss.com.br';     // Endereço do servidor SMTP
$mail->SMTPAuth = true;                   // Usa autenticação SMTP? (opcional)
$mail->Username = $user;                  // Usuário do servidor SMTP       
$mail->Password = '***********';          // Senha do servidor SMTP
$mail->Port     = 587;

$mail->setFrom($user, 'Fale Conosco AVSS'); // // Define o remetente
$mail->addReplyTo($email_address); // Define email para resposta
$mail->addAddress($user); // Define os destinatário(s)
//$mail->addAddress($destino);
//$mail->addBCC('');

$mail->IsHTML(true); // Define que o e-mail será enviado como HTML
$mail->CharSet = 'utf-8';
$mail->WorWrap = 70; 

$mail->Subject = 'Contato via site'; // Assunto da mensagem
$mail->Body =
    "<body style='background: #FFF; padding: 0; margin: 0;'>
            <div style='position: relative; width: 800px; height: auto; padding: 0; margin: 0;'>
                <br />
                <span style='line-height: 20px; font-family: Arial; font-size: 13px; color: #444;'><b>NOME: </b></span>
                <span style='font-family: Arial; font-size: 13px; color: #0076a3;'>" . $name . "</span><br />

                <span style='line-height: 20px; font-family: Arial; font-size: 13px; color: #444;'><b>EMPRESA: </b></span>
                <span style='font-family: Arial; font-size: 13px; color: #0076a3;'>" . $empresa . "</span><br />

                <span style='line-height: 20px; font-family: Arial; font-size: 13px; color: #444;'><b>EMAIL: </b></span>
                <span style='font-family: Arial; font-size: 13px; color: #0076a3;'>" . $email_address . "</span><br />

                <span style='line-height: 20px; font-family: Arial; font-size: 13px; color: #444;'><b>TELEFONE: </b></span>
                <span style='font-family: Arial; font-size: 13px; color: #0076a3;'>" . $phone . "</span><br /><br />

                <span style='line-height: 20px; font-family: Arial; font-size: 13px; color: #444;'><b>MENSAGEM: </b></span>
                <span style='font-family: Arial; font-size: 13px; color: #0076a3;'>" . $message . "</span><br />
            </div>
        </body>";

// Envia o e-mail
if(!$mail->Send()){
$mensagemRetorno = 'Erro ao enviar e-mail: '. print($mail->ErrorInfo);
} else {
$mensagemRetorno = 'E-mail enviado com sucesso!';
}
} {}

return true;

?>
    
asked by anonymous 16.12.2014 / 14:30

2 answers

1

I use a code for sending secure email with PHPMailer and comparing yours to mine, I noticed a small difference in the line:

$mail->Host = 'smtp.avss.com.br'; //Endereço do servidor SMTP

Try to put ssl:// before the server address:

$mail->Host = 'ssl://smtp.avss.com.br'; //Endereço do servidor SMTP

I use to upload with a gmail account and it works fine. Take a look at the settings I use.

$this->SMTPDebug = false;
$this->SetLanguage('br');
$this->IsSMTP(true);
$this->IsHTML(true);
$this->Host      = "ssl://smtp.googlemail.com";
$this->Port      = 465;
$this->SMTPAuth  = true;
$this->Username  = "**********@gmail.com";
$this->Password  = "**********";
$this->CharSet   = 'UTF-8'; 
    
16.12.2014 / 14:57
1

Sample code given by UOL HOST itself. See and analyze what is missing. There are no errors here:

/*apenas dispara o envio da mensagem caso houver/existir $_POST['enviar']*/
if (isset($_POST['enviar']))
{
/* 
/* $destinatarios = 'email-para-receber-formulario@seu-dominio';

/* $nomeDestinatario = 'Nome do destinatário';

/* $usuario = 'usuario@seu-dominio';

/* $senha = 'senha';


/*abaixo as veriaveis principais, que devem conter em seu formulario*/
$nomeRemetente = $_POST['nomeRemetente'];
$assunto = $_POST['assunto'];
$_POST['mensagem'] = nl2br('E-mail: '. $_POST['email'] ."

". $_POST['mensagem']);


/*********************************** A PARTIR DAQUI NAO ALTERAR ************************************/

include_once("class.phpmailer.php");

$To = $destinatarios;
$Subject = $assunto;
$Message = $_POST['mensagem'];

$Host = 'smtp.'.substr(strstr($usuario, '@'), 1);
$Username = $usuario;
$Password = $senha;
$Port = "587";

$mail = new PHPMailer();
$body = $Message;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $Host; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = $Port; // set the SMTP port for the service server
$mail->Username = $Username; // account username
$mail->Password = $Password; // account password

$mail->SetFrom($usuario, $nomeDestinatario);
$mail->Subject = $Subject;
$mail->MsgHTML($body);
$mail->AddAddress($To, "");

if(!$mail->Send()) {
$mensagemRetorno = 'Erro ao enviar e-mail: '. print($mail->ErrorInfo);
} else {
$mensagemRetorno = 'E-mail enviado com sucesso!';
}
}
?>
    
15.01.2015 / 16:05