6

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?

$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);
ToddN
  • 2,901
  • 14
  • 56
  • 96
  • possible duplicate of [Send CSV file attached to the email](http://stackoverflow.com/questions/5816421/send-csv-file-attached-to-the-email) – nickb May 08 '12 at 17:09
  • yes I saw that post but it does not explain what does what such as multipart messages which is I think what I am looking for – ToddN May 08 '12 at 17:15

3 Answers3

20
 $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.

ToddN
  • 2,901
  • 14
  • 56
  • 96
Ata S.
  • 890
  • 7
  • 12
  • Will it work for a simple boundary such as "Bound bound bound"? – ToddN May 08 '12 at 17:31
  • 1
    Why do you put your own boundary? The code calculates you a nice one. – Ata S. May 08 '12 at 17:36
  • This is what I get in my email with NO attachement: `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--` – ToddN May 08 '12 at 17:48
  • That code is for a one attachment. To send email with no attachment, you need to change and remove some lines. It would still send the email but as you see the headers get a little complicated and unnecessary now. Try with a file :) – Ata S. May 08 '12 at 17:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11027/discussion-between-toddn-and-ata-s) – ToddN May 08 '12 at 17:53
  • This was exactly what I've been searching for, thanks for the example! – john.w Jun 29 '12 at 19:11
  • 1
    I believe this answer is now outdated. I was able to find a solution to the problem here: https://stackoverflow.com/questions/30887610/error-with-php-mail-multiple-or-malformed-newlines-found-in-additional-header – Josh Whitlow May 23 '18 at 18:50
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.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
1

This code works for me. You can try this.

<?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)

?>
Solomon Suraj
  • 1,162
  • 8
  • 8