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.

Read Also:  Laravel 6 display flash message on view

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.

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 :

Read Also:  Removing file extensions using htaccess


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. Read Also:  PHP Multiple Email Attachments with Sending Email AJAX Multiple File upload
  2. Read Also:  how to save ckeditor text in database php?
  3. Read Also:  how to send attachment in mail in php?
  4. Read Also:  Laravel Search Multiple Columns
  5. Read Also:  sql offset
  6. Read Also:  REST API CRUD Example in PHP with MySQLi
  7. Read Also:  PHP SEND mail with Multiple Attachments
  8. Read Also:  php send mail attachment with Pear Mail
  9. Read Also:  PHP Contact Form Send Email File Attachment Example
  10. Read Also:  Laravel 6 Update Query using Eloquent Example