How to send an email with an attachment in php

At first, you should check if your php.ini file is configured correctly. It depends on your operating system. So locate and open your php.ini file. E.g. /etc/php5/apache2/php.ini and find the [mail function] section.

This file should looks like this. Be sure you have the right configuration depending on your system. ;)

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from =

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on NT, not valid in Windows 95).
;mail.log = syslog

In PHP is used bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) function to send emails. The meaning of the parameters is:

  • to - Receiver, or receivers of the mail. Examples:
    • User <>
    • User <>, Another User <>
  • subject - Subject of the email to be sent.
  • additional_headers (optional) - String to be inserted at the end of the email header. It's typically used to set From, CC, BCC.
  • additional_parameters (optional) - It can be used to pass additional flags as command line options to the sendmail program.

The mail() function returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

Adding an attachment

If you want to send an email with an attachment is required to set Content-type header to multipart/mixed. Then you can specify the body and the attachment sections within the boundaries.

A boundary starts with two hyphens followed by a unique number. It can't appear in the body part of the email. For generating this number you can use e.g. md5() or sha1() function. A final boundary of the final section must also end with two hyphens.

For safer transmission of the attachments you should encode them with the base64_encode() function, and with the chunk_split() function split them into chunks. This function adds \r\n break inside the file at regular intervals (normally every 76 characters).

See this example which send HTML email with /tmp/file.pdf file as an attachment.

$to = "PHP ninja ";
$subject = "See attachment";
$body = 'Hi ninja,
please check the attachment! :)'; // Open a file $file = fopen( '/tmp/file.pdf', 'r' ); // Read the file into a variable $size = filesize('/tmp/file.pdf'); $content = fread($file, $size); // encode the data for safe transit // and insert \r\n after every 76 chars. $encodedContent = chunk_split(base64_encode($content)); $number = sha1(microtime()); // Define the main headers. $header = "\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; "; $header .= "boundary=$number\r\n"; $header .= "--$number\r\n"; // Define the message section $header .= "Content-Type: text/html\r\n"; $header .= "Content-Transfer-Encoding:8bit\r\n\n"; $header .= $body."\r\n"; $header .= "--$number\r\n"; // Define the attachment section $header .= "Content-Type: multipart/mixed; "; $header .= "name=\"file.pdf\"\r\n"; $header .= "Content-Transfer-Encoding:base64\r\n"; $header .= "Content-Disposition:attachment; "; $header .= "filename=\"file.pdf\"\r\n\n"; $header .= $encodedContent."\r\n"; $header .= "--$number--"; # Send email now if(mail($to, $subject, "", $header)) echo("E-mail was successfully sent"); else echo("Ooops! Something's not right.");