我需要发送一封包含HTML格式数据和excel表格附件的邮件。我可以发送邮件,但它不支持HTML格式数据。这是我的代码:
<?php
$cmpy_name = $_POST["cmpy_name"];
$business = $_POST["business"];
$name = $_POST["name"];
$mb_no = $_POST["mb_no"];
$no_emp = $_POST["no_emp"];
$emp_family = $_POST["emp_family"];
$insu_name = $_POST["insu_name"];
$insu_edate = $_POST["insu_edate"];
$exp_years = $_POST["exp_years"];
$insu_sum = $_POST["insu_sum"];
$insu_coverage= $_POST["insu_coverage"];
$count=count($insu_coverage);
for($i=0;$i<$count;$i++)
{
$coverage="$coverage$insu_coverage[$i], ";
}
$f_upload = $_POST["f_upload"];
$p_name = $_POST["p_name"];
$p_mail = $_POST["p_mail"];
$p_mb = $_POST["p_mb"];
$p_date = $_POST["p_date"];
$p_time = $_POST["p_time"];
$arr=array ($p_date,$p_time);//,$health_ap);
$g_time=implode(" ",$arr);
$p_auth = $_POST["p_auth"];
$email_message .= "<html><body><table width='100%'>";
$email_message .= "<tr><td>Company Name</td><td>:</td><td>$cmpy_name</td></tr>";
$email_message .= "<tr><td>Nature of business</td><td>:</td><td>$business</td></tr>";
$email_message .= "<tr><td>Person Name</td><td>:</td><td>$name</td></tr>";
$email_message .= "<tr><td>Mobile Number</td><td>:</td><td>$mb_no</td></tr>";
$email_message .= "<tr><td>Nature of business</td><td>:</td><td>$no_emp</td></tr>";
$email_message .= "<tr><td>Required for employee family members</td><td>:</td><td>$emp_family</td></tr>";
$email_message .= "<tr><td>Previous insurance name</td><td>:</td><td>$insu_name</td></tr>";
$email_message .= "<tr><td>Previous insurance expiry date</td><td>:</td><td>$insu_edate</td></tr>";
$email_message .= "<tr><td>Claim experience in last three years</td><td>:</td><td>$exp_years</td></tr>";
$email_message .= "<tr><td>Sum insured</td><td>:</td><td>$insu_sum</td></tr>";
$email_message .= "<tr><td>Coverage</td><td>:</td><td>$coverage</td></tr>";
$email_message .= "<tr><td>Personal Information</td></tr>";
$email_message .= "<tr><td>Name</td><td>:</td><td>$p_name</td></tr>";
$email_message .= "<tr><td>Email-ID</td><td>:</td><td>$p_mail</td></tr>";
$email_message .= "<tr><td>Mobile Number</td><td>:</td><td>$p_mb</td></tr>";
$email_message .= "<tr><td>Convinent time to contact</td><td>:</td><td>$g_time</td></tr>";
$email_message.= "</table></body></html>";
$to = "sangeetha.lrl2014@gmail.com";
$fromEmail = "sangee.selvi@gmail.com";
$fromName = "lrl";
$subject = "Group mediclaim";
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
/* GET File Variables */
$tmpName = $_FILES['f_upload']['tmp_name'];
$fileType = $_FILES['f_upload']['type'];
$fileName = $_FILES['f_upload']['name'];
/* Start of headers */
$headers = "From: $fromName";
if(in_array($_FILES['f_upload']['type'],$mimes)){
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$email_message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
else {
die("Sorry, mime type not allowed");
}
$flgchk = mail ("$to", "$subject", "$email_message", "$headers");
if($flgchk){
echo "A email has been sent to: $to";
}
else{
echo "Error in Email sending";
}
?>
我需要提供哪个标头来支持HTML格式数据和附件?
答案 0 :(得分:1)
更改内容类型:text / plain;到
内容类型:text / html;
在你的第一个$ email_message
答案 1 :(得分:1)
我建议使用phpmailer,这对于处理所有内容来说非常简单易行。
答案 2 :(得分:0)
也许是多部分/混合?
"Content-Type: multipart/mixed; boundary=\"$bound_text\"";