Question

I have a PHP form setup using SMTP SendGrid. 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?

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:

<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>
Was it helpful?

Solution

Oops! I see the problem now. You are using the variables from your form, e.g. $FirstName before they have been assigned.

Move your $mail->Body assignment below your // Form fields block.

:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top