{"id":162,"date":"2017-04-18T18:05:17","date_gmt":"2017-04-18T18:05:17","guid":{"rendered":"http:\/\/scriptut.com\/?p=162"},"modified":"2024-05-28T00:39:21","modified_gmt":"2024-05-28T00:39:21","slug":"sending-mail-in-php","status":"publish","type":"post","link":"https:\/\/scriptut.com\/php\/sending-mail-in-php\/","title":{"rendered":"PHP Send Email with Attachment"},"content":{"rendered":"\n<p>Email communication is a crucial aspect of modern web applications. Whether it&#8217;s welcoming new users, notifying them of changes, or sending important updates, email serves as a primary channel. PHP, being a versatile server-side scripting language, provides built-in functionalities to handle email operations efficiently. In this article, we will explore how to use PHP to send emails, including emails with attachments, to enhance your application&#8217;s communication capabilities.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The PHP <code>mail()<\/code> Function<\/h2>\n\n\n\n<p>PHP&#8217;s <code>mail()<\/code> function is a straightforward way to send emails directly from your server. This function can handle plain text emails, HTML emails, and even emails with attachments. Here&#8217;s the basic syntax of the <code>mail()<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mail(to, subject, message, headers, parameters)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Sending a Simple Plain Text Email<\/h2>\n\n\n\n<p>Sending a plain text email using PHP is simple. Below is a basic example of how to send a plain text email using the <code>mail()<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$to = 'johndoe@email.com';\n$subject = 'New Notification Received';\n$message = 'Hi Admin, a new notification has been received on the website';\n$from = 'info@scriptut.com';\n\n\/\/ Sending email\nif(mail($to, $subject, $message, \"From:\" . $from)){\n    echo 'Your mail has been sent successfully.';\n} else{\n    echo 'Unable to send email. Please try again.';\n}\n?>\n<\/code><\/pre>\n\n\n\n<p>In this example, we specify the recipient&#8217;s email address, subject, message, and the sender&#8217;s email address.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sending HTML Formatted Emails<\/h2>\n\n\n\n<p>HTML emails are more visually appealing and can include rich text, images, and links. To send an HTML email, we need to set the appropriate headers to inform the email client that the email content is in HTML format. Here\u2019s how you can send an HTML email using PHP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$to = 'johndoe@gmail.com';\n$subject = 'Welcome to Our Website';\n$from = 'info@scriptut.com';\n\n\/\/ To send HTML mail, the Content-type header must be set\n$headers = \"MIME-Version: 1.0\" . \"\\r\\n\";\n$headers .= \"Content-type: text\/html; charset=iso-8859-1\" . \"\\r\\n\";\n\n\/\/ Create email headers\n$headers .= 'From: ' . $from . \"\\r\\n\" .\n            'Reply-To: ' . $from . \"\\r\\n\" .\n            'X-Mailer: PHP\/' . phpversion();\n\n\/\/ Compose a simple HTML email message\n$message = '&lt;html>&lt;body>';\n$message .= '&lt;h1 style=\"color:#f40;\">Hi Jane!&lt;\/h1>';\n$message .= '&lt;p style=\"color:#080;font-size:18px;\">Welcome to our website.&lt;\/p>';\n$message .= '&lt;\/body>&lt;\/html>';\n\n\/\/ Sending email\nif(mail($to, $subject, $message, $headers)){\n    echo 'Your mail has been sent successfully.';\n} else{\n    echo 'Unable to send email. Please try again.';\n}\n?>\n<\/code><\/pre>\n\n\n\n<p>In this example, we set the <code>Content-type<\/code> header to <code>text\/html<\/code> to ensure the email is treated as HTML by the recipient&#8217;s email client.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sending Emails with Attachments<\/h2>\n\n\n\n<p>Sending emails with attachments in PHP involves a bit more complexity. We need to handle MIME types and encode the attachment properly. Below, we will create a function named <code>mail_attachment()<\/code> to facilitate sending emails with attachments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the <code>mail_attachment()<\/code> Function<\/h2>\n\n\n\n<p>The <code>mail_attachment()<\/code> function will help us send emails with attachments. Here is the complete function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nfunction mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {\n    $file = $path . $filename;\n    $file_size = filesize($file);\n    $handle = fopen($file, \"r\");\n    $content = fread($handle, $file_size);\n    fclose($handle);\n    $content = chunk_split(base64_encode($content));\n    $uid = md5(uniqid(time()));\n    \n    $header = \"From: \" . $from_name . \" &lt;\" . $from_mail . \">\\r\\n\";\n    $header .= \"Reply-To: \" . $replyto . \"\\r\\n\";\n    $header .= \"MIME-Version: 1.0\\r\\n\";\n    $header .= \"Content-Type: multipart\/mixed; boundary=\\\"\" . $uid . \"\\\"\\r\\n\\r\\n\";\n    $header .= \"This is a multi-part message in MIME format.\\r\\n\";\n    $header .= \"--\" . $uid . \"\\r\\n\";\n    $header .= \"Content-type:text\/plain; charset=iso-8859-1\\r\\n\";\n    $header .= \"Content-Transfer-Encoding: 7bit\\r\\n\\r\\n\";\n    $header .= $message . \"\\r\\n\\r\\n\";\n    $header .= \"--\" . $uid . \"\\r\\n\";\n    $header .= \"Content-Type: application\/octet-stream; name=\\\"\" . $filename . \"\\\"\\r\\n\";\n    $header .= \"Content-Transfer-Encoding: base64\\r\\n\";\n    $header .= \"Content-Disposition: attachment; filename=\\\"\" . $filename . \"\\\"\\r\\n\\r\\n\";\n    $header .= $content . \"\\r\\n\\r\\n\";\n    $header .= \"--\" . $uid . \"--\";\n    \n    if (mail($mailto, $subject, \"\", $header)) {\n        echo \"Mail sent successfully.\";\n    } else {\n        echo \"Failed to send mail.\";\n    }\n}\n?>\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the <code>mail_attachment()<\/code> Function<\/h2>\n\n\n\n<p>Now, let&#8217;s see how we can use the <code>mail_attachment()<\/code> function to send an email with an attachment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$my_file = \"documents.zip\";\n$my_path = \"\/home\/shared\/\";\n$my_name = \"John Doe\";\n$my_mail = \"johndoe@mailonator.com\";\n$my_replyto = \"info@scriptut.com\";\n$my_subject = \"Please find the attachment\";\n$my_message = \"Hello James, please find the attached document.\";\n\nmail_attachment($my_file, $my_path, \"recipient@mail.org\", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);\n?>\n<\/code><\/pre>\n\n\n\n<p>In this example, we specify the file to be attached (<code>documents.zip<\/code>), its path, and other email details. The <code>mail_attachment()<\/code> function handles the rest, ensuring the file is properly encoded and attached to the email.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Sending emails from a PHP application is a powerful feature that enhances user communication. Whether you need to send plain text emails, HTML emails, or emails with attachments, PHP\u2019s <code>mail()<\/code> function, along with custom handling for attachments, provides a comprehensive solution. By following the examples and guidelines provided in this article, you can implement robust email functionality in your PHP applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enhance your web application&#8217;s communication capabilities by learning how to send emails using PHP. This comprehensive guide explores the use of PHP&#8217;s mail() function to send plain text emails, HTML formatted emails, and emails with attachments. Detailed examples and code snippets are provided to help you implement these features seamlessly.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":3,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/162\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}