0

我有一个使用 SMTP SendGrid 的 PHP 表单设置。表单发送良好,但电子邮件通过时没有包含任何数据。HTML 表单具有匹配的 name= ...我在做什么错?

名字:
姓氏:
公司:
职位:
电子邮件:
电话:
留言:

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.';
    }

这是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>
4

1 回答 1

1

哎呀!我现在看到了问题。您正在使用表单中的变量,例如$FirstName在它们被分配之前。

将您的$mail->Body作业移到您的// Form fields块下方。

:)

于 2013-11-08T16:04:03.727 回答