Mail Csv File as attachment

<?php

$email = “test@bdchatsites.com”;
$firstName = “Bangladesh”;
$lastName = “Dhaka”;

$to = “test@test.com”;

$subject = “Test”;

$message = “”.
“Email: $email” . “\n” .
“First Name: $firstName” . “\n” .
“Last Name: $lastName”;

//The Attachment

$cr = “\n”;
$data = “Email” . ‘,’ . “First Name” . ‘,’ . “Last Name” . $cr;
$data .= “$email” . ‘,’ . “$firstName” . ‘,’ . “$lastName” . $cr;
$fp = fopen(‘file.csv’,’a’);
fwrite($fp,$data);
fclose($fp);

$attachments[] = Array(
‘data’ => $data,
‘name’ => ‘file.csv’,
‘type’ => ‘application/vnd.ms-excel’
);

//Generate a boundary string

$semi_rand = md5(time());
$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;

//Add the headers for a file attachment

$headers = “MIME-Version: 1.0\n” .
“From: {$from}\n” .
“Cc: emaito@domain.com\n”.
“Content-Type: multipart/mixed;\n” .
” boundary=\”{$mime_boundary}\””;

//Add a multipart boundary above the plain message

$message = “This is a multi-part message in MIME format.\n\n” .
“–{$mime_boundary}\n” .
“Content-Type: text/html; charset=\”iso-8859-1\”\n” .
“Content-Transfer-Encoding: 7bit\n\n” .
$text . “\n\n”;

//Add sttachments

foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment[‘data’]));
$name = $attachment[‘name’];
$type = $attachment[‘type’];

$message .= “–{$mime_boundary}\n” .
“Content-Type: {$type};\n” .
” name=\”{$name}\”\n” .
“Content-Transfer-Encoding: base64\n\n” .
$data . “\n\n” ;
}

$message .= “–{$mime_boundary}–\n”;
mail($to, $subject, $message, $headers);

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s