【问题标题】:php send html email with .csv attachmentphp 发送带有 .csv 附件的 html 电子邮件
【发布时间】:2012-05-08 17:05:05
【问题描述】:

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

$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);

【问题讨论】:

  • 是的,我看到了那篇帖子,但它没有解释什么是我认为我正在寻找的多部分消息之类的内容

标签: php csv html-email email-attachments


【解决方案1】:
 $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);

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

【讨论】:

  • 它是否适用于简单的边界,例如“Bound bound bound”?
  • 为什么要设置自己的边界?代码计算你一个不错的。
  • 这是我在没有附件的电子邮件中收到的内容:MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xaa04eec9f70b8e7b42496fa47f70257dx" This is a multi-part message in MIME format. --==Multipart_Boundary_xaa04eec9f70b8e7b42496fa47f70257dx Content-Type:text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_xaa04eec9f70b8e7b42496fa47f70257dx Content-Type: text/csv; name="" Content-Transfer-Encoding: base64 TUFLRSxNT0RFTA0KQU1BTkEsSERDMTgyDQo= --==Multipart_Boundary_xaa04eec9f70b8e7b42496fa47f70257dx--
  • 该代码用于一个附件。要发送不带附件的电子邮件,您需要更改并删除一些行。它仍然会发送电子邮件,但正如您所见,标题现在变得有点复杂和不必要。尝试使用文件:)
  • 我相信这个答案现在已经过时了。我能够在这里找到问题的解决方案:stackoverflow.com/questions/30887610/…
【解决方案2】:

您将 HTTP 标头与邮件混淆了......您可能想要发送 multipart messages,但我不想重新实现它,而是使用类似 PHPMailer 的东西。

【讨论】:

    【解决方案3】:

    此代码适用于我。你可以试试这个。

    <?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,\r\n do you got attachment?\r\n\r\Solomon";
    $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 . ">\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/plain; 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: application/octet-stream; name=\"" . $filename2 . "\"\r\n"; 
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"" . $filename2 . "\"\r\n\r\n";
    $header .= $content . "\r\n\r\n";
    $header .= "--" . $uid . "--";
    mail($mailto, $subject, "", $header)
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2013-11-26
      • 2019-11-21
      • 2019-05-28
      • 1970-01-01
      • 2020-11-20
      • 2014-02-28
      • 2017-09-06
      • 2012-06-15
      相关资源
      最近更新 更多