ds15812330851
2019-07-07 10:18
浏览 99
已采纳

在通过phpmailer发送变量之前将循环等于变量

I have a checkbox form which is sent to mail.php to be mailed. The selected items should be sent via mail. I have a query to output selected items. In mail body; to echo all selected items which pulled from database, i need to equal the while loop to a variable. But i can not manage it to equal.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

include 'nedmin/netting/database.php';

if (isset($_POST['sendmail'])) {

$city=array_keys($_POST['cities']);

$cities=implode(",",$city);


$query = $db->prepare("SELECT * FROM semt WHERE semt_id IN('".$cities."')");

$query->execute();

while ($statement = $query->fetch(PDO::FETCH_ASSOC)) {

?>
   <td><?php echo $statement["city_name"]?></td>

<?php }


$mail = new PHPMailer;

try {
   /* SMTP parameters. */
   $mail->isSMTP();
   $mail->SMTPKeepAlive = TRUE;   
   $mail->SMTPAuth = TRUE;
   $mail->SMTPSecure = 'tls';

   ...
   ...
   ...
   $mail->isHTML(TRUE);
   $mail->Subject = "Subject";
   $mail->Body = "Selected cities: $......";

   $mail->send();
}
catch (Exception $e)
{
   echo $e->errorMessage();
}
catch (\Exception $e)
{
   echo $e->getMessage();
}
}
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douweng9427 2019-07-07 10:30
    已采纳
    $citiesName = [];
    
    while ($statement = $query->fetch(PDO::FETCH_ASSOC)) { ?>
        <td><?php echo $statement["city_name"]?></td>
        <?php $citiesName[] = $statement["city_name"]?>
    <?php }
    
    //code ...
    $mail->Body = "Selected cities: " . implode(',', $citiesName);
    
    点赞 打赏 评论
  • doudengshen5591 2019-07-07 10:28

    Concatenate to a string instead of printing output.

    $city_names = "";
    while ($statement = $query->fetch(PDO::FETCH_ASSOC) {
        $city_names .= "<td>{$statement["city_name"]}</td>
    ";
    }
    

    Then you can use $city_names in the email.

    点赞 打赏 评论

相关推荐 更多相似问题