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

在通过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.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 字符串的比较老是报错
  • ¥15 很简单的c#代码补全
  • ¥15 复杂表达式求值程序里的函数优先级问题
  • ¥15 求密码学的客成社记ji着用
  • ¥35 POI导入树状结构excle
  • ¥15 初学者c语言题目解答
  • ¥15 div editable中的光标问题
  • ¥15 mysql报错1415Not allowed to return a result set from a trigger 不知如何修改
  • ¥60 Python输出Excel数据整理,算法较为复杂
  • ¥15 回答几个问题 关于数据库