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

时间:2017-01-13 10:07:40

标签: php mysql phpmailer

<?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);  
  • 邮件正在发送空。它没有显示从数据库中获取数据。

3 个答案:

答案 0 :(得分:2)

试试这个

$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);  

答案 1 :(得分:0)

您可以使用另一种最佳方法发送电子邮件,swiftmailer可以在作曲家中使用,并包含发送电子邮件的课程。 你可以发送smtp和所有服务谷歌,雅虎,...点到垃圾邮件文件夹,只是在收件箱

   //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";
    }

答案 2 :(得分:-1)

首先:

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);