Help with email form with PHP Mailer

Asked

Viewed 1,533 times

2

I am having trouble sending messages through my website hosted at UOL Host. They require sending via authenticated SMTP. I’m using PHP Mailer. The form worked but stopped suddenly.

php contact.

<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;

?>
  • Hi Jordache B. Can you tell what’s in the $messaging variable? You can see that there is a possible error message.

  • use door 587

2 answers

1

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

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

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

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

I use to send with a gmail account and it works well. Check out 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'; 
  • Thanks for the expensive help, but at UOL Host there is no SSL in this case. That’s why it’s like this, according to the tutorial that the provider provides. However I will test. Go that! rs obg

1

Sample code given by UOL HOST itself. View and analyze what is missing. There is no error 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!';
}
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.