In this post I'm going to show you how we can send mail with file attachment using PHP mail function. To do this we need to encode file into base64 encoding system using PHPbase64_encode function and add it to PHP mail header parameter with MIME.
function email($mail_from, $from_name, $mail_to, $subject, $message, $file){
$file_name = basename($file); // Get file name
$data = file_get_contents($file); // Read file contents
$file_contents = chunk_split(base64_encode($data)); // Encode file data into base64
$uid = md5(time()); // Create unique boundary from timestamps
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: {$from_name}<{$mail_from}>";
$headers[] = "Reply-To: {$mail_from}";
$headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "--{$uid}";
$headers[] = "Content-type:text/plain; charset=iso-8859-1"; // Set message content type
$headers[] = "Content-Transfer-Encoding: 7bit";
$headers[] = $message; // Dump message
$headers[] = "--{$uid}";
$headers[] = "Content-Type: application/octet-stream; name=\"{$file_name}\""; // Set content type and file name
$headers[] = "Content-Transfer-Encoding: base64"; // Set file encoding base
$headers[] = "Content-Disposition: attachment; filename=\"{$file_name}\""; // Set file Disposition
$headers[] = $file_contents; // Dump file
$headers[] = "--{$uid}--"; //End boundary
// Send mail with header information
if (mail($mail_to, $subject, '', implode("\r\n", $headers) ))
return true;
}
Usages
$from = "mail@w3bees.com";
$name = "W3Bees";
$to = "name@server.com";
$subject = "My mail subject";
$message = "My message";
$file = 'path/to/file';
if(email($from, $name, $to, $subject, $message, $file)){
echo "Success!";
}
else{
echo "Error!";
}
2 comments:
Stainless Steel Condenser Brushes - Titanium Arts
The stainless steel handle titanium alloy allows you to adjust titanium chloride the blade angle ford fusion titanium for sale and titanium keychain speed of a blade. Made by TINORE LTD. Stainless Steel Condenser Brushes is available titanium teeth to $33.99 · In stock
g482d3pntvl838 replica handbags,fake designer bags,replica handbags,replica bags designe,replica handbags,louisvuittonfakebag,fake bags,replicabagsnow,replica handbags y449u5qurqa558
Post a Comment