我有PHP代碼發送帶附件的電子郵件,但我想要的是將所有數據,如全名,聯繫電話等等放入表中。我嘗試使用<table>
,但電子郵件仍然是純文本。有人可以幫我怎麼做嗎?以html格式發送電子郵件附件
這是我的PHP代碼:
<?php
$cname = $_POST['Name'];
$cnumber = $_POST['Tel'];
$cemail = $_POST['emailadd'];
$cmess = $_POST['contactmess'];
$index = 'index.php';
move_uploaded_file($_FILES["attachment"]["tmp_name"] , "/files/upload/" . $_FILES["attachment"]["name"]);
$to = '[email protected]';
$subject = 'Careers Inquiry';
$random_hash = md5(date('r', time()));
$headers = "From: ". $cemail ."\r\nReply-To:". $cemail;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$headers .= "Content-Type: text/html; charset='iso-8859-1'";
$attachment = chunk_split(base64_encode(file_get_contents("/files/upload/" .$_FILES["attachment"]["name"])));
ob_start();
?>
--PHP-mixed-`<?php echo $random_hash; ?> `
Content-Type: multipart/alternative; boundary="PHP-alt-`<?php echo $random_hash; ?>`"
--PHP-alt-`<?php echo $random_hash; ?>`
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
--PHP-alt-`<?php echo $random_hash; ?>`
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>
<body>
<table>
<tr><td>Name: </td><td><?php echo $cname; ?></td></tr>
<tr><td>Contact Number: </td><td><?php echo $cnumber; ?></td></tr>
<tr><td>Email Address: </td><td><?php echo $cemail; ?></td></tr>
<tr><td>Message: </td><td><?php echo $cmess; ?></td></tr>
</table>
</body>
</html>
--PHP-alt-`<?php echo $random_hash; ?>`--
--PHP-mixed-`<?php echo $random_hash; ?>`
Content-Type: application/octet-stream; name="`<?php echo $_FILES["attachment"]["name"];?>`"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
`<?php echo $attachment; ?>`
--PHP-mixed-`<?php echo $random_hash; ?>`--
<?php
$message = ob_get_clean();
$mail_sent = @mail($to, $subject, $message, $headers);
unlink('/files/upload/' . $_FILES["attachment"]["name"]);
echo $mail_sent ? header('Location: '. $index): '<script type="text/javascript"> alert("Sorry, service temporary unavailable."); </script>';
?>
[不要重新發明輪子](https://github.com/Synchro/PHPMailer)。 –