Send Email with Attachment in PHP

Sending emails from the script is a very useful functionality in the web application. Most of the websites used the email sending feature to send notifications to the user. If your web application developed with PHP or uses PHP, it’s very easy to send email from the script using PHP.

PHP provides an easy way to send emails from the website. You can send text or HTML email with mail() function in PHP. But sometimes email functionality needs to be extended for sending an attachment with the mail. In this tutorial, we will show you how to send email with attachment in PHP.

In the example script, we will make it simple to send text or HTML email including any types of files as an attachment (like image, .doc, .docx, .pdf, .txt, etc.) using PHP.

Send HTML Email with Attachment

The PHP mail() function with some MIME type headers can be used to send email with attachment in PHP. In the following example code, MIME and Content-Type headers are used with mail() function to send email with attachment using PHP.

  • $to – Recipient email address.
  • $from – Sender email address.
  • $fromName – Sender name.
  • $subject – Subject of the email.
  • $file – Relative path of the file that you want to attach with the email.
  • $htmlContent – Body content of the email (Text or HTML).

The following script lets you send both types of messages (text or HTML) with an attachment file to the email.

<?php 

// Recipient
$to 'recipient@example.com';

// Sender
$from 'sender@example.com';
$fromName 'CodexWorld';

// Email subject
$subject 'PHP Email with Attachment by CodexWorld'

// Attachment file
$file "files/codexworld.pdf";

// Email body content
$htmlContent '
    <h3>PHP Email with Attachment by CodexWorld</h3>
    <p>This email is sent from the PHP script with attachment.</p>
'
;

// Header for sender info
$headers "From: $fromName"." <".$from.">";

// Boundary 
$semi_rand md5(time()); 
$mime_boundary "==Multipart_Boundary_x{$semi_rand}x"

// Headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" "Content-Type: multipart/mixed;\n" " boundary=\"{$mime_boundary}\"";

// Multipart boundary 
$message "--{$mime_boundary}\n" "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" $htmlContent "\n\n"

// Preparing attachment
if(!empty($file) > 0){
    if(
is_file($file)){
        
$message .= "--{$mime_boundary}\n";
        
$fp =    @fopen($file,"rb");
        
$data =  @fread($fp,filesize($file));

        @
fclose($fp);
        
$data chunk_split(base64_encode($data));
        
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" 
        
"Content-Description: ".basename($file)."\n" .
        
"Content-Disposition: attachment;\n" " filename=\"".basename($file)."\"; size=".filesize($file).";\n" 
        
"Content-Transfer-Encoding: base64\n\n" $data "\n\n";
    }
}
$message .= "--{$mime_boundary}--";
$returnpath "-f" $from;

// Send email
$mail = @mail($to$subject$message$headers$returnpath); 

// Email sending status
echo $mail?"<h1>Email Sent Successfully!</h1>":"<h1>Email sending failed.</h1>";

?>

The example script allows you to send a single attachment to the email, to send an email with multiple attachments follow this tutorial – Send Email with Multiple Attachments in PHP

Sending Email to Multiple Recipients:
You can send email to multiple recipients at once with Cc and Bcc headers. Use the Cc and Bcc headers for sending email with attachment to multiple recipients in PHP.

$headers .= "\nCc: mail@example.com"; 
$headers .= "\nBcc: mail@example.com";

Send Email via SMTP Server in PHP

Conclusion

Here we have provided the easiest way to send email with attachment in PHP. You don’t need to include any library to send HTML email with attachments. Using the PHP default mail() function you can easily send email with pdf/image attachment.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

20 Comments

  1. Ottavino Gnata Said...
  2. Alcides Said...
  3. Ed Said...
  4. Ali Said...
  5. Val Said...
  6. Mehul Said...
  7. Usman Mustafa Khawar Said...
  8. Emanuele Said...
  9. Hans Rondon Said...
  10. Ivan Said...
  11. Umesh Computer Said...
  12. Christoph Said...
  13. Sukhdeep Kaur Said...
  14. Jithin S Said...
  15. Gyula Said...
  16. Benjamin Said...
    • CodexWorld Said...
  17. Sachin Said...
  18. Jason Morita Said...

Leave a reply

keyboard_double_arrow_up