How To Send MYSQL Data To An Email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rasputin009
    New Member
    • Jan 2025
    • 1

    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_PASS WORD', '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("SE LECT * FROM demo ORDER BY ID DESC LIMIT 1")
    or die(mysql_error ());
    Print "<table border cellpadding=3>" ;
    while($info = mysql_fetch_arr ay( $data ))
    {
    Print "<tr>";
    Print "<th>ID:</th> <td>".$info['ID'] . "</td> ";
    Print "<th>Input1 :</th> <td>".$info['input1'] . "</td> ";
    Print "<th>MAmoun t:</th> <td>".$info['MAmount'] . " </td></tr>";
    }
    Print "</table>";
    mysql_close();
    //end print the database on form processing page
    //start emailing the data
    date_default_ti mezone_set('Asi a/Kolkata');
    require_once('c lass.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 = " emaildomain.com "; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "XXXXXXX.co m"; // GMAIL username
    $mail->Password = "XXXXXXX"; // GMAIL password
    $mail->SetFrom('con ta ct@emaildomain. com ', 'HAL');
    //$mail->AddReplyTo(" XX XXXXX', '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","XXX XXXX","XXXXXXX" );
    @mysql_select_d b("XXXXXXX");
    $query["SELECT * FROM demo ORDER BY ID DESC LIMIT 1"];
    $result = mysql_query($qu ery);
    //while ($row = mysql_fetch_arr ay ($result)) {
    // $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($bod y) ;
    $address = "XXXXXXX";
    $mail->AddAddress($ ad dress, "user2");
    //$mail->AddAttachmen t( "images/phpmailer.gif") ; // attachment
    //$mail->AddAttachmen t( "images/phpmailer_mini. gif"); // attachment
    if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
    echo "Message sent!";
    }
    ?>
    zz0.1ew3fp4s6vz z
    Last edited by Niheel; Jan 10 '25, 11:53 PM. Reason: website information removed
Working...