Garrett Sanderson Garrett Sanderson - 2 months ago 16
PHP Question

$_GET Input Data from Form and output into email using PHPMailer

I have a form that I am building that will then send an email using PHPMailer.

The email is working and sending however I am getting stuck on putting the input values into the email body message.

This is my HTML

<form class="get-a-quote" method="get" action="/wp-content/themes/wozencraft/php-mailer/mailer.php">
<div class="get-a-quote-form-carousel">

<!-- Start Insured Information -->
<div class="carousel-cell">
<div class="form-inputs">
<h4>Insured Information</h4>
<p>This wont take long. Please fill out the following information about yourself.</p>

<label for="date">Date</label>
<input type="date" placeholder="" id="date">

<label for="reffered-by">Reffered By</label>
<input type="text" placeholder="" id="reffered-by">

<label for="insureds-name">Insursed's Name</label>
<input type="text" placeholder="" id="insureds-name">

<label for="street-address">Street Address</label>
<input type="text" placeholder="" id="street-address">

<label for="city">City</label>
<input type="text" placeholder="" id="city">

<label for="state">State</label>
<input type="text" placeholder="" id="state">

<label for="zip-code">Zip Code</label>
<input type="text" placeholder="" id="zip-code">

<label for="insured-email">E-mail Address</label>
<input type="email" name="insuredEmail" placeholder="" id="insured-email">

<label for="insured-phone-number">Phone Number</label>
<input type="text" placeholder="" id="insured-phone-number">

<input type="submit" value="Submit">
</div>
</div>
</div>
</form>


mailer.php

<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');

// $email and $message are the data that is being
// posted to this page from our html contact form

$email = $_GET['insuredEmail'];


require 'PHPMailerAutoload.php';

echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n";

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

// use
// $mail->Host = gethostbyname('smtp.gmail.com');

// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "myemail@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "mypasswod";

//Set who the message is to be sent from
$mail->setFrom($email, 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo($email, 'First Last');

//Set who the message is to be sent to
$mail->addAddress($email, 'John Doe');

//Set the subject line
$mail->Subject = 'Test Get A Quote';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->Body = 'This is the HTML message body' . $email;

$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
echo $email;
}


Any I know this must be something small that I am missing. Just need another set of eyes. Thanks in Advance.

Answer

Your problem is nothing to do with what you name your variables; there's nothing special about $email. It isn't working because you have not given names to your form inputs. Here's a fixed version:

<form class="get-a-quote" method="get" action="/wp-content/themes/wozencraft/php-mailer/mailer.php">
        <div class="get-a-quote-form-carousel">

            <!-- Start Insured Information -->
            <div class="carousel-cell">
                <div class="form-inputs">
                    <h4>Insured Information</h4>
                    <p>This wont take long. Please fill out the following information about yourself.</p>

                    <label for="date">Date</label>
                    <input type="date" name="date" placeholder="" id="date">

                    <label for="referred-by">Referred By</label>
                    <input type="text" name="referred-by" placeholder="" id="reffered-by">

                    <label for="insureds-name">Insured's Name</label>
                    <input type="text" name="insureds-name" placeholder="" id="insureds-name">

                    <label for="street-address">Street Address</label>
                    <input type="text" name="street-address" placeholder="" id="street-address">

                    <label for="city">City</label>
                    <input type="text" name="city" placeholder="" id="city">

                    <label for="state">State</label>
                    <input type="text" name="state" placeholder="" id="state">

                    <label for="zip-code">Zip Code</label>
                    <input type="text" name="zip-code" placeholder="" id="zip-code">

                    <label for="insured-email">E-mail Address</label>
                    <input type="email" name="insuredEmail" placeholder="" id="insured-email">

                    <label for="insured-phone-number">Phone Number</label>
                    <input type="text" name="insured-phone-number" placeholder="" id="insured-phone-number">

                    <input type="submit" name="submit" value="Submit">
                </div>
            </div>
       </div>
</form>

I also fixed some spelling mistakes in there.

In your PHP, you access the submitted form values by the same names (which must match exactly) in the $_GET superglobal. You should also only try to process the form if it's been submitted, not just when the page loads - I've given a name to the submit button to test for that. You're setting Body and AltBody manually, so there's no point in calling msgHTML(). Do not use the submitter's address as the from address as it will cause your messages to fail anti-forgery SPF checks; use your own address as the from address, put the submitter in reply-to:

if (array_key_exists('submit', $_GET)) {

    $email = $_GET['insuredEmail'];

    //Now do the rest of the stuff the PHPMailer script does, with these changes:

    //Put all submitted values in the message body:
    $mail->Body = <<<EOT
    date: {$_GET['date']}
    referred-by: {$_GET['referred-by']}
    insureds-name: {$_GET['insureds-name']}
    street-address: {$_GET['street-address']}
    city: {$_GET['city']}
    state: {$_GET['state']}
    zip-code: {$_GET['zip-code']}
    insured-email: {$_GET['insured-email']}
    insured-phone-number: {$_GET['insured-phone-number']}
    EOT;

    $mail->setFrom('myemail@gmail.com', 'Form submission');
    $mail->addReplyTo($email, $_GET['insureds-name']);

}
Comments