php send email with attachment example

Today, We want to share with you php send email with attachment example.In this post we will show you Send an email attachment with PHP, hear for program to send email with attachment in php we will give you demo and example for implement.In this post, we will learn about write a program in php to send an email along with attachment with an example.

php send email with attachment example

There are the Following The simple About php send email with attachment example Full Information With Example and source code.

As I will cover this Post with live Working example to develop smtp mail with attachment in php, so the php send email with image attachment for this example is following below.

  AngularJS Arrays - tips, tricks and examples

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Download Project Scripts

Here’s the PHP Send an Email attachment with PHP complete code listing.


//PHP Send Email Settings
$name        = "Krunal Sisodiya";
$email       = "[email protected]";
$to          = "$name <$email>";
$from        = "Jaydeep-Sojitra ";
$subject     = "For An PHP JOB Offer Latters";
$mainMessage = "Hi dear, Below attachemnets your Offer Latters.";
$attach_files     = "./offer-latter.pdf";
$attach_filestype = "application/pdf";
$attach_filesname = "2019-offer-latter.pdf";
$headers = "From: $from";

//simple PHP File handalings
$file = fopen($attach_files, 'rb');
$data = fread($file, filesize($attach_files));

// This is a PHP attaches the file
$semi_rand     = md5(time());
$data_send_mime_types = "==Multipart_Boundary_x{$semi_rand}x";
$headers      .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$data_send_mime_types}\"";
$message = "Welcome to you This is a multi-part message in MIME format.\n\n" .
"-{$data_send_mime_types}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage  . "\n\n";

$data = chunk_split(base64_encode($data));
$message .= "--{$data_send_mime_types}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .

// PHP Send the email
if(mail($to, $subject, $message, $headers)) {

    echo "Good Luck, Your email was sent.";

else {

    echo "There was some an error for PHP sending the mail.";


Angular 6 CRUD Operations Application Tutorials

Read :

  PHP Send HTML Email File Attachment Using PHPs mail


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about php send email with attachment example.
I would like to have feedback on my 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.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Removing # hash from URL in AngularJS
  2.   PHP Multiple Email Attachments with Sending Email AJAX Multiple File upload
  3.   WordPress AJAX URL GET POST Call PHP Function
  4.   PHP SEND mail with Multiple Attachments
  5.   Create Authentication Custom Middleware in Laravel 5.7
  6.   Http JSONP Service using Angular Example
  7.   PHP Mail Attachment - Php Send Email File-Images Attachments
  8.   PHP Webcam Capture Image Save from Camera using jQuery
  9.   PHP Contact Form Send Email File Attachment Example
  10.   Stripe Refund Api Tutorial Example From Scratch

Leave a Reply

Your email address will not be published. Required fields are marked *