Send Email With File Attachment In PHP

Hi Developers, Thanks for reading send an email with file attachment In PHP. Email is a method of exchanging message between people by electronic devices. We can say It’s a very Common and Important functionality for Web and mobile application. Nowadays Most of the E-commerce website having functionality like Contact us, Feedback, Various Notification like order received, Order Shipped, Amount Get paid, Invoice etc..

The more important functionality of send email is implementing at time of registration a website or an application. We need to verify the registration email ID to get rid of spam or fake users from your websites or applications.

Send Email with file in php

Here this blog section you will get the best example of the send email functionality with sample code and video tutorials for easy understanding. you will get an idea of how to send an email with attachment in the PHP script.

In General PHP gives us in-built send email functionality which is mail();

$to= This parameter is for a receiver. [required]
$subject = This parameter is for Subject of Email.[required]
$message =This is for Message or email content.[required]
$headers =This contains additional headers, like From, Cc, Mime-Version, Bcc. [optional]
$parameters = use to pass additional parameter.[optional]

Different Way to send email using PHP find below.

  • Simple Email using PHP script.
  • Send Email with HTML format.
  • Send Email With attachment with a PHP script.
  • Sending Email with dynamic Attachment when submitting the form using PHP script.
  • Email With multiple Attachment in Core PHP script.
  • Email With Dynamic Attachment when Submitting the form using Core PHP.
  • Email Send Using SMTP in Core PHP.
  • Email Send Using PHP mailer library in PHP.
  • Email Send Using Codeigniter Framework.
  • Email with Attached email Send Using Codeigniter framework.
  • Email With Attached mail Sends Using Laravel Framework.
  • Email With Attachment by Python.

Simple Email send using PHP


Send Email with HTML format in PHP

When we send simple email in PHP,  we consider all the text is simple text, means it not contain any kind of HTML.
Here you have to send HTML formatted text. For that, we have to add some in-built features which can help to get HTML formatted email in PHP. you need to add some line of text in $headers parameter.

MIME – It stands for Multipurpose Internet Mail Extension.
To format any HTML tag, we can specify the MIME version, content type and character set of the header section.


Sensitization and validation is good practice before sending email in PHP.

Nowadays hackers are more active to make spam your website or inbox. To get rid of this kind of spam you need to sanitize or validate user’s email from input data. let see how to sanitize or filter these unknown emails or data inject from user-end from web forms.
PHP used a function called filter_var($email, SANITIZATION TYPE);

FILTER_VALIDATE_EMAIL – It returns true for valid email addresses and false for invalid email addresses.

FILTER_SANITIZE_EMAIL – It removed illegal characters from email addresses.

$email = support\

$output = filter_var($email, FILTER_SANITIZE_EMAIL);

//output =;

FILTER_SANITIZE_URL – It removes illegal characters from URLs.

$email  = “http://www.example@.cém”;

$output = filter_var($email, FILTER_SANITIZE_URL );

//$output =

FILTER_SANITIZE_STRING – It removes tags from string values.

$email  = “<b>Some text</b>”;

$output = filter_var($email, FILTER_SANITIZE_STRING );

//$output =Some text;

Send Email with attachment in PHP

To send an attachment with the email, we need to set the Content-type as mixed/multipart and we have to specify the text and attachment sections within a Boundary.

Download source code & Demo