1

I need to send form data to mail in tabular format. for this i'm using using PHPMailer. when I am trying to display values that are selected by user it only display as array. can somebody help me to find what is wrong in this code?

Below is the part of HTML

<input type="checkbox" name="Favorite_Drink[]" value="Vodka">Vodka
<input type="checkbox" name="Favorite_Drink[]" value="Vodka">Vodka
<input type="checkbox" name="Favorite_Drink[]" value="Bear">Bear

Here is PHP part

$Favorite_Drink = $_POST['Favorite_Drink'];
$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('[email protected]', 'User');
$mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
//$mail->addAddress('[email protected]');               // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
$mail->Subject = 'Here is the subject';
//$mail->Body    = "Name of patient is ";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true); 
$mail->Subject = ' Test';
$mail->Body    = <<<EOF
<html><body>

    <br>
    <table rules="all" style="border-color: #666;" cellpadding="10">
        <tr style='background: #eee;'>
            <td>Favorite_Drink </td>
            <td> echo implode(" ",$Favorite_Drink)."<br>";</td>
        </tr>
</table>
</body>
</html>
EOF;

            //Altbody is used to display plain text message for those email viewers which are not HTML compatible

$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
        }
 }
1
  • can you var_dump($Favorite_Drink); ?? Commented Jan 23, 2017 at 5:30

2 Answers 2

3

PHP statements aren't executed inside a here-doc. So it doesn't execute echo or call the implode() function. It just expands variables, so when you use $Favorite_Drink it gets expanded to Array.

You should put the result of implode() into a variable first.

$drinks = implode(" ", $Favorite_Drink);
$mail->Body    = <<<EOF
<html><body>

    <br>
    <table rules="all" style="border-color: #666;" cellpadding="10">
        <tr style='background: #eee;'>
            <td>Favorite_Drink </td>
            <td> $drinks <br></td>
        </tr>
</table>
</body>
</html>
EOF;
Sign up to request clarification or add additional context in comments.

1 Comment

for the definition +1. I got a clear picture of here-doc
-1
<input type="checkbox" name="Favorite_Drink['checkBox']" value="Vodka">

Now this index can be used to get the value. Apply this to all check-boxes

1 Comment

If he does this, he won't get all of them. Using name=xxx[] tells PHP to combine all the inputs into an indexed array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.