Blog of King Software

Learn online with us in simple steps.

EMAIL ATTACHMENT IN PHP WITH SIMPLE MAIL FUNCTION


Created by Bhavesh Gohil & Rikita Vasava on Date: 28/11/2022

In this Blog, you will learn how to attach file in email with simple PHP mail() function.
php-email-attachment-form-mail-function


Create a html form to send data in email. You can use below mentioned code to create form.
<form action='send_email.php' method='POST'>
<div class='form-group'>
<label>Send To</label>
<span class='abstric'>*</span>
<input type='text' name='email' id='emailto' class='form-control' placeholder='Enter Email ID'>
<span class='error_msg'></span>
</div>
<div class='form-group'>
<span style='color: red' class='eremail'></span>
</div>
<div class='form-group'>
<label>Subject</label>
<span class='abstric'>*</span>
<input type='text' name='subject' id='subject' class='form-control' placeholder='Enter Email Subject'>
<span class='error_msg'></span>
</div>
<div class='form-group'>
<span style='color: red' class='esubject'></span>
</div>
<div class='form-group'>
<label>Message</label>
<span class='abstric'>*</span>
<input name='message' id='message' class='form-control' placeholder='Enter Email Message' rows='3'>
<span class='error_msg'></span>
</div>
<div class='form-group'>
<span style='color: red' class='emsg'></span>
</div>
<div id='attach_csv_div' style='display:none'>
<div class='form-group'>
<input type='file' name='attachment' id='attachment' class='form-control' >
<span style='color: red' class='eattachment'></span>
</div>
<div class='form-group'>
<input type='submit' name='submit' value='Submit' class='btn btn-primary'>
</div>
</form>

Now our html form is ready.

Send HTML Email with Attachment

The PHP mail() function with some MIME type headers can be used to send email with attachment in PHP. In the following example code, MIME and Content-Type headers are used with mail() function to send email with attachment using PHP.

  • $to – Recipient email address.
  • $from – Sender email address.
  • $fromName – Sender name.
  • $subject – Subject of the email.
  • $file – Relative path of the file that you want to attach with the email.
  • $htmlContent – Body content of the email (Text or HTML).
  • md5(uniqid(time())) – The uniqid() function generates a unique ID based on the microtime (the current time in microseconds). Here generated unique ID will be encryped through MD5 function as well.
  • $mime_boundary – The boundary parameter is a text string used to delineate one part of the message body from another. All boundaries start with two hyphens (--). The final boundary also concludes with two hyphens (--). The boundary can be made up of any ASCII character except for a space, a control character, or special characters.
  • Content-Type: multipart/mixed; – A multipart/mixed MIME message is composed of a mix of different data types.When Exchange Server sends MIME messages, the content-type depends on whether there are attachments to the message, and on the formatting of the message text. If there are attachments, the content-type is multipart/mixed.
  • Content-Type: application/octet-stream; – A MIME attachment with the content type "application/octet-stream" is a binary file. Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. If the attachment has a filename extension associated with it, you may be able to tell what kind of file it is. A .exe extension, for example, indicates it is a Windows or DOS program (executable), while a file ending in .doc is probably meant to be opened in Microsoft Word.
  • Content-Transfer-Encoding: base64 – Base64 encoding is the scheme used to transmit binary data. Base64 processes data as 24-bit groups, mapping this data to four encoded characters. It is sometimes referred to as 3-to-4 encoding.
  • Content-Transfer-Encoding: 7bit – The 7bit is the most fundamental message encoding. Actually, 7bit is not encoded; 7bit encoded files are files that use only 7-bit characters and have lines no longer than 1000 characters. NULL characters are not allowed. 7bit encoded files need no encoding or decoding. This is the default.

The following script lets you send both types of messages (text or HTML) with an attachment file to the email.

File: send_email.php

$message = $_POST['message'];
$to = $_POST['email'];
$subject = $_POST['subject'];
$body = '
<h3>PHP Email with Attachment by Kingsoftware</h3>
<p>This email is sent from the PHP script with attachment.</p>
<p>Message : '.$message.'</p>
<p>This email is sent from the PHP script with attachment.</p>
';
$semi_rand = md5(uniqid(time()));
$headers = "From: abcd@example.com \r\n";
$headers .= "Cc: abcd@example.com \r\n";
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$strFilesName = $_FILES["attachment"]["name"];
$strContent = chunk_split(base64_encode(
file_get_contents($_FILES["attachment"]["tmp_name"])));
$email_message = "--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$body . "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: application/octet-stream;\n" .
" name=\"{$strFilesName}\"\n" .
" //"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$strContent .= "\n\n" .
"--{$mime_boundary}--\n";
$result = mail($to,$subject,$email_message,$headers);