如何在提交表单后将mysql数据发送到电子邮件? [英] How to send mysql data to email after submitting the form?

查看:111
本文介绍了如何在提交表单后将mysql数据发送到电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户填写表单后,表单数据将发送到mysql,并会向客户发送一封电子邮件,其中包含客户提交的最后一份表单数据。一切正常,但只有问题出现在电子邮件中最后的mysql数据不会像内联文本一样。那么,怎么做呢?我在用PHP做。我是PHP新手。请帮我。

示例代码如下:



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.

<?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!";
}

?>

推荐答案

link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

if (!


link){
die(' 无法连接:' .mysql_error());
}

link) { die('Could not connect: ' . mysql_error()); }


db_selected = mysql_select_db(DB_NAME,
db_selected = mysql_select_db(DB_NAME,


这篇关于如何在提交表单后将mysql数据发送到电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆