php发送.csv附件的html电子邮件

时间:2022-10-16 12:29:24

I am sending an email through php no problem. I want to attach a .csv file along with the HTML email provided. I can't seem to find how to do that. Is there a certain Content-Type I also have to include in the $headers along with a Content-Disposition of attachment?

我通过PHP发送电子邮件没问题。我想附上.csv文件以及提供的HTML电子邮件。我似乎无法找到如何做到这一点。是否有某种Content-Type I还必须包含在$ headers中以及Content-Disposition of attachment?

$to = 'email@email.com';
$subject = 'A Subject Line';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$message = "
<html>
<head>
  <title>List of New Price Changes</title>
</head>
<body>";

$message .="HTML table";
$message .="</body></html>";

mail($to, $subject, $message, $headers);

2 个解决方案

#1


19  

 $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.">\r\n";
        #$header .= "Reply-To: ".$replyto."\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n"; // use diff. tyoes here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--".$uid."--";

mail($to, $subject, $message, $header);

Try to modify the code for your situation.

尝试根据您的情况修改代码。

#2


1  

You are confusing HTTP headers with mail.... You probably want to send a multipart messages, but instead of reimplementing it, I'd go for something like PHPMailer.

你正在混淆HTTP标题和邮件....你可能想发送一个多部分的消息,但我没有重新实现它,我会去像PHPMailer。

#1


19  

 $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.">\r\n";
        #$header .= "Reply-To: ".$replyto."\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n"; // use diff. tyoes here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--".$uid."--";

mail($to, $subject, $message, $header);

Try to modify the code for your situation.

尝试根据您的情况修改代码。

#2


1  

You are confusing HTTP headers with mail.... You probably want to send a multipart messages, but instead of reimplementing it, I'd go for something like PHPMailer.

你正在混淆HTTP标题和邮件....你可能想发送一个多部分的消息,但我没有重新实现它,我会去像PHPMailer。