How to send an Email with attachment using PHP

These days, almost every application has the ability to send an email. Apparently, the functionality looks difficult but in reality, sending an email using PHP is really easy. The syntax of the mail function is as follows:

mail( string $to, string $subject, string $message [,mixed $additional_headers [,$additional_parameters]]): bool

String $to will contain the valid email address you want to send the email to.
String $subject will have the subject line of the email.
String $message will contain the real message of the email.

Each line of the message should be separated with a CRLF and lines should not be more than 70 characters. The term CRLF refers to carriage return (ASCII 13, \r) and line feed(ASCII 10, \n). In windows, both are required to describe the end of a line, whereas in Linux/Unix only LF is required.

Mixed $additional_headers is an optional parameter. It is used to add extra headers (From, Cc, Bcc). Multiple extra headers should be separated with a CRLF. It could be an array or string.

The main() function opens and closes an SMTP socket for each email. The mail() function doesn’t support sending attachments by default. You need to use different headers and MIME mail parts to make this possible.

So, let’s learn how to send an email with an attachment file using PHP mail() function.

Create a file mail.php and copy paste the following code.

Execute this code in your browser.

    $mailto = 'send_to_Email';
    $subject = 'Subject';
    $message = 'Hi! Please check – Your Source of Programming Tutorials';

   // get the file’s content you are trying to send as an attachment
    $filename = 'file.txt';
    $content = file_get_contents($filename);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $rand_hash= md5(time());

    // carriage return type (RFC)
    $crlf = "\r\n";

     // main header
      $headers = "From: name <[email protected]>" . $crlf;
      $headers .= "MIME-Version: 1.0" . $crlf;
      $headers .= "Content-Type: multipart/mixed; boundary=" . $rand_hash . $crlf;
      $headers .= "Content-Transfer-Encoding: 7bit" . $crlf;
      $headers .= "This is a multi-part message in MIME format." . $crlf;

      // message
      $message.= "--" . $rand_hash. $crlf;
      $message.= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $crlf;
      $message.= "Content-Transfer-Encoding: 8bit" . $crlf;
      $message.=  $message . $crlf;

      // attach the file
      $message.= "--" . $rand_hash. $crlf;
      $message.= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $crlf;
      $message.= "Content-Transfer-Encoding: base64" . $crlf;
      $message.= "Content-Disposition: attachment" . $crlf;
      $message.= $content . $crlf;
      $message.= "--" . $rand_hash. "--";

      //send email using mail() function
      if (mail($mailto, $subject, $body, $headers)) {
          echo "Success! Mail has been sent successfully!";
      } else echo "Mail couldn't sent";

    $msg=  mail($mailto, $subject, $message);

Related Articles

Upload and Download Files Using PHP

Export data from MySQL to excel using php

Export data from MySQL to JSON using PHP

Leave a Reply