如何通过邮件发送从数据库中获取的数据

How can I send fetching data from database via mail

<?php       
    while($row = $result->fetch_assoc()) 
    {
        echo "<br>  ". $row["description"]. " <br>";
    } 
?>

当用户输入他们的姓名并根据该姓名提交时,结果将从数据库中显示在单独的页面中。我添加了邮件功能。邮件功能正在运行。但是如何发送邮件并在 PHP

中显示该结果(从数据库中获取数据)
$subject2 = "Copy of your form submission";
$message = $row["description"];

mail($to,$subject,$message,$headers);  

试试这个

$to = 'You@example.com';
$subject = "Copy of your form submission";

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: you@example.com\n";
$message = $row["description"];

mail($to,$subject,$message,$headers);  

第一个:

var_dump($result->fetch_assoc());exit;

如果数组包含一个值,则使用此:

<?php       
    while($row = $result->fetch_assoc()) 
    {
        echo "<br>  ". $row["description"]. " <br>";
    } 
?

此处您从关联数组中获取结果。将其存储在这样的变量中:

//store data in Description variable
$description='';
<?php       
    while($row = $result->fetch_assoc()) 
    {
        $description=$description."<br>".$row["description"]."<br>";
    } 
?>

//send mail
mail($to,$subject,$description,$headers);

您可以使用另一种最好的方法来发送电子邮件,swiftmailer 可以在 composer 中使用并包含 class 来发送电子邮件。 您可以使用 smtp 和所有服务发送 google , yahoo , ... dot get spam folder , just in inbox

   //Create the Transport.
    $transport = \Swift_SmtpTransport::newInstance('smtp.ddd.com', 587, 'tls')
        ->setUsername('noreply@ddd.com')
        ->setPassword('iFv0(W6Ltl=r')
        ->setStreamOptions(array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        )
        );

    //Create the message
    $message = \Swift_Message::newInstance();

    //Give the message a subject
    $message->setSubject($title)
      ->setFrom('noreply@ddd.com')
      ->setTo($email) 
      ->setBody($title, 'text/html')
      ->addPart($body, 'text/html');

    //Create the Mailer using your created Transport
    $mailer = \Swift_Mailer::newInstance($transport);

    //Send the message
    $result = $mailer->send($message);

    if ($result) {
      echo "Email sent successfully";
    }
    else
    {
      echo "Email failed to send";
    }