not sending form via phpmailer

Asked

Viewed 187 times

0

My problem is this, I have a form on a page html and would like it to be sent by e-mail, I did some research and decided to use the phpmailer, but when I click the send button nothing happens, it is disappointing. my form.

 <form class="formulario" action="enviar.php" method="post">
        <div class="form-group">
        <fieldset>

                <label for="nome">Nome</label>
            <input required name="nome" type="name" class="form-control" placeholder="Seu nome">
        </fieldset>
        </div>
        <div class="form-group">
        <fieldset>

            <label for="email">E-mail: </label>
            <input required name="email" type="email" class="form-control" placeholder="Seu email">
        </fieldset>
        </div>
        <div class="form-group">
        <fieldset>

            <label for="assunto">Assunto</label>
            <input required name="assunto" type="assunto" class="form-control" placeholder="Assunto do contato">
        </fieldset>
        </div>
        <div class="form-group">
        <fieldset>

            <label for="mensagem">Mensagem: </label>
            <textarea required name="mensagem" type="text" class="form-control" placeholder="Sua mensagem" rows="10" ></textarea>
        </fieldset>
        </div>
        <div class="form-group">
        <fieldset>
        <div class="form-group">
            <button type="submit" class="btn btn-default" name="enviar" value="enviar">Enviar</button>
        </fieldset>
  </div>  

File code send php. is:

   <?php 

if (isset($_POST['enviar'])) {
$destinatarios = '[email protected]';
$nomeDestinatario = 'Contato formulario';
$usuario = '[email protected]'; 
$senha = 'SENHABLABLEBI'; 

/*abaixo as veriaveis principais, que devem conter em seu formulario*/ 

$nome = $_POST['nome'];
$assunto = $_POST['assunto'];
$_POST['mensagem'] = nl2br('E-mail: '. $_POST['email'] ." ". $_POST['mensagem']);

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

include_once("/phpmailer/PHPMailerAutoload.php");

$To = $destinatarios;
$Subject = $assunto;
$Message = $_POST['mensagem'];
$Host = 'mail.'.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 = 1; // 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!'; 
}
} ?>

However the page gets static and nothing happens nor error message appears, I thought it is the hosting that is not sending, I decided to test creating a file in php to send, but every time I run it, is sent email on time, already tested several times.

Code that sends on time the email is:

    <?php
require '/phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'localhost';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // SMTP username
$mail->Password = 'SENHABLABLEBI';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('[email protected]', 'blabe');
$mail->addAddress('[email protected]');               // Name is optional

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'ASSUNTO BLABLE';
$mail->Body    = 'qualquer coisa bla bla blabla msgs';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

  • 1

    It is a good idea to make a simpler version of the problem, it helps to decrease external factors. But in its minimized version you still have to change the $_POST variables since you are no longer posting a form.

  • yes, but I think that nothing is being sent to the script because when I click on the button nothing happens.

1 answer

0

Good morning expec,

Why don’t you use the Mail function? manual link Mail Php

I find it simple and functional, so much so that I’ve used it in a bulk email sending tool.

I hope I’ve helped.

  • the hosting suggests the use of phpmailer, I tried to use directly in the form a mailto, also without success, after I tried the mail of separate php also did not succeed, I only managed to send something in the last script but it does not send anything of the form, apparently the form that is not sending anything by post.

  • Have you checked to see if the data sent from the form is coming? Like, you ignore for now your sending of emails, just print on the screen the data received from your form. Soon the problem is not even in the library, before.

  • are not really coming, but already reread the all apparent code was to go will be some incompatibility with bootstrap?

  • I believe the error is exactly here "if (isset($_POST['send'])) {"

Browser other questions tagged

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