469,342 Members | 5,948 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

How To Send MYSQL Data To The Email

After the customer fills the form on the website, 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.



Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. define('DB_NAME', 'XXXXXXX');
  4. define('DB_USER', 'XXXXXXX');
  5. define('DB_PASSWORD', 'XXXXXXX');
  6. define('DB_HOST', 'localhost');
  7.  
  8. $link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
  9.  
  10. if (!$link) {
  11. die('Could not connect: ' . mysql_error());
  12. }
  13.  
  14. $db_selected = mysql_select_db(DB_NAME, $link);
  15.  
  16. if (!$db_selected) {
  17. die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
  18. }
  19.  
  20. //Start Posting the data in Mysql database from Form Input
  21.  
  22. $value = $_POST['input1'];
  23. $value2 = $_POST['MAmount'];
  24.  
  25. $sql = "INSERT INTO demo (input1, MAmount) VALUES ('$value', '$value2')";
  26.  
  27. if (!mysql_query($sql)) {
  28. die('Error: ' . mysql_error());
  29.  
  30. }
  31.  
  32. //start print the database
  33.  
  34. $data = mysql_query("SELECT * FROM demo ORDER BY ID DESC LIMIT 1")
  35. or die(mysql_error());
  36. Print "<table border cellpadding=3>";
  37. while($info = mysql_fetch_array( $data ))
  38. {
  39. Print "<tr>";
  40. Print "<th>ID:</th> <td>".$info['ID'] . "</td> ";
  41. Print "<th>Input1:</th> <td>".$info['input1'] . "</td> ";
  42. Print "<th>MAmount:</th> <td>".$info['MAmount'] . " </td></tr>";
  43. }
  44. Print "</table>";
  45.  
  46. mysql_close();
  47.  
  48.  
  49. //end print the database on form processing page
  50.  
  51. //start emailing the data
  52.  
  53.  
  54. date_default_timezone_set('Asia/Kolkata');
  55.  
  56. require_once('class.phpmailer.php');
  57. //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
  58.  
  59. $mail = new PHPMailer();
  60.  
  61. //$body = "gdssdh";
  62. //$body = preg_replace("[\]",'',$body);
  63.  
  64. $mail->IsSMTP(); // telling the class to use SMTP
  65. $mail->Host = "ssl://XXXXXXX.XXXXXXX.org"; // SMTP server
  66. $mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
  67. // 1 = errors and messages
  68. // 2 = messages only
  69. $mail->SMTPAuth = true; // enable SMTP authentication
  70. $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
  71. $mail->Host = " raunakgroup "; // sets GMAIL as the SMTP server
  72. $mail->Port = 465; // set the SMTP port for the GMAIL server
  73. $mail->Username = "XXXXXXX"; // GMAIL username
  74. $mail->Password = "XXXXXXX"; // GMAIL password
  75.  
  76. $mail->SetFrom('XXXXXXX', 'HAL');
  77.  
  78. //$mail->AddReplyTo("XXXXXXX', 'First Last");
  79.  
  80. $mail->Subject = "XXXXXXX";
  81.  
  82. //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.
  83.  
  84. $body = 'Print the data';
  85. mysql_connect("localhost","XXXXXXX","XXXXXXX");
  86. @mysql_select_db("XXXXXXX");
  87. $query["SELECT * FROM demo ORDER BY ID DESC LIMIT 1"];
  88. $result = mysql_query($query);
  89.  
  90. //while ($row = mysql_fetch_array ($result)) {
  91. // $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  92. $mail->MsgHTML($body);
  93. $address = "XXXXXXX";
  94. $mail->AddAddress($address, "user2");
  95.  
  96. //$mail->AddAttachment("images/phpmailer.gif"); // attachment
  97. //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
  98.  
  99. if(!$mail->Send()) {
  100. echo "Mailer Error: " . $mail->ErrorInfo;
  101. } else {
  102. echo "Message sent!";
  103. }
  104.  
  105. ?>
Feb 29 '20 #1
0 2711

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by jim west via SQLMonster.com | last post: by
5 posts views Thread by sam | last post: by
1 post views Thread by Malli mindwave | last post: by
2 posts views Thread by Malli mindwave | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.