How To Send MYSQL Data To An Email

After the customer fills the form, the form data is sent to mysql and an email gets sent to me with the last form data that the customer submitted. But, the email "last mysql data" is not going as inline text. Please help me. Sample code is given below.


<?php

define('DB_NAME', 'XXXXXXX');

define('DB_USER', 'XXXXXXX');

define('DB_PASSWORD', 'XXXXXXX');

define('DB_HOST', 'localhost');

$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {

die('Could not connect: ' . mysql_error());

}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {

die('Can\'t use ' . DB_NAME . ': ' . mysql_error());

}

//Start Posting the data in Mysql database from Form Input

$value = $_POST['input1'];

$value2 = $_POST['MAmount'];

$sql = "INSERT INTO demo (input1, MAmount) VALUES ('$value', '$value2')";

if (!mysql_query($sql)) {

die('Error: ' . mysql_error());

}

//start print the database

$data = mysql_query("SELECT * FROM demo ORDER BY ID DESC LIMIT 1")

or die(mysql_error());

Print "<table border cellpadding=3>";

while($info = mysql_fetch_array( $data ))

{

Print "<tr>";

Print "<th>ID:</th> <td>".$info['ID'] . "</td> ";

Print "<th>Input1:</th> <td>".$info['input1'] . "</td> ";

Print "<th>MAmount:</th> <td>".$info['MAmount'] . " </td></tr>";

}

Print "</table>";

mysql_close();

//end print the database on form processing page

//start emailing the data

date_default_timezone_set('Asia/Kolkata');

require_once('class.phpmailer.php');

//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();

//$body = "gdssdh";

//$body = preg_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP

$mail->Host = "ssl://XXXXXXX.XXXXXXX.org"; // SMTP server

$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)

// 1 = errors and messages

// 2 = messages only

$mail->SMTPAuth = true; // enable SMTP authentication

$mail->SMTPSecure = "ssl"; // sets the prefix to the servier

$mail->Host = " www.astaguru.com "; // sets GMAIL as the SMTP server

$mail->Port = 465; // set the SMTP port for the GMAIL server

$mail->Username = "XXXXXXX.com"; // GMAIL username

$mail->Password = "XXXXXXX"; // GMAIL password

$mail->SetFrom('contact@XXXXXXXX.com', 'HAL');

//$mail->AddReplyTo("XXXXXXX', 'First Last");

$mail->Subject = "XXXXXXX";

//THE PROBLEM IS HERE WHEN I WANT TO SEND THE DATA AS INLINE TEXT TO EMAIL FROM MYSQL IT IS NOT WORKING. ONLY "PRINT THE DATA" IS SENDING TO EMAIL.

$body = 'Print the data';

mysql_connect("localhost","XXXXXXX","XXXXXXX");

@mysql_select_db("XXXXXXX");

$query["SELECT * FROM demo ORDER BY ID DESC LIMIT 1"];

$result = mysql_query($query);

//while ($row = mysql_fetch_array ($result)) {

// $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "XXXXXXX";

$mail->AddAddress($address, "user2");

//$mail->AddAttachment("images/phpmailer.gif"); // attachment

//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {

echo "Mailer Error: " . $mail->ErrorInfo;

} else {

echo "Message sent!";

}

?>




3 Replies 1 reply marked as answer

VM Vishwanathan Muruganantham Syncfusion Team January 15, 2025 11:55 AM UTC

Hi Nick,


Greetings from Syncfusion support.


We have reviewed your query and understand that you are facing an issue where you are unable to send the last inserted row from the MySQL database as the body of the email. We have analyzed the problem and found a solution. The main issue in your code was that the email body was not being populated with the actual data from the MySQL database. We suggest fetching the last inserted row from the demo table and storing it in the $info array. Additionally, if you are using Syncfusion components in your projects, please share the details with us, and we will provide updates based on that.


Refer to the code snippet below for reference.


….

 

<?php

 

define('DB_NAME', 'XXXXXXX');

define('DB_USER', 'XXXXXXX');

define('DB_PASSWORD', 'XXXXXXX');

define('DB_HOST', 'localhost');

 

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

 

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {

    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());

}

 

// Start Posting the data in Mysql database from Form Input

$value = $_POST['input1'];

$value2 = $_POST['MAmount'];

 

$sql = "INSERT INTO demo (input1, MAmount) VALUES ('$value', '$value2')";

if (!mysql_query($sql)) {

    die('Error: ' . mysql_error());

}

 

// Fetch the last inserted data

$data = mysql_query("SELECT * FROM demo ORDER BY ID DESC LIMIT 1") or die(mysql_error());

$info = mysql_fetch_array($data);

 

// Prepare the email body with the fetched data

$body = "<table border='1' cellpadding='3'>

            <tr>

                <th>ID:</th> <td>{$info['ID']}</td>

            </tr>

            <tr>

                <th>Input1:</th> <td>{$info['input1']}</td>

            </tr>

            <tr>

                <th>MAmount:</th> <td>{$info['MAmount']}</td>

            </tr>

        </table>";

 

mysql_close();

 

// Start emailing the data

date_default_timezone_set('Asia/Kolkata');

require_once('class.phpmailer.php');

 

$mail = new PHPMailer();

$mail->IsSMTP();

$mail->Host = "ssl://XXXXXXX.XXXXXXX.org";

$mail->SMTPDebug = 1;

$mail->SMTPAuth = true;

$mail->SMTPSecure = "ssl";

$mail->Host = "www.astaguru.com";

$mail->Port = 465;

$mail->Username = "XXXXXXX.com";

$mail->Password = "XXXXXXX";

$mail->SetFrom('contact@XXXXXXXX.com', 'HAL');

$mail->Subject = "XXXXXXX";

 

// Set the email body

$mail->MsgHTML($body);

 

$address = "XXXXXXX";

$mail->AddAddress($address, "user2");

 

if (!$mail->Send()) {

    echo "Mailer Error: " . $mail->ErrorInfo;

} else {

    echo "Message sent!";

}

?>

….


Regards,
Vishwanathan


Marked as answer

ND Nick Douglas replied to Vishwanathan Muruganantham January 15, 2025 12:28 PM UTC

Hi Vishwanathan,

Thank you for your detailed response.

I will definitely try fetching the last inserted data and use it to populate the email body as you suggested. The approach with using the $info array to get the latest form submission and include it in the email looks great. I will also ensure that the email body is formatted as an HTML table, as you recommended, to send the data inline.

I’ll implement this and let you know if I run into any further issues. Thanks again for your help!



VM Vishwanathan Muruganantham Syncfusion Team January 16, 2025 04:07 AM UTC

Hi Nick,


Thanks for the update. We will wait to hear from you.


Best Regards,
Vishwanathan


Loader.
Up arrow icon