我无法输入输入值

时间:2019-01-26 15:27:28

标签: php phpmailer

所有人,我遇到一个无聊的问题。
我有一个联系表,当我发送联系电子邮件时,电子邮件甚至到达了,但是除了消息外,没有传递输入值。

我收到这样的消息:

联系网站

Name:
Email:
Message: (here I get what I write in the message input)

但是,我输入的我没有收到的姓名,电子邮件和主题显示为空白。
有人可以帮我吗?

这是我的表单代码:

<form id="contactform" name="contactform" action="email.php" method="POST" >
    <div class="row form-group">
        <div class="col-md-12">
            <!-- <label for="fname">First Name</label> -->
            <input type="text" id="name" class="form-control" placeholder="Nome" required>
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <!-- <label for="email">Email</label> -->
            <input type="email" id="email" class="form-control" placeholder="E-mail" required>
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <!-- <label for="subject">Subject</label> -->
            <input type="text" id="subject" class="form-control" placeholder="Assunto">
        </div>
    </div>

    <div class="row form-group">
        <div class="col-md-12">
            <!-- <label for="message">Message</label> -->
            <textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Mensagem" required></textarea>
        </div>
    </div>
    <div class="form-group">
        <input id="submit" name="submit" type="submit" value="Envie sua mensagem" class="btn btn-primary">
    </div>
</form>

这是我的PHPMailer代码:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once 'vendor/autoload.php';

if (isset($_POST) && !empty($_POST)){
 try {
    $mail = new PHPMailer();
    //Server settings
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'conta***@gmail.com';
    $mail->Password = '(my supa password)';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    //Recipients
    $mail->setFrom('lv***@gmail.com', 'Leo');
    $mail->addAddress('lv***@gmail.com', 'Leo');

    $mensagem = "<strong>Contato site</strong><br><br>";
    $mensagem .= "<strong>Nome:</strong> " . $_POST['name'] . "<br>";
    $mensagem .= "<strong>E-mail:</strong> " . $_POST['email'] . "<br>";
    $mensagem .= "<strong>Mensagem: </strong> " . $_POST['message'] . "<br>";

    //Content
    $mail->isHTML(true);
    $mail->Subject = $_POST['subject'];
    $mail->Body    = nl2br($mensagem);
    //$mail->AltBody = $mensagem;

    if($mail->send()){
        $mail->ClearAllRecipients();
        echo json_encode(array('status' => 'OK', 'mensagem' => 'E-mail enviado com sucesso!'));
        header('Location: /agradecimentos.php');
    }else{
        $mail->ClearAllRecipients();
        echo json_encode(array('status' => 'ERRO', 'mensagem' => $mail->ErrorInfo));
        header('Location: /error.php');
    }

} catch (Exception $e) {
    $mail->ClearAllRecipients();
    echo json_encode(array('status' => 'ERRO', 'mensagem' => $mail->ErrorInfo));
    header('Location: /error.php');
  }
}

该如何解决?

2 个答案:

答案 0 :(得分:1)

“消息”是唯一带有name的表单元素:

<textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Mensagem" required></textarea>

因此这是唯一发送到服务器的邮件。也将name赋予您其他表单元素:

<input type="text" name="name" id="name" class="form-control" placeholder="Nome" required>
... etc.

name必须与您用来在服务器上获取值的密钥相匹配:

$_POST['name']

答案 1 :(得分:0)

替换以下几行

<input type="text" id="name" class="form-control" placeholder="Nome" required>
<input type="email" id="email" class="form-control" placeholder="E-mail" required>
<input type="text" id="subject" class="form-control" placeholder="Assunto">

使用

<input type="text" name="name" id="name" class="form-control" placeholder="Nome" required>
<input type="text" name="email" id="email" class="form-control" placeholder="E-mail" required>
<input type="text" name="subject" id="subject" class="form-control" placeholder="Assunto">