{"id":391,"date":"2022-04-03T01:57:01","date_gmt":"2022-04-03T01:57:01","guid":{"rendered":"http:\/\/basicwebsitesolutions.com\/blog\/?p=391"},"modified":"2022-04-03T02:01:51","modified_gmt":"2022-04-03T02:01:51","slug":"contact-us-how-to-send-email-with-attachment-in-php","status":"publish","type":"post","link":"https:\/\/basicwebsitesolutions.com\/blog\/2022\/04\/03\/contact-us-how-to-send-email-with-attachment-in-php\/","title":{"rendered":"Contact Us &#8211; How to send email with attachment in PHP"},"content":{"rendered":"\n<p>I have done a lot of contact us page in websites before and this one, i had to add email attachments as well. The secret to it was to upload it to your website before you can send it as an email attachment. <\/p>\n\n\n\n<p>Recently I found a great website that does that.<\/p>\n\n\n\n<p>Hope it helps somebody out there<\/p>\n\n\n\n<p>Coffee Cup<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php \n \n\/\/ Recipient \n$to = 'recipient@example.com'; \n \n\/\/ Sender \n$from = 'sender@example.com'; \n$fromName = 'CodexWorld'; \n \n\/\/ Email subject \n$subject = 'PHP Email with Attachment by CodexWorld';  \n \n\/\/ Attachment file \n$file = \"files\/codexworld.pdf\"; \n \n\/\/ Email body content \n$htmlContent = ' \n    &lt;h3&gt;PHP Email with Attachment by CodexWorld&lt;\/h3&gt; \n    &lt;p&gt;This email is sent from the PHP script with attachment.&lt;\/p&gt; \n'; \n \n\/\/ Header for sender info \n$headers = \"From: $fromName\".\" &lt;\".$from.\"&gt;\"; \n \n\/\/ Boundary  \n$semi_rand = md5(time());  \n$mime_boundary = \"==Multipart_Boundary_x{$semi_rand}x\";  \n \n\/\/ Headers for attachment  \n$headers .= \"\\nMIME-Version: 1.0\\n\" . \"Content-Type: multipart\/mixed;\\n\" . \" boundary=\\\"{$mime_boundary}\\\"\"; \n \n\/\/ Multipart boundary  \n$message = \"--{$mime_boundary}\\n\" . \"Content-Type: text\/html; charset=\\\"UTF-8\\\"\\n\" . \n\"Content-Transfer-Encoding: 7bit\\n\\n\" . $htmlContent . \"\\n\\n\";  \n \n\/\/ Preparing attachment \nif(!empty($file) &gt; 0){ \n    if(is_file($file)){ \n        $message .= \"--{$mime_boundary}\\n\"; \n        $fp =    @fopen($file,\"rb\"); \n        $data =  @fread($fp,filesize($file)); \n \n        @fclose($fp); \n        $data = chunk_split(base64_encode($data)); \n        $message .= \"Content-Type: application\/octet-stream; name=\\\"\".basename($file).\"\\\"\\n\" .  \n        \"Content-Description: \".basename($file).\"\\n\" . \n        \"Content-Disposition: attachment;\\n\" . \" filename=\\\"\".basename($file).\"\\\"; size=\".filesize($file).\";\\n\" .  \n        \"Content-Transfer-Encoding: base64\\n\\n\" . $data . \"\\n\\n\"; \n    } \n} \n$message .= \"--{$mime_boundary}--\"; \n$returnpath = \"-f\" . $from; \n \n\/\/ Send email \n$mail = @mail($to, $subject, $message, $headers, $returnpath);  \n \n\/\/ Email sending status \necho $mail?\"&lt;h1&gt;Email Sent Successfully!&lt;\/h1&gt;\":\"&lt;h1&gt;Email sending failed.&lt;\/h1&gt;\"; \n \n?&gt;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-codexworld wp-block-embed-codexworld\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"jlgahnfp0E\"><a href=\"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/\">Send Email with Attachment in PHP<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Send Email with Attachment in PHP&#8221; &#8212; CodexWorld\" src=\"https:\/\/www.codexworld.com\/send-email-with-attachment-php\/embed\/#?secret=OOoZrVVaUf#?secret=jlgahnfp0E\" data-secret=\"jlgahnfp0E\" width=\"525\" height=\"296\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to send contact us emails with file attachments<\/p>\n","protected":false},"author":1,"featured_media":394,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[217,215,214,42,216],"class_list":["post-391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-day-to-day-life","category-life-as-a-programmer","tag-attach-files","tag-attachment","tag-contact-us","tag-php","tag-send-email"],"_links":{"self":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":2,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":393,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions\/393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/media\/394"}],"wp:attachment":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}