[英]Send HTML table with data via mail in php
我的网站上有一个 php 代码,用于发送邮件联系我们部分。 我想在邮件正文中以表格格式显示用户的输入。
代码
<?php
try{
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$to = "abc@gmail.com";
$response = mail($to, 'Query from website',
"<table>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Email-Id</th>
<th>Comments</th>
</tr>
<tr>
<td>'.$name'</td>
<td>'.$phone'</td>
<td>'.$email'</td>
<td>.'$comments'</td>
</tr>
</table>
");
print_r($response);
} catch(Exception $e){
print_r($e);
}
?>
问题是整个 html 代码出现在邮件正文中。
请参阅以下示例,如 PHP 手册页中所述。
<?php
// Multiple recipients
$to = 'johny@example.com, sally@example.com'; // note the comma
// Subject
$subject = 'Birthday Reminders for August';
// Message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Johny</td><td>10th</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
?>
您必须通过向mail
提供标题来告诉mail
您的mail
的内容类型是什么。 mail
函数的签名是:
mail($to, $subject, $message, $headers);
创建一个 content-type 标头并将其传递给mail
:
$headers = "Content-Type: text/html; charset=UTF-8";
我已经稍微改变了你的代码。 您的变量名没有得到值,因为它们在引号 (') 内,而且它们之前和之后都没有 (.)。 您没有使用 content-type 标头来使用这些 HTML 标签来正确呈现。 请参考以下代码。 此外,必须有一个内爆函数来返回这些存储在数组中的标头。
<?php
try{
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$to = "doke.prathamesh1994@gmail.com";
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
$message = '<html><body>';
$message .= '<table border="1">
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Email-Id</th>
<th>Comments</th>
</tr>
<tr>
<td>'.$name.'</td>
<td>'.$phone.'</td>
<td>'.$email.'</td>
<td>'.$comments.'</td>
</tr>
</table>';
$response = mail($to, 'Query from website', $message,implode("\r\n",$headers));
print_r($response);
} catch(Exception $e){
print_r($e);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.