Email With Attachment

Generally we use to send text/html mails in PHP by mail() function, this function is not providing to sent attachment file by default, so here i write a function using mail() function to send the email with attachment.

Below is that I wrote and modified to send email with attachment and how to use this function.

[php]

<?php
function qt_mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$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"; // use mail format here
$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=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "–".$uid."–";
if (mail($mailto, $subject, "", $header)) {
echo "mail send … OK";
} else {
echo "mail send … ERROR!";
}
}

// function uses below

$qt_recipient = ""; // recipient mail address goes here
$qt_file = "somefile.zip"; // name of the file goes here
$qt_path = $_SERVER[‘DOCUMENT_ROOT’]."/your_path_here/"; // path where the file is goes here
$qt_name = "Quality Tuts"; // sender name goes here
$qt_mail = "my@domain.com"; // sender email goes here
$qt_replyto = "qt_reply_to@domain.com"; // reply to email goes here
$qt_subject = "This is a mail with attachment."; // mail subject goes here
$qt_message = "Hello, Your message goes here, QT"; // mail message goes here
qt_mail_attachment($qt_file, $qt_path, $qt_recipient, $qt_mail, $qt_name, $qt_replyto, $qt_subject, $qt_message); // call to function

?>

[/php]

I'm a web developer / PHP programmer. I am also working on custom plugin development of wordpress.