PHP form without email field

Asked

Viewed 81 times

1

I would like to know a method to send a form who doesn’t have the field email. It will have only three fields: Nome, Telefone and Cidade.

However, when sending arrives like this

inserir a descrição da imagem aqui

The code I have:

<?php

require_once('PHPMailer-master/PHPMailerAutoload.php');

PHP_OS == "Linux" ? $quebra_linha = "\n" : $quebra_linha = "\r\n";

$nome = trim($_POST["nome"]);
$tel = $_POST["telefone"];
$cid = $_POST["cidade"];

switch ($cid) {
    case "ipatinga":
        $destino = "[email protected]";
        break;
    case "fabriciano":
        $destino = "[email protected]";
        break;
    case "timoteo":
        $destino = "[email protected]";
        break;
}


$email = "<br><b>NOME:</b> ".$nome."<br><b>TELEFONE:</b> ".$tel." <br>        <b>CIDADE:</b> ".$cid."</body></html>";

$mail = new PHPMailer();

$mail->IsMail();
$mail->SetFrom($eml, $nom);
$mail->Subject    = "Agendamento de visita";
$mail->MsgHTML($email);
$mail->AddAddress($destino, utf8_decode('Unimed'));


if (!$mail->send())
    $msgerro = "Não foi possível enviar sua mensagem de contato. Tente novamente dentro de alguns instantes.";
else
    $msgerro = "Sua mensagem de contato foi enviada com sucesso!";

?> 

How to make that instead of Root User and [email protected] have personalized data?

2 answers

1


It is good that you put a username and password authenticated to be used to send the email.

See your code working here.

Note that I added variables that authenticate smtp so that sending is done in the best way. This authentication will use a true email for sending the email.

I also added the variable $eml and $nom which will be the personalized email and name you want.

Below is the complete code:

<?php

require_once('PHPMailer-master/PHPMailerAutoload.php');

PHP_OS == "Linux" ? $quebra_linha = "\n" : $quebra_linha = "\r\n";

$nome = trim($_POST["nome"]);
$tel = $_POST["telefone"];
$cid = $_POST["cidade"];

switch ($cid) {
    case "ipatinga":
        $destino = "[email protected]";
        break;
    case "fabriciano":
        $destino = "[email protected]";
        break;
    case "timoteo":
        $destino = "[email protected]";
        break;
}


$email = "<br><b>NOME:</b> ".$nome."<br><b>TELEFONE:</b> ".$tel." <br>           <b>CIDADE:</b> ".$cid."</body></html>";

$mail = new PHPMailer();

$mail->IsMail();
$mail->isSMTP();
//$mail->SMTPDebug = 2; retire o comentário para debugar
//$mail->Debugoutput = 'html';
$mail->Host = "mail.seusite.com.br"; //servidor smtp
$mail->Port = 25; //a porta do servidor smtp
$mail->SMTPAuth = true;
$mail->Username = "[email protected]"; //email que vai ser usado para enviar o email
$mail->Password = "senha"; //senha do email que vai ser usado para enviar o email

$eml = "[email protected]"; //dados personalizados que você deseja (email)
$nom = "Seu Nome Personalizado"; //dados personalizados que você deseja (nome)

$mail->SetFrom($eml, $nom);
$mail->Subject    = "Agendamento de visita";
$mail->MsgHTML($email);
$mail->AddAddress($destino, utf8_decode('Unimed'));


if (!$mail->send())
    $msgerro = "Não foi possível enviar sua mensagem de contato. Tente novamente dentro de alguns instantes.";
else
    $msgerro = "Sua mensagem de contato foi enviada com sucesso!";

?> 
  • Great Wendell. Is this authentication for email base only or for each city correspondent? There will be three different emails, one for each city.

  • @Marcelovictor this authentication is for the email that will be used to send the email to the correspondents, regardless of the amount of correspondents you need only one authentication.

  • Got it. Thanks a lot for the help @Wendell

0

The configuration of PHPMailer apparently correct, however the variables that define the sender’s name and email $mail->Setfrom are not defined: $eml and $nom , at least in the code you posted here, do a test replacing the parameters with fixed values, for example

$mail->SetFrom('[email protected]', 'Nome do Remetente');
  • Hello rubStackOverflow. I did this, but the email is not enough.

Browser other questions tagged

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