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

时间:2016-09-21 02:49:42

标签: php email input 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;
}

任何我知道这一定是我想念的小事。只需要另一套眼睛。在此先感谢。

1 个答案:

答案 0 :(得分:2)

您的问题与您为变量命名的内容无关; $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检查;使用您自己的地址作为发件人地址,将提交者置于回复中:

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']);

}