09 July 2014

How to put online form data in a table, before sending it to an email address?

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

Chinese displayed as gibberish

To correctly display Chinese, 4 things should be done –

1. When creating the database, Chinese should be defined –

create database dbsettle character set 'gbk' collate 'gbk_chinese_ci';

use dbsettle;

2. On the web page, in the header section, Chinese should be defined –

<meta http-equiv="content-type" content="charset=gbk" />

3. In the programme code, after having connected to the database, Chinese should be defined –

mysql_select_db("dbsettle",$db);

mysql_query("set names gbk", $db);

4. In the sending email section of the code, content type should be defined –

// Always set content-type when sending HTML email

$headers = "MIME-Version: 1.0" . "\r\n";

$headers .= "Content-type:text/html;charset=gbk" . "\r\n";

// More headers

$headers .= 'From: xxxx@cicscanada.com' . "\r\n";

// $headers .= 'Cc: myboss@example.com' . "\r\n";

        // send mail

        mail($to, $subject, $message, $headers)

If all the above is done, then Chinese will correctly be displayed in the email.