Send unicode mail using HTML and PHP
The following PHP script resides on the server.
<?php
$msg =
mail(
/* To */ 'name@domain',
/* Subject */ '=?UTF-8?B?' . base64_encode($_POST['subject']) . '?=',
/* Message */ "<html><body><pre>{$_POST['message']}</pre></body></html>",
/* Header */ "MIME-Version: 1.0
Content-type: text/html; charset=UTF-8
From: {$_POST['from']}
"
) ?
'Your message was successfully sent' :
'There was an error sending your message';
echo <<<EOT
<!DOCTYPE html>
<html>
<body>
<script>
alert('$msg');
window.location = "http://{$_SERVER[HTTP_HOST]}/{$_POST['url']}";
</script>
<noscript>
<p>$msg</p>
<p><a href="http://{$_SERVER[HTTP_HOST]}/{$_POST['url']}">Return to the page I was viewing</a></p>
</noscript>
</body>
</html>
EOT;
?>
An HTML form calls the PHP script, supplying it with the necessary information.
<form action="SendMail.php" method="post">
<input type="hidden" name="url" value="ContactMe.html" />
<table style="padding:1em 1em; border-width:2px; border-style:groove">
<tr><th>Your e-mail address:</th><td><input type="email" name="from" size="60" /></td></tr>
<tr><th>
<select name="subject">
<option selected>Make a donation</option>
<option>Report an error</option>
<option>Other</option>
</select>
</th><td></td></tr>
<tr><th>Message:</th><td></td></tr>
<tr><td colspan="2"><textarea name="message" rows="20" cols="58"></textarea></td></tr>
<tr><td colspan="2" style="text-align:center"><br><button>Send</button></td></tr>
</table>
</form>
name@domain | The destination e-mail address. |
SendMail.php | The name of the PHP script. |
ContactMe.html | The HTML page to return to on completion. |
from, message, subject and url are the parameter names which are used by the form to transfer the information to the PHP script.
The names can be anything you like but obviously the script and the PHP must correspond.