使用PHPMailer从表单输入数据并输出到电子邮件


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

我有一个窗体,我正在建设,然后将发送电子邮件使用PHPMailer。

电子邮件正在工作和发送,但是我被卡在将输入值放入电子邮件正文消息中。

这是我的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;
}

任何我知道这一定是我错过了一些小的东西。只是需要另一双眼睛。

您的问题与您的变量名称无关;$email没有什么特别之处。它不能工作,因为您没有为表单输入指定名称。下面是一个固定的版本:

<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>

我还修正了一些拼写错误。

在PHP中,通过$_GET超全局变量中的相同名称(必须完全匹配)访问提交的表单值。您还应该只在提交表单时才处理表单,而不仅仅是在页面加载时才处理表单—我已经为提交按钮指定了一个名称,以便对此进行测试。您手动设置了BodyAltBody,因此调用msgHTML()没有意义。不要使用提交者的地址作为发送地址,因为这会导致你的邮件无法通过防伪造SPF检查;使用您自己的地址作为发送地址,将提交者放在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']);
}