我正在通过php发送电子邮件,没问题。 我想附加一个.csv文件以及提供的 HTML 电子邮件。 我似乎找不到该怎么做。 我是否还必须在$headers
中包含某种内容类型以及附件Content-Disposition
?
$to = 'email@email.com';
$subject = 'A Subject Line';
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$message = "
<html>
<head>
<title>List of New Price Changes</title>
</head>
<body>";
$message .="HTML table";
$message .="</body></html>";
mail($to, $subject, $message, $headers);
$fileatt_type = "text/csv";
$myfile = "myfile.csv";
$file_size = filesize($myfile);
$handle = fopen($myfile, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$message = "<html>
<head>
<title>List of New Price Changes</title>
</head>
<body><table><tr><td>MAKE</td></tr></table></body></html>";
$uid = md5(uniqid(time()));
#$header = "From: ".$from_name." <".$from_mail.">rn";
#$header .= "Reply-To: ".$replyto."rn";
$header .= "MIME-Version: 1.0rn";
$header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
$header .= "This is a multi-part message in MIME format.rn";
$header .= "--".$uid."rn";
$header .= "Content-type:text/html; charset=iso-8859-1rn";
$header .= "Content-Transfer-Encoding: 7bitrnrn";
$header .= $message."rnrn";
$header .= "--".$uid."rn";
$header .= "Content-Type: text/csv; name="".$myfile.""rn"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64rn";
$header .= "Content-Disposition: attachment; filename="".$myfile.""rnrn";
$header .= $content."rnrn";
$header .= "--".$uid."--";
mail($to, $subject, $message, $header);
尝试根据您的情况修改代码。
您将HTTP标头与邮件混淆了。您可能希望发送多部分消息,但与其重新实现它,我会选择像 PHPMailer 这样的东西。
这段代码对我有用。你可以试试这个。
<?php
$con = mysqli_connect('localhost', 'username', 'password', 'databasename');
if (!$con)
{
die("error" . mysqli_connect_error());
}
error_reporting(E_ERROR);
$filename = "adhaar_info";
$sql = mysqli_query($con, "SELECT * FROM adhaar_info order by id desc limit 0,10");
$row = mysqli_fetch_assoc($sql);
$filename2='datas/'.$filename.'.csv';
$fp = fopen($filename2, "w");
$seperator = "";
$comma = "";
foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $name);$comma = ",";}
$seperator .= "n";
$seperator;
fputs($fp, $seperator);
mysqli_data_seek($sql, 0);
while ($row = mysqli_fetch_assoc($sql))
{
$seperator = "";
$comma = "";
foreach ($row as $name => $value){$seperator .= $comma . '' . str_replace('', '""', $value);$comma = ",";}
$seperator .= "n";
fputs($fp, $seperator);
}
fclose($fp);
$my_file = $filename2;
$path = "datas/";
$from_name = "solomon";
$from_mail = "pss@gmail.com";
$mailto = "pssworkcse@gmail.com";
$subject = "This is a mail with attachment.";
$message = "Hi,rn do you got attachment?rnrSolomon";
$replyto = "pssworkcse@gmail.com";
$file = $my_file;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: " . $from_name . " <" . $from_mail . ">rn";
$header .= "Reply-To: " . $replyto . "rn";
$header .= "MIME-Version: 1.0rn";
$header .= "Content-Type: multipart/mixed; boundary="" . $uid . ""rnrn";
$header .= "This is a multi-part message in MIME format.rn";
$header .= "--" . $uid . "rn";
$header .= "Content-type:text/plain; charset=iso-8859-1rn";
$header .= "Content-Transfer-Encoding: 7bitrnrn";
$header .= $message . "rnrn";
$header .= "--" . $uid . "rn";
$header .= "Content-Type: application/octet-stream; name="" . $filename2 . ""rn";
$header .= "Content-Transfer-Encoding: base64rn";
$header .= "Content-Disposition: attachment; filename="" . $filename2 . ""rnrn";
$header .= $content . "rnrn";
$header .= "--" . $uid . "--";
mail($mailto, $subject, "", $header)
?>