Skip to content

Instantly share code, notes, and snippets.

@feisuzhu
Created September 27, 2012 13:13
Show Gist options
  • Save feisuzhu/3793912 to your computer and use it in GitHub Desktop.
Save feisuzhu/3793912 to your computer and use it in GitHub Desktop.
PHP: accept form and send as mail
<?php
$str = '';
foreach($_POST as $k => $v) {
if(is_array($v)) {
$v = implode(', ', $v);
}
$str .= "$k: $v\n";
}
$rnd = md5(time() + 'haha');
$headers = <<<EENNDD
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="$rnd";
EENNDD;
$message = <<<EENNDD
--$rnd
Content-Type: text/plain; charset=utf-8
$str
EENNDD;
foreach($_FILES as $k => $f) {
if($f['error']) continue;
$fn = $f['name'];
$type = $f['type'];
$content = chunk_split(base64_encode(file_get_contents($f['tmp_name'])));
$message .= <<<EENNDD
--$rnd
Content-Type: $type; name="$fn"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$content
EENNDD;
}
$message .= "--$rnd--";
if(isset($_POST['姓名'])) {
$tail = ':' . $_POST['姓名'];
} else {
$tail = '';
}
mail('[email protected]', '客户信息' . $tail, $message, $headers);
?>
<script type="text/javascript">
alert("您的信息已经收到!我们会尽快联系您!");
window.location.href = 'index.html';
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment