简体   繁体   English

PHP SMTP SendGrid表单发送但没有表单数据

[英]PHP SMTP SendGrid Form Sending but with no form data

I have a PHP form setup using SMTP SendGrid. 我有一个使用SMTP SendGrid的PHP表单设置。 The form sends fine but the emails comes through without any of the data included. 该表格可以很好地发送邮件,但电子邮件通过时没有包含任何数据。 The HTML form has matching name= ...what am I doing wrong? HTML表单具有匹配的name = ...我在做什么错?

First Name: 名字:
Last Name: 姓:
Company: 公司:
Job Title: 职称:
Email: 电子邮件:
Phone: 电话:
Message: 信息:

require("phpmailer.php");
    $mail = new PHPMailer();

    // SMTP & Sendgrid settings
    $mail->IsSMTP();
    $mail->Host = "smtp.sendgrid.net";
    $mail->Port = "NNN";
    $mail->SMTPAuth = "true";   // Enable SMTP authentication
    $mail->Username = "username";  
    $mail->Password = "password";
    $mail->SMTPSecure = ''; // Enable encryption, 'ssl' also accepted

    // Email headers and body
    $mail->SetFrom("myemail@email.com");
    $mail->AddAddress("myemail@email.com");

    $mail->Subject  = "Message from site.com";
    $mail->Body     = "You have a new message from your contact form on site.com \n \n First Name: $FirstName \n Last Name: $LastName \n Company: $Company \n Job Title: $JobTitle \n Email: $Email \n Phone: $Phone \n Message: $Message";
    $mail->WordWrap = 50;

    // Form fields
    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $Company = $_POST['Company'];
    $JobTitle = $_POST['JobTitle'];
    $Email = $_POST['Email'];
    $Phone = $_POST['Phone'];
    $Message = $_POST['Message'];

    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } 
    else {
      echo 'Message has been sent.';
    }

Here is the HTML: 这是HTML:

<form id="ContactForm" class="form" method="POST" action="http://staging.shipmenthq.com/mail.php" onsubmit="validateForm()">
                    <div class="formRow">
                        <div class="formHalf padRight">
                            <label>*First Name:</label>
                            <input type="text" name="FirstName">
                        </div>
                        <div class="formHalf">
                            <label>*Last Name:</label>
                            <input type="text" name="LastName">
                        </div>
                    </div>
                    <div class="formRow">
                        <span class="formFull">
                            <label>*Company:</label>
                            <input type="text" name="Company">
                        </span>
                    </div>
                    <div class="formRow">
                        <span class="formFull">
                            <label>*Job Title:</label>
                            <input type="text" name="JobTitle">
                        </span>
                    </div>
                    <div class="formRow">
                        <span class="formFull">
                            <label>*Email:</label>
                            <input type="email" name="Email">
                        </span>
                    </div>
                    <div class="formRow">
                        <span class="formFull">
                            <label>*Phone Number:</label>
                            <input type="tel" name="Phone">
                        </span>
                    </div>
                    <div class="formRow">
                        <span class="formFull">
                            <label>Message:</label>
                            <textarea name="Message"></textarea>
                        </span>
                    </div>
                    <div class="formRow">
                        <span class="formFull">
                            <input type="submit" value="Submit Form" class="button" />
                        </span>
                    </div>
                        </form>

Oops! 糟糕! I see the problem now. 我现在看到了问题。 You are using the variables from your form, eg $FirstName before they have been assigned. 您正在使用表单中的变量,例如$FirstName然后再分配它们。

Move your $mail->Body assignment below your // Form fields block. $mail->Body分配$mail->Body // Form fields块下方。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM