如何通过PHP mail()函数发送检索到的表
下面的代码给出了屏幕截图中的输出。我想通过php邮件功能发送此内容。当我尝试将此代码添加到$message中时,它不起作用,因为它在html代码之间包含php代码(用于从表中检索数据)。请帮助我们如何做到这一点?如何通过PHP mail()函数发送检索到的表,php,mysql,Php,Mysql,下面的代码给出了屏幕截图中的输出。我想通过php邮件功能发送此内容。当我尝试将此代码添加到$message中时,它不起作用,因为它在html代码之间包含php代码(用于从表中检索数据)。请帮助我们如何做到这一点? 这是我尝试发送电子邮件的代码。 我建议在字符串外循环,然后将结果连接起来,如下所示: $string = ''; // The variable used to contain your data $records = mysql_Something is wrong("SELEC
这是我尝试发送电子邮件的代码。
我建议在字符串外循环,然后将结果连接起来,如下所示:
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
//Using the variable in your message
$message = '
...
'. $string .'
...
';
$string='';//用于包含数据的变量
$records=mysql\u出现问题(“按兰德(curdate())限制1)从印地安订单中选择*”;
而($course=mysql\u fetch\u assoc($records)){
//填充该变量
$string.=“”;
$string.=''.$course['hindiword'].'..$course['拼音'.'.'.'.'..$course['id'.''.'';'';
}
//在消息中使用变量
$message='1
...
“.$string。”
...
';
这样,无论您想在哪里添加$string,只要将其连接起来,就好像您试图在一个字符串中执行PHP代码一样,它将被视为一个字符串,而所讨论的PHP将不会被执行
因此,最终结果如下所示:
<?php
include('db.php'); //connection to database
$to = 'example@gmail.com';
$subject = "test php mail" ;
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
$message = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta charset="utf-8">
<title> Fetching data </title>
</head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:#DC143C;
}
td {
color:#0000FF;
}
</style>
<body>
<font size="4" face="Courier New" >
<table border="1" style="width:50%" align="center">
<tr bgcolor="#2ECCFA">
<th style="padding: 20px" bgcolor="#E6E6FA"
color="#DC143C" color="red">Word of the Day</th>
</tr>
<!-- I used while loop to fetch data and display rows of date on html
table -->
'. $string .'
</table>
</body>
</html> ' ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: abc@gmail.com.com>' . "\r\n";
// Send email
if(mail($to,$subject,$message,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
您可以添加邮件代码吗?您还需要在PHP的一个变量中添加邮件,例如$message='anycontaint';我已经添加了我正在尝试的代码。请帮助完成以下$message='Word of the Day'$records=mysql_查询(“SELECT*FROM hindiday order by rand(curdate())limit 1”);而($course=mysql\u fetch\u assoc($records)){$message.==;$message.=.$course['hindiword'].;$message.==;}$message.=''当然不起作用了@KeerthanaA,因为你没有做我在回答中所做的事情。我将发布完整的代码答案。
<?php
include('db.php'); //connection to database
$to = 'example@gmail.com';
$subject = "test php mail" ;
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
$message = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta charset="utf-8">
<title> Fetching data </title>
</head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:#DC143C;
}
td {
color:#0000FF;
}
</style>
<body>
<font size="4" face="Courier New" >
<table border="1" style="width:50%" align="center">
<tr bgcolor="#2ECCFA">
<th style="padding: 20px" bgcolor="#E6E6FA"
color="#DC143C" color="red">Word of the Day</th>
</tr>
<!-- I used while loop to fetch data and display rows of date on html
table -->
'. $string .'
</table>
</body>
</html> ' ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: abc@gmail.com.com>' . "\r\n";
// Send email
if(mail($to,$subject,$message,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>