{"id":301,"date":"2016-03-17T00:52:34","date_gmt":"2016-03-17T00:52:34","guid":{"rendered":"http:\/\/www.howtolearnlinux.com\/?p=301"},"modified":"2016-03-17T00:52:34","modified_gmt":"2016-03-17T00:52:34","slug":"sending-email-with-multiple-attachment-using-php","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=301","title":{"rendered":"Sending email with multiple attachment using PhP"},"content":{"rendered":"<p><strong>The posted below PhP code (function) can be used to send an email with multiple attachment using PhP.<\/strong><\/p>\n<p>========= =========== =========<\/p>\n<p>&lt;?php<\/p>\n<p>$attch1=\/attach\/file1.txt<\/p>\n<p>$attach2=\/attach\/file2.txt<\/p>\n<p>$thefiles[0]=&#8221;$attch1&#8243;;<\/p>\n<p>$thefiles[1]=&#8221;$attch2&#8243;;<\/p>\n<p>$to = &#8220;destination@domain.com&#8221;;<\/p>\n<p>$subject = &#8220;Hello&#8221;;<\/p>\n<p>$message = &#8220;How are you&#8221;;<\/p>\n<p>$senderEmail = &#8220;origin@domain.com&#8221;;<\/p>\n<p>$senderName = &#8220;My Name&#8221;;<\/p>\n<div>function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $thefiles){<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 $from = $senderName.&#8221; &lt;&#8220;.$senderEmail.&#8221;&gt;&#8221;;<\/div>\n<div>\u00a0 \u00a0 $headers = &#8220;From: $from&#8221;;<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 ## boundary<\/div>\n<div>\u00a0 \u00a0 $semi_rand = md5(time());<\/div>\n<div>\u00a0 \u00a0 $mime_boundary = &#8220;==Multipart_Boundary_x{$semi_<wbr \/>rand}x&#8221;;<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 ## headers for attachment<\/div>\n<div>\u00a0 \u00a0 $headers .= &#8220;\\nMIME-Version: 1.0\\n&#8221; . &#8220;Content-Type: multipart\/mixed;\\n&#8221; . &#8221; boundary=\\&#8221;{$mime_boundary}\\&#8221;&#8221;<wbr \/>;<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 ## multipart boundary<\/div>\n<div>\u00a0 \u00a0 $message = &#8220;&#8211;{$mime_boundary}\\n&#8221; . &#8220;Content-Type: text\/html; charset=\\&#8221;UTF-8\\&#8221;\\n&#8221; .<\/div>\n<div>\u00a0 \u00a0 &#8220;Content-Transfer-Encoding: 7bit\\n\\n&#8221; . $message . &#8220;\\n\\n&#8221;;<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 ## preparing attachments<\/div>\n<div>\u00a0 \u00a0 if(count($thefiles) &gt; 0){<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 for($i=0;$i&lt;count($thefiles);$<wbr \/>i++){<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(is_file($thefiles[$i])){<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $message .= &#8220;&#8211;{$mime_boundary}\\n&#8221;;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $fp = \u00a0 \u00a0@fopen($thefiles[$i],&#8221;rb&#8221;);<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $data = \u00a0@fread($fp,filesize($<wbr \/>thefiles[$i]));<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 @fclose($fp);<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $data = chunk_split(base64_encode($<wbr \/>data));<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $message .= &#8220;Content-Type: application\/octet-stream; name=\\&#8221;&#8221;.basename($thefiles[$<wbr \/>i]).&#8221;\\&#8221;\\n&#8221; .<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8220;Content-Description: &#8220;.basename($thefiles[$i]).&#8221;\\n&#8221; .<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8220;Content-Disposition: attachment;\\n&#8221; . &#8221; filename=\\&#8221;&#8221;.basename($<wbr \/>thefiles[$i]).&#8221;\\&#8221;; size=&#8221;.filesize($thefiles[$i])<wbr \/>.&#8221;;\\n&#8221; .<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8220;Content-Transfer-Encoding: base64\\n\\n&#8221; . $data . &#8220;\\n\\n&#8221;;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 }<\/div>\n<div>\u00a0 \u00a0 }<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 $message .= &#8220;&#8211;{$mime_boundary}&#8211;&#8220;;<\/div>\n<div>\u00a0 \u00a0 $returnpath = &#8220;-f&#8221; . $senderEmail;<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 ##send email<\/div>\n<div>\u00a0 \u00a0 $mail = @mail($to, $subject, $message, $headers, $returnpath);<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 ##function return true, if email sent, otherwise return fasle<\/div>\n<div>\u00a0 \u00a0 if($mail){ return TRUE; } else { return FALSE; }<\/div>\n<div><\/div>\n<div>}<\/div>\n<div><\/div>\n<div>?&gt;<\/div>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The posted below PhP code (function) can be used to send an email with multiple attachment using PhP. ========= =========== ========= &lt;?php $attch1=\/attach\/file1.txt $attach2=\/attach\/file2.txt $thefiles[0]=&#8221;$attch1&#8243;; <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=301\" title=\"Sending email with multiple attachment using PhP\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":302,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,16],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-php"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=301"}],"version-history":[{"count":1,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":303,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/301\/revisions\/303"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/302"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}