{"id":2741,"date":"2017-09-04T17:02:22","date_gmt":"2017-09-04T17:02:22","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=2741"},"modified":"2026-04-19T15:49:08","modified_gmt":"2026-04-19T15:49:08","slug":"send-email-with-attachment-php","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/","title":{"rendered":"Send Email with Attachment in PHP"},"content":{"rendered":"<p>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 is developed with PHP or uses PHP, it\u2019s very easy to <a href=\"https:\/\/www.codexworld.com\/send-beautiful-html-email-using-php\/\">send emails from the script using PHP<\/a>.<\/p>\n<p>PHP provides an easy way to send emails from the website. You can send text or HTML email with the <b>mail() function in PHP<\/b>. But sometimes email functionality needs to be extended to send an attachment with the email. Sending emails with attachments in PHP using the native mail() function requires manual handling of MIME types, encoding, and boundaries. In this tutorial, you\u2019ll learn exactly how to <b>send email with attachment in PHP<\/b> using a working example.<\/p>\n<p>In this tutorial, we will show you how to send an email with an attachment using the PHP mail() function. We will also cover how to send HTML emails with attachments (any file type, such as images, documents, and PDFs) and how to send emails to multiple recipients. <\/p>\n<h2>\ud83d\ude80 Prerequisites<\/h2>\n<p>Before you can use the code in this tutorial, make sure you have the following:<\/p>\n<ul>\n<li>A web server with PHP installed.<\/li>\n<li>mail() function is enabled on your server<\/li>\n<li>You have a file ready to attach (PDF, image, etc.)<\/li>\n<\/ul>\n<h2>How to Send Email with Attachment in PHP<\/h2>\n<p>To send an email with an attachment in PHP, you need to set the appropriate headers and encode the attachment file in a format that can be sent via email. The following example demonstrates how to send an email with a PDF attachment using the mail() function in PHP.<br \/>\nHere are the key steps involved in the process:<\/p>\n<ul>\n<li>Define Email Configuration (these variables control who receives the email and who sends it): The <code>$to<\/code> variable is the recipient&#8217;s email address, <code>$formName<\/code> is the name that will appear in the &#8220;From&#8221; field of the email, and <code>$formEmail<\/code> is the email address that will appear in the &#8220;From&#8221; field.<\/li>\n<li>Create HTML Email Body: You can send a rich HTML email instead of plain text. Using HTML improves readability and user experience. The <code>$messageHtml<\/code> variable contains the HTML content of the email. You can customize this HTML to include your desired message, formatting, and styling.<\/li>\n<li>Attach a File and Encode the Attachment: The <code>$attachmentPath<\/code> variable specifies the path to the file you want to attach. The script reads the file content, encodes it in base64 format, and prepares it for sending as an email attachment.<\/li>\n<li>Create a Unique Boundary: Boundary separates different parts of the email. The $boundary variable is generated using the md5 hash of the current time to ensure uniqueness. This boundary is used in the email headers and body to indicate where the HTML content ends and the attachment begins.<\/li>\n<li>Set Email Headers: The script sets the necessary email headers, including &#8220;From&#8221;, &#8220;Reply-To&#8221;, &#8220;MIME-Version&#8221;, and &#8220;Content-Type&#8221;. The &#8220;Content-Type&#8221; header specifies that the email is multipart\/mixed and includes the boundary string.<\/li>\n<li>Construct the Email Body: The email body is constructed in a multipart format. It includes the HTML content followed by the attachment. Each part is separated by the boundary string defined earlier.<\/li>\n<li>Send the Email: Finally, the mail() function is called with the recipient&#8217;s email address, subject, email body, and headers. The script checks if the email was sent successfully and outputs an appropriate message.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Configuration&nbsp;-&nbsp;change&nbsp;these&nbsp;values&nbsp;as&nbsp;needed <br \/><\/span><span style=\"color: #0000BB\">$to&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'recipient@example.com'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$formName&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'Demo&nbsp;Site'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$formEmail&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'sender@example.com'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Subject&nbsp;of&nbsp;the&nbsp;email <br \/><\/span><span style=\"color: #0000BB\">$subject&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'Test&nbsp;email&nbsp;with&nbsp;attachment&nbsp;(PHP&nbsp;mail())'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;HTML&nbsp;message&nbsp;body <br \/><\/span><span style=\"color: #0000BB\">$messageHtml&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;&lt;&lt;&lt;HTML <br \/><\/span><span style=\"color: #DD0000\">&lt;html&gt; <br \/>&lt;head&gt; <br \/>&nbsp;&nbsp;&lt;title&gt;Test&nbsp;Email&lt;\/title&gt; <br \/>&lt;\/head&gt; <br \/>&lt;body&gt; <br \/>&nbsp;&nbsp;&lt;h1&gt;PHP&nbsp;mail()&nbsp;with&nbsp;attachment&lt;\/h1&gt; <br \/>&nbsp;&nbsp;&lt;p&gt;This&nbsp;is&nbsp;an&nbsp;&lt;strong&gt;HTML&lt;\/strong&gt;&nbsp;email&nbsp;body.&lt;\/p&gt; <br \/>&nbsp;&nbsp;&lt;p&gt;Regards,&lt;br&gt;PHP&nbsp;Script&lt;\/p&gt; <br \/>&lt;\/body&gt; <br \/>&lt;\/html&gt; <br \/><\/span><span style=\"color: #007700\">HTML; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Attachment&nbsp;file&nbsp;path&nbsp;(local) <br \/><\/span><span style=\"color: #0000BB\">$attachmentPath&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">__DIR__&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/sample-attachment.pdf'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #0000BB\">$fileSize&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">filesize<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$attachmentPath<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$handle&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">fopen<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$attachmentPath<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'rb'<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$content&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">fread<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$handle<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$fileSize<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">fclose<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$handle<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$encodedContent&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">chunk_split<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">base64_encode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$content<\/span><span style=\"color: #007700\">)); <br \/> <br \/><\/span><span style=\"color: #0000BB\">$boundary&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">md5<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">time<\/span><span style=\"color: #007700\">()); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Headers <br \/><\/span><span style=\"color: #0000BB\">$headers&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[]; <br \/><\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'From:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$formName&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'&nbsp;&lt;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$formEmail&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'&gt;'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'Reply-To:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$formEmail<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'MIME-Version:&nbsp;1.0'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-Type:&nbsp;multipart\/mixed;&nbsp;boundary=\\\"<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$boundary<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\\\"\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Multipart&nbsp;body&nbsp;(HTML&nbsp;only) <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"--<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$boundary<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-Type:&nbsp;text\/html;&nbsp;charset=UTF-8\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-Transfer-Encoding:&nbsp;7bit\\r\\n\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #0000BB\">$messageHtml&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\r\\n\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Attachment&nbsp;part <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"--<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$boundary<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-Type:&nbsp;application\/octet-stream;&nbsp;name=\\\"\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">basename<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$attachmentPath<\/span><span style=\"color: #007700\">).<\/span><span style=\"color: #DD0000\">\"\\\"\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-Transfer-Encoding:&nbsp;base64\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-Disposition:&nbsp;attachment;&nbsp;filename=\\\"\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">basename<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$attachmentPath<\/span><span style=\"color: #007700\">).<\/span><span style=\"color: #DD0000\">\"\\\"\\r\\n\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #0000BB\">$encodedContent&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;End&nbsp;of&nbsp;mixed&nbsp;boundary <br \/><\/span><span style=\"color: #0000BB\">$body&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"--<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$boundary<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">--\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Send&nbsp;mail <br \/><\/span><span style=\"color: #0000BB\">$sent&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">mail<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$to<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$subject<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$body<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">implode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"\\r\\n\"<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">)); <br \/> <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$sent<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Email&nbsp;successfully&nbsp;sent&nbsp;to&nbsp;<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$to<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\\n\"<\/span><span style=\"color: #007700\">; <br \/>}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Failed&nbsp;to&nbsp;send&nbsp;email&nbsp;to&nbsp;<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$to<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\\n\"<\/span><span style=\"color: #007700\">; <br \/>} <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<p><u><b>Send Email to Multiple Recipients:<\/b><\/u><br \/>\nYou can send emails to multiple recipients by separating their email addresses with commas in the <code>$to<\/code> variable. For example:<\/p>\n<pre><span style=\"color: #0000BB\">$to&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'user@example.com,&nbsp;anotheruser@example.com'<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p>The Cc and Bcc fields can also be added to the email headers to send copies of the email to additional recipients without revealing their email addresses to others.<\/p>\n<pre><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'Cc:&nbsp;cc@example.com'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'Bcc:&nbsp;bcc@example.com'<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p><u><b>Send Email with Multiple Attachments:<\/b><\/u><br \/>\nThe above example demonstrates how to send an email with a single attachment. If you want to send an email with multiple attachments, you can repeat the attachment part in the email body for each file you want to attach. Each attachment should be separated by the same boundary string.<\/p>\n<p>Here is a separate tutorial that explains how to send an email with multiple attachments in PHP:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.codexworld.com\/send-email-with-multiple-attachments-php\/\">Send Email with Multiple Attachments in PHP<\/a><\/li>\n<\/ul>\n<h2>\u26a0\ufe0f Important Notes (Very Important for Production)<\/h2>\n<ul>\n<li><code>mail()<\/code> is not recommended for production use, especially with attachments, due to reliability and security issues.<\/li>\n<li>For production, consider using SMTP libraries like PHPMailer or SwiftMailer, which provide better handling of email sending, including attachments, and support for authentication and encryption.<\/li>\n<\/ul>\n<p>Here is a tutorial that explains how to send email via SMTP server in PHP using PHPMailer:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.codexworld.com\/send-html-email-php-gmail-smtp-phpmailer\/\">Send Email via SMTP Server in PHP<\/a><\/li>\n<\/ul>\n<h2>\ud83d\udee0\ufe0f Common Issues &#038; Fixes<\/h2>\n<p><b>1. Email Not Sending on Localhost:<\/b><\/p>\n<ul>\n<li>Configure SMTP in php.ini<\/li>\n<li>Use tools like MailHog or sendmail<\/li>\n<\/ul>\n<p><b>2. Attachment Corrupted:<\/b><\/p>\n<ul>\n<li>Ensure proper Base64 encoding<\/li>\n<li>Do not miss chunk_split()<\/li>\n<\/ul>\n<p><b>3. Email Goes to Spam:<\/b><\/p>\n<ul>\n<li>Use valid &#8220;From&#8221; email<\/li>\n<li>Add SPF\/DKIM records<\/li>\n<li>Avoid spammy content<\/li>\n<\/ul>\n<h2>\ud83c\udfaf Final Thoughts<\/h2>\n<p>Sending emails with attachments in PHP is a common requirement for many web applications. Here we have provided the easiest way to <b>send email with attachment in PHP<\/b>. You don\u2019t 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. This method gives you full control over email structure, but also requires careful handling of MIME formatting.<br \/>\nWhile the default <code>mail()<\/code> function can handle basic email sending, it has limitations when it comes to attachments and reliability. For production environments, it&#8217;s recommended to use dedicated libraries like PHPMailer or SwiftMailer to ensure better performance and security. If you\u2019re building a real-world application, switching to a mailing library will save you time and prevent delivery issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 is developed <\/p>\n","protected":false},"author":1,"featured_media":6068,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[165,23,24,14],"class_list":["post-2741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-attachment","tag-email","tag-html-email","tag-php","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Send Email with Attachment in PHP - CodexWorld<\/title>\n<meta name=\"description\" content=\"PHP send email with attachment - Learn how to send HTML email with attachment in PHP. Simple script to send email with pdf\/image attachment using the PHP mail() function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Send Email with Attachment in PHP - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"PHP send email with attachment - Learn how to send HTML email with attachment in PHP. Simple script to send email with pdf\/image attachment using the PHP mail() function.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2017-09-04T17:02:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-19T15:49:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1536\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Send Email with Attachment in PHP\",\"datePublished\":\"2017-09-04T17:02:22+00:00\",\"dateModified\":\"2026-04-19T15:49:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/\"},\"wordCount\":946,\"commentCount\":20,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/09\\\/send-email-with-attachment-in-php-codexworld.jpg\",\"keywords\":[\"Attachment\",\"Email\",\"HTML Email\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/\",\"name\":\"Send Email with Attachment in PHP - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/09\\\/send-email-with-attachment-in-php-codexworld.jpg\",\"datePublished\":\"2017-09-04T17:02:22+00:00\",\"dateModified\":\"2026-04-19T15:49:08+00:00\",\"description\":\"PHP send email with attachment - Learn how to send HTML email with attachment in PHP. Simple script to send email with pdf\\\/image attachment using the PHP mail() function.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/09\\\/send-email-with-attachment-in-php-codexworld.jpg\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2017\\\/09\\\/send-email-with-attachment-in-php-codexworld.jpg\",\"width\":1536,\"height\":1024,\"caption\":\"send-email-with-attachment-in-php-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/send-email-with-attachment-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Send Email with Attachment in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Send Email with Attachment in PHP - CodexWorld","description":"PHP send email with attachment - Learn how to send HTML email with attachment in PHP. Simple script to send email with pdf\/image attachment using the PHP mail() function.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/","og_locale":"en_US","og_type":"article","og_title":"Send Email with Attachment in PHP - CodexWorld","og_description":"PHP send email with attachment - Learn how to send HTML email with attachment in PHP. Simple script to send email with pdf\/image attachment using the PHP mail() function.","og_url":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2017-09-04T17:02:22+00:00","article_modified_time":"2026-04-19T15:49:08+00:00","og_image":[{"width":1536,"height":1024,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg","type":"image\/jpeg"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Send Email with Attachment in PHP","datePublished":"2017-09-04T17:02:22+00:00","dateModified":"2026-04-19T15:49:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/"},"wordCount":946,"commentCount":20,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg","keywords":["Attachment","Email","HTML Email","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/","url":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/","name":"Send Email with Attachment in PHP - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg","datePublished":"2017-09-04T17:02:22+00:00","dateModified":"2026-04-19T15:49:08+00:00","description":"PHP send email with attachment - Learn how to send HTML email with attachment in PHP. Simple script to send email with pdf\/image attachment using the PHP mail() function.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/send-email-with-attachment-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg","width":1536,"height":1024,"caption":"send-email-with-attachment-in-php-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Send Email with Attachment in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2017\/09\/send-email-with-attachment-in-php-codexworld.jpg","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-Id","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/2741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=2741"}],"version-history":[{"count":11,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/2741\/revisions"}],"predecessor-version":[{"id":6067,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/2741\/revisions\/6067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/6068"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=2741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=2741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=2741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}