Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2013, 06:36 PM   PM User | #1
sandipcd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
sandipcd is an unknown quantity at this point
Question How to send mysql data to email after submitting the form?

After customer filling the form, the form data will be send to mysql, and an email will sent to me with the last form data that customer submitted. All is working, but only the problem is in the email "last mysql data" is not going as inline text. So, how to do this? I am doing in PHP. In am new in PHP. Please help me.
Sample code is given below.

Code:
<?php

define('DB_NAME', 'sandi565_form11');
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       = "XXXXXXX.XXXXXXX.org";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "contact@XXXXXXX.com";  // GMAIL username
$mail->Password   = "XXXXXXX";            // GMAIL password

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

//$mail->AddReplyTo("user2@gmail.com', 'First Last");

$mail->Subject    = "Halmira 469";

//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("sandi565_form11");
$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@gmail.com";
  $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!";
}

?>
sandipcd is offline   Reply With Quote
Old 01-05-2013, 09:56 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Something like:

PHP Code:
//while ($row = mysql_fetch_array ($result)) {
//  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
$row0 // format and sanitize $row[0]
    
$row1 // format and sanitize $row[1] etc..
    
$body .= $row0 ' ' $row1;
    
// then after the while loop..
  
$mail->MsgHTML($body); 
but, as indicated, you'll need to format, and perhaps sanitize, the database-data.

You appear not to have the closing bracket } for your while loop.

NB You have made no attempt to sanitize, or check, your post-data before inserting it into the database;
You are using the deprecated/ discouraged mysql library.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
sandipcd (01-06-2013)
Old 01-06-2013, 10:39 AM   PM User | #3
sandipcd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
sandipcd is an unknown quantity at this point
Question

Sir, can you please tell me, which portion of data I should format and sanitize? And where I should give the closing bracket }?
I am very new in PHP Mysql, please help.
sandipcd is offline   Reply With Quote
Old 01-06-2013, 04:43 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
PHP Code:
}    <<< I believe the closing bracket should be here
if(!$mail->Send()) {
  echo 
"Mailer Error: " $mail->ErrorInfo;
} else {
  echo 
"Message sent!";

I cannot tell you how to format, and possibly sanitize, your data as I don't know what the data looks like that you are retrieving from your database.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
sandipcd (01-06-2013)
Old 01-06-2013, 05:51 PM   PM User | #5
sandipcd
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
sandipcd is an unknown quantity at this point
Question

Sir, please see the print screen of my database. I am using WAMP for phpMyAdmin. Apache version 2.2.22, PHP Version 5.4.3 , MySQL Version 5.5.24. Please if you let me know where I can improve my database, your help will be very much appreciated.

sandipcd is offline   Reply With Quote
Old 01-06-2013, 06:23 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Well, I suppose if I assume that your fields input1 and MAmount are just plain text, and you just want to insert them into the email, then you could just do:

PHP Code:
//while ($row = mysql_fetch_array ($result)) { 
//  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
    
$row1 $row[1];
    
$row2 $row[2]; 
    
$body .= $row1 ' ' $row2 '\n'// add a newline between each row in the email
    // then after the while loop.. 
  
$mail->MsgHTML($body); 
If no formatting of the text is required then you could even dispense with $row1 and 2, and just:

Code:
    $body .= $row[1] . ' ' . $row[2];
If you look at the resultant email you may then decide that you want to tidy up the message with spaces and newlines.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
sandipcd (01-06-2013)
Reply

Bookmarks

Tags
email, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.