通过PHPMailer向电子邮件发送多个复选框值

时间:2016-11-03 10:19:17

标签: php

我有一个PHP测试表单,我在点击提交按钮后从我的电子邮件中的所选复选框字段中获取空值。我正在使用PHPMailer向我的邮件发送输入.~请帮助

PHP PART

// Check the values from checkbox and POST them to email       
If (isset($_POST["checkbox"]) && !empty($_POST["checkbox"]))             
    {
        $checkbox =$_POST["checkbox"];                //Array of values from the checkbox values
            $value= implode(' , ', $checkbox);           //implode values and separate by a comma
                echo $value;
    }                          

else 
    {
        test_input($_POST["value"]);          //POST values to email
    }

PHPMAILER PART

require ("PHPMailer/PHPMailerAutoload.php");                      //Instantiate the Phpmailer class

    $mail = new PHPMailer();

    $mail->IsSMTP();                                      // mailer to use SMTP
    $mail->SMTPDebug = 2;
    $mail->Host = "smtp.gmail.com";            // main and backup server
    $mail->SMTPSecure = "tls";                     // TLS connection
    $mail->Port = 587;                                //Gmail SMTP port
    $mail->SMTPAuth = true;                         // turn on SMTP authorization
    $mail->Username = "gmail_address";          //username
    $mail->Password = "****************";            // password

    $mail->From = "$email";                         //sender's email
    $mail->FromName = "$name";                        //name
    $mail->AddAddress("gmail_address", "Pettans");        //email address of recepient and name
    $mail->AddReplyTo($_POST["email"], $_POST["name"]);                     //Address to which recepient will reply
    $mail->Subject = "Graphics Design Form";    //subject of email

    $mail->WordWrap = 120;                                   //word wrap to 100 characters
    $mail->IsHTML(true);                                          //email format to HTML

    $mail->Body = "<br>Checkbox: " . $_POST["value"];




    if ($valid) {
        if (!$mail->Send()) {
            echo 'Form could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            exit;
        } else {
            header('Location:thanks.html');
        }
    }
}


function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;

}

HTML code:

<br> <br> What project would you like for your institution? <span class= "error"> * </span>
                   <br> <br> <input type="checkbox" class="input" name="checkbox[]" value="Graphics Design"> Graphics Design (Book, Posters, Flyers, Banners, Magazines, T-Shirts) <br>
                   <input type="checkbox" class="input" name="checkbox[]" value="Logo Design" > Professional logo Design <br>
                   <input type="checkbox" class="input" name="checkbox[]" value="Branding" > Branding 

2 个答案:

答案 0 :(得分:1)

您似乎试图通过从$value - 超级全局访问变量$_POST来访问它。那不会起作用......

这是一个重写应该可以解决的问题:

$value = null; // Define the variable and set a default value

if (isset($_POST["checkbox"])) {
    // empty() is not needed since 'checkbox' won't be sent
    // if none was selected.

    $value = implode(' , ', $_POST['checkbox']);
    $value = test_input($value);

}

require ("PHPMailer/PHPMailerAutoload.php");

$mail = new PHPMailer();

//...the rest of the PHPMailer-setup

$mail->Body = "<br>Checkbox: " . $value;

//...the rest of your script

答案 1 :(得分:0)

这就是我的完整PHP代码:

<?php

        //INITIALIZE VARIABLES
             $checkbox ="";

if ($_SERVER["REQUEST_METHOD"] == "POST") {



// Check the values from checkbox and POST them to email       
If (isset($_POST["checkbox"]) && !empty($_POST["checkbox"]))             
    {
        $checkbox =$_POST["checkbox"];                //Array of values from the checkbox values
            $value= implode(' , ', $checkbox);           //implode values and separate by a comma
                echo $value;
    }                          

else 
    {
        test_input($_POST["value"]);          //POST values to email
    }      

    require ("PHPMailer/PHPMailerAutoload.php");                      //Instantiate the Phpmailer class

    $mail = new PHPMailer();

    $mail->IsSMTP();                                      // mailer to use SMTP
    $mail->SMTPDebug = 2;
    $mail->Host = "smtp.gmail.com";            // main and backup server
    $mail->SMTPSecure = "tls";                     // TLS connection
    $mail->Port = 587;                                //Gmail SMTP port
    $mail->SMTPAuth = true;                         // turn on SMTP authorization
    $mail->Username = "gmail_address";          //username
    $mail->Password = "****************";            // password

    $mail->From = "$email";                         //sender's email
    $mail->FromName = "$name";                        //name
    $mail->AddAddress("gmail_address", "Pettans");        //email address of recepient and name
    $mail->AddReplyTo($_POST["email"], $_POST["name"]);                     //Address to which recepient will reply
    $mail->Subject = "Graphics Design Form";    //subject of email

    $mail->WordWrap = 120;                                   //word wrap to 100 characters
    $mail->IsHTML(true);                                          //email format to HTML

    $mail->Body = "<br>Checkbox: " . $_POST["value"];




    if ($valid) {
        if (!$mail->Send()) {
            echo 'Form could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            exit;
        } else {
            header('Location:thanks.html');
        }
    }
}


function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;

}

?>