Rajesh Dharmaraj
Rajesh Dharmaraj

Reputation: 21

How can I send fetching data from database via mail

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

When user enter their name and submit based on that name result will be displayed from database in a separate page. I added mail function. Mail function is working. But How to send mail with displaying that result(fetching data from database) in PHP

$subject2 = "Copy of your form submission";
$message = $row["description"];

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

Upvotes: 1

Views: 2266

Answers (3)

Ashu
Ashu

Reputation: 1320

First:

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

If the array contains a value, then go with this:

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

Here you get the result from an associative array. Store it in a variable like this:

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

Upvotes: -1

Vahid Alvandi
Vahid Alvandi

Reputation: 612

you can use another and best method for send email , swiftmailer can work in composer and include class for send email. you can send with smtp and in all services google , yahoo , ... dot get spam folder , just in inbox

   //Create the Transport.
    $transport = \Swift_SmtpTransport::newInstance('smtp.ddd.com', 587, 'tls')
        ->setUsername('[email protected]')
        ->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('[email protected]')
      ->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";
    }

Upvotes: 0

Vivek
Vivek

Reputation: 76

Try with this

$to = '[email protected]';
$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: [email protected]\n";
$message = $row["description"];

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

Upvotes: 2

Related Questions