how to send attachment in mail in php?

Today, We want to share with you how to send attachment in mail in php.In this post we will show you PHP mail attachment script, hear for simple file attachment in php mail function we will give you demo and example for implement.In this post, we will learn about php mail with attachment pdf with an example.

Send Email with Attachment in PHP

index.php

<?php
function 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()));
	 $required_data = "From: ".$from_name." <".$from_mail.">\r\n";
	 $required_data .= "Reply-To: ".$replyto."\r\n";
	 $required_data .= "MIME-Version: 1.0\r\n";
	 $required_data .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
	 $required_data .= "This is a multi-part message in MIME format.\r\n";
	 $required_data .= "--".$uid."\r\n";
	 $required_data .= "Content-type:text/plain; charset=iso-8859-1\r\n";
	 $required_data .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
	 $required_data .= $message."\r\n\r\n";
	 $required_data .= "--".$uid."\r\n";
	 $required_data .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
	 $required_data .= "Content-Transfer-Encoding: base64\r\n";
	 $required_data .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
	 $required_data .= $content."\r\n\r\n";
	 $required_data .= "--".$uid."--";
	 if (mail($mailto, $subject, "", $required_data)) {
	 echo "Good Luck mail send ... OK"; 
	 } else {
	 echo "mail send ... ERROR!";
	 }
}

PHP Code
use this PHP function to send an email message with one attached .zip file:

 
$member_file = "pakainfo.zip";
$member_path = "/domain-folder/to_the_attachment/";
$member_name = "Olaf Lederer";
$member_mail = "[email protected]";
$member_replyto = "[email protected]";
$member_subject = "My Website Logo and Website Banner Send You";
$member_message = "PHP Send HTML Email File Attachment Using PHPs mail,php send email with attachment example,write a program in php to send an email along.";
mail_attachment($member_file, $member_path, "[email protected]", $member_mail, $member_name, $member_replyto, $member_subject, $member_message);

I hope you get an idea about simple file attachment in php mail function.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.