Phpmailer sends email but does not send variable values

Asked

Viewed 329 times

0

I’m making a system to send emails with the contact form data to the administrator. So far so good, I was able to configure everything, send the e-mail, but there’s a very peculiar problem. The message and subject data is not sent to the email. It simply goes with the variable name, without its value. How to resolve? Follow form and php codes:

php contact.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=1423" />
    <title>Contato | Lizard Fishing</title>
    <link rel="stylesheet" href="style/estilo.css" type="text/css" />
    <link href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto" rel="stylesheet">
    <script src="js/script.js"></script>
    <link rel="icon" type="image/png" href="icon/favicon.png" />
    <meta name="author" content="Samuel Santos, Thiago Lamonica e Ruan Vicente Bianco" />
    <meta name="robots" content="all" />
    <meta name="rating" content="general" />
</head>
<body>
    <header class="header header_geral">
        <?php
            include "header.html";
        ?>
    </header>
    <main class="main main_geral main_contato">
        <h2 class="title_content">Contato</h2>
        <h3 class="subtitle_content subtitle_contato">Entre em contato conosco!</h3>
        <br />
            <div class="form_contato">
                <form name="form_contato" method="post" action="enviar_contato.php">
                    <p>
                        <label>Nome:</label>
                        <br />
                        <input type="text" name="nome_contato" class="text_input" size="38" required />
                    </p>
                    <p>
                        <label>E-mail:</label>
                        <br/>
                        <input type="email" name="email_contato" class="text_input" size="38" required />
                    </p>
                    <p>
                        <label>Assunto</label>
                        <br/>
                        <input type="text" name="assunto_contato" class="text_input" size="38" required />
                    </p>
                    <p>
                        <label>Mensagem:</label>
                        <br/>
                        <textarea name="mensagem_contato" rows="10" cols="40" class="text_input" required ></textarea>
                    </p>
                    <p>
                        <input type="submit" value="Enviar" class="button_submit" />
                    </p>
                </form>
            </div>
            <div class="dados_contato">
                <p class="text_content text_contato">
                    <a href="https://www.facebook.com/Lizard-Fishing-Equipamentos-Para-Pesca-361157610633775" class="link_fb" target="_blank"><img src="icon/fb.png" width="25" class="icon_fb" />&nbsp;<b>Lizard Fishing</b></a>
                    <br/>
                    <b>Televendas:</b> (55)11 2534-7082
                    <br/>
                    <b>Celular:</b> (55)11 96437-8004
                    <br/>
                    <b>E-mail:</b> [email protected]                    
                </p>
            </div>
    </main>
</body>
</html>

contact_php.

<?php
    $nome=$_POST["nome_contato"];
    $email=$_POST["email_contato"];
    $assunto=$_POST["assunto_contato"];
    $mensagem=$_POST["mensagem_contato"];

include 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.live.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "*********";
$mail->setFrom('[email protected]', 'Administrador');
$mail->addAddress('$email_contato', '$nome_contato');
$mail->addReplyTo('$nome_contato', '$nome_contato');
$mail->addCC('[email protected]', 'Administrador');

$mail->isHTML(true);  

$mail->Subject = '$assunto_contato';
$mail->Body = '$mensagem_contato';

if (!$mail->send()) {
   echo "Mailer Error: " . $mail->ErrorInfo;
} else {
   echo "Message sent!";
}


?>
  • 1

    Where you use variables, exchange single quotes (') in double quotes ("). PHP only analyzes variables present in string set with double quotes. You can also leave the quotes without if the value is only that of the variable.

2 answers

2


PHP documentation defines single quotation marks as simple literal, unprocessed. Double quotation marks will be processed see more.

So do what Anderson Carlos Woss said in the comment "Where you use variables, exchange single quotes (') for double quotes ("). PHP only analyzes variables present in string set with double quotes. You can also leave the quotes without if the value is only that of the variable."

$mail->Subject = "$assunto_contato";
$mail->Body = "$mensagem_contato";
  • it all worked out, Leo Caracciolo! Thank you so much for your help. Strong hug!

  • @Samuelsantos since everything went well, read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

0

On the line $mail->Body = '$mensagem_contato'; you must enter an HTML code with the variables. For example:

$html = 'Nome: ' . $nome .'<br>';
$html .= 'E-mail: ' . $email .'<br>';
$html .= 'Assunto: ' . $assunto .'<br>';
$html .= 'Mensagem: ' . $mensagem .'<br>';

$mail->Body = $html;
  • I did everything you recommended and ran perfect! Thank you very much, friend. Success!

Browser other questions tagged

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