【发布时间】:2015-09-03 21:04:37
【问题描述】:
我在通过 PHP 发送邮件时遇到问题。这是我的完整代码:
<?php
if ($_POST){
$num = md5(time());
//MAIL BODY
$body = "
<html>
<head>
<title>CadizCarnaval - Colabora</title>
</head>
<body style='background:#EEE; padding:30px;'>
<h2 style='color:#767676;'>Mensaje recibido sección Colabora.</h2>";
$body .= "
<strong style='color:#0090C6;'>Nombre: </strong>
<span style='color:#767676;'>" . $_POST["nombre"] . "</span><br>";
$body .= "
<strong style='color:#0090C6;'>Email: </strong>
<span style='color:#767676;'>" . $_POST["mail"] . "</span><br>";
$body .= "
<strong style='color:#0090C6;'>Mensaje: </strong>
<span style='color:#767676;'>" . $_POST["mensaje"] . "</span><br>";
$body .= "</body></html>";
$nombre = $_POST['nombre'];
$email = $_POST['mail'];
$asunto = $_POST['asunto'];
$mensaje = $_POST['mensaje'];
$para = 'fff@gmail.com';
// MULTI-HEADERS Content-Type: multipart/mixed and Boundary is mandatory.
$headers = "From: $nombre <$email>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--".$num."\n";
// HTML HEADERS
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$body."\n";
$headers .= "--".$num."\n";
if (isset ($_FILES["archivos"])) {
$tot = count($_FILES["archivos"]["name"]);
for ($i = 0; $i < $tot; $i++){
$_name=$_FILES["archivos"]["name"][$i];
$_type=$_FILES["archivos"]["type"][$i];
$_size=$_FILES["archivos"]["size"][$i];
$_temp=$_FILES["archivos"]["tmp_name"][$i];
//FILES EXISTS
if(strcmp($_name, "")){
$fp = fopen($_temp, "rb");
$file = fread($fp, $_size);
$file = chunk_split(base64_encode($file));
}
// FILES HEADERS
$headers .= "Content-Type:application/octet-stream ";
$headers .= "name=\"".$_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."\r\n";
}
}else { //FILES NO EXISTS
// HTML HEADERS
$headers = "From: $nombre <$email>\r\n";
$headers .= "Ficheros adjuntos no recibidos.\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
}
// SEND MAIL
if (mail($para, $asunto, $body, $headers)){
echo "<script language='javascript'> alert('Mensaje enviado, muchas gracias.'); window.location.href='http://[...]/inicio.html';</script>";
}
else {
echo "Falló el envio";
}
}
?>
正确发送电子邮件,但显示我不想要的 html 代码:
我需要您的帮助来删除最后的 html 代码。谢谢。
【问题讨论】:
-
您检查过 $_POST 中的内容吗?
-
是的,我得到了正确的值,但也显示了我不想要的代码
-
尝试在正文前添加标题
-
对不起,但不工作。谢谢你的回答
-
是邮件后打印HTML的问题吗?或者这些值是空白的?