PHP Send HTML Email File Attachment Using PHPs mail

PHP Send HTML Email File Attachment Using PHPs mail

In this Post We Will Explain About is PHP Send HTML Email File Attachment Using PHPs mail With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to create PHP based email form with file attachmentExample

In this post we will show you Best way to implement How to Send a Confirmation Email with a File Attachment, hear for Php Send Email with Attachment – Sending Mailwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

PHP – send email with attachment file

PHP simple send mail with attachment script. This simple or custom function or source code PHP mail attachment script is simple able to send a text or plain text email send message as well as with a single data path attachment file.Below is an simple example on how we use this function to as send an simple email message with one some attached live24u.pdf file:Include the simple main script file — require_once(‘path/to/file/class.phpmailer.php’); as well as Download the PHPMailer libs or script source code from here: http://github.com/PHPMailer/PHPMailer

Source code : PHP mail attachment script

$my_file = "live24u.pdf"; // Pakainfo.com attachment file name
$path = "Your folder name"; //Pakainfo.com path of attachment file
$user_name = "Your Usrename"; //Pakainfo.com user name
$mail_to = "[email protected]"; //Pakainfo.com to eamil
$mail_sub = "Notification of attachment for user"; //Pakainfo.com subject of eamil
$body_text = "Hello ".$user_name.",\nThis demo message with file attachment. You will be notified when their review has been completed.\n Regard \n live24u Team";
 
$attachment_file = $path . "/" . $my_file; // path of file
$attachment_file_size = filesize($attachment_file);
$email_handle = fopen($attachment_file, "r");
$content = fread($email_handle, $attachment_file_size);
fclose($email_handle);// file close of email handle
$content = chunk_split(base64_encode($content));
$myLivetime = md5(time());// a random hash will be necessary to send mixed content
 
// carriage return type (we use a PHP end of line constant)
$mail_seol = PHP_EOL;
$body_message = $body_text;
// main header (multipart mandatory)
$mail_header = "From: Pakainfo.com Team <[email protected]>" . $mail_seol;
//Pakainfo.com sender eamil detail
$mail_header .= "MIME-Version: 1.0" . $mail_seol;//MIME-Version
$mail_header .= "Content-Type: multipart/mixed; boundary=\"" . $myLivetime . "\"" . $mail_seol;//Content-Type
$mail_header .= "Content-Transfer-Encoding: 7bit" . $mail_seol;//Content-Transfer-Encoding
$mail_header .= "This is a MIME encoded message." . $mail_seol;
 
//Pakainfo.com eamil message
$mail_header .= "--" . $myLivetime . $mail_seol;
$mail_header .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $mail_seol;
$mail_header .= "Content-Transfer-Encoding: 8bit" . $mail_seol;
$mail_header .= $body_message . $mail_seol;
 
//Pakainfo.com attachment
$mail_header .= "--" . $myLivetime . $mail_seol;
$mail_header .= "Content-Type: application/octet-stream; name=\"" . $my_file . "\"" . $mail_seol;
$mail_header .= "Content-Transfer-Encoding: base64" . $mail_seol;
$mail_header .= "Content-Disposition: attachment" . $mail_seol;
$mail_header .= $content . $mail_seol;
$mail_header .= "--" . $myLivetime . "--";
mail($mail_to, $mail_sub, ", $mail_header); //SEND Mail TO USER

About PHP’s native mail() function

The attachment simple file has to be PHP uploaded first or we can use a simple file which already here exists on your any linux file or folder web server

how to send attachment in mail in php?

To send an email with an attachment in PHP, you can use the built-in mail function along with some additional headers and boundary delimiters to specify the attachment. Here is an example PHP script that sends an email with an attachment:

$to = "[email protected]";
$subject = "Test Email with Attachment";
$message = "This is a test email with an attachment.";
$attachment = "/path/to/file.pdf";
$file_name = "attachment.pdf";
$file_type = "application/pdf";

// Generate a boundary delimiter for the email
$boundary = md5(uniqid());

// Set up the email headers
$headers = "From: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

// Set up the email body
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message."\r\n";

// Set up the attachment
$file = fopen($attachment, "rb");
$data = fread($file, filesize($attachment));
fclose($file);
$data = chunk_split(base64_encode($data));
$body .= "--$boundary\r\n";
$body .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
$body .= $data."\r\n";
$body .= "--$boundary--";

// Send the email
mail($to, $subject, $body, $headers);

In the above example, replace the $to, $subject, $message, $attachment, $file_name, and $file_type variables with your own values. The script will generate a boundary delimiter for the email, set up the email headers and body, and attach the specified file to the email.

Note that the mail function may not work on all systems or configurations. You may need to use a third-party library or service to send emails with attachments in PHP.

Example

I hope you have Got What is Sending HTML-email with Attachment Using PHP’s mail() Function And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment