{"id":290,"date":"2011-04-12T09:07:58","date_gmt":"2011-04-12T09:07:58","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=290"},"modified":"2011-04-12T09:07:58","modified_gmt":"2011-04-12T09:07:58","slug":"send-email-with-attachments-using-php","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/php\/send-email-with-attachments-using-php\/","title":{"rendered":"Send email with attachments using php"},"content":{"rendered":"<p>Most people know about php&#8217;s <a title=\"php mail function\" href=\"http:\/\/www.php.net\/manual\/en\/function.mail.php\">mail function<\/a>. Even if it&#8217;s not the only way to send emails it is very used because of its simplicity. One thing that is not very often found in tutorials but is very useful might be the way to add attachments to the message using only this function and not a heavyweight classes.<\/p>\n<p>This is in fact very easy. All you need to do is to provide the full message body (not only the text\/html) and that&#8217;s where you will add the attachment in a block like this one:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n--PHP-mixed-$random_hash\r\nContent-Type: application\/zip; name=&quot;$filename&quot;\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment\r\n\r\n$attachment\r\n\r\n<\/pre>\n<p>Where $filename is the name you want to show to the email receiver and $attachment is the encoded file that you get like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$attachment = chunk_split(base64_encode(file_get_contents($value)));\r\n<\/pre>\n<p>That&#8217;s all you need to do. You can download <a title=\"PHP - send email with attachments\" href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2011\/04\/email.zip\">here<\/a> a full working example, that supports multiple attachments (the archive also contains two test files for this) and all you need to do to use it is to change the sender email address and the one for the receiver.<\/p>\n<p>Make sure that as sender you are using and email address that exists on your server.<\/p>\n<p>That&#8217;s all. Feel free to post your comments\/questions using the form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Send email with attachments using php\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/php\/send-email-with-attachments-using-php\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Most people know about php&#8217;s mail function. Even if it&#8217;s not the only way to send emails it is very used because of its simplicity. One thing that is not very often found in tutorials but is very useful might be the way to add attachments to the message using only this function and not &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/send-email-with-attachments-using-php\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/send-email-with-attachments-using-php\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[76,75,74,35],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/290"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=290"}],"version-history":[{"count":2,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":293,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/290\/revisions\/293"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}