Table of Contents
  • Sending emails from the script may be a very useful functionality within the web application. Most of the websites used the e-mail sending feature to send the notifications to the user. If your web application developed with PHP or uses PHP, it’s very easy to send email from the script using PHP.
  • PHP provides a simple thanks to send emails from the web site. You’ll send text or HTML email with mail () function in PHP. But sometimes email functionality must be extended for sending an attachment with the mail. During this tutorial, we’ll show you ways to send email with attachment in PHP.
  • In the example script, we’ll make it simple to send text or HTML email including any sorts of files as an attachment (like image, .doc, .docx, .pdf, .txt, etc.) Using PHP.

Send HTML Email with Attachment

The PHP mail () function with some MIME type headers is used to send email with attachment in PHP. Within 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 e-mail.
  • $file – Relative path of the file that you simply want to connect with the e-mail .
  • $htmlcontent – Body content of the email (Text or HTML).

The following script allows you to send both sorts of messages (text or HTML) with an attachment file to the e-mail.

<?Php 

// Recipient
$to = 'recipient@example.com';

// Sender
$from = 'sender@example.com';
$fromname = 'gpkumar';

// Email subject
$subject = 'PHP Email with Attachment by praveen';

// Attachment file
$file = "files/gpkumar.pdf";

// Email body content
$htmlcontent = '
<h3>PHP Email with Attachment by gpkumar</h3>
<p>This email is sent from the PHP script with attachment.</p>
';

// Header for sender info
$headers = "From: $fromname"." <".$from.">";

// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Headers for attachment
$headers .= "\nmime-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlcontent . "\n\n";

// Preparing attachment
If(!Empty($file) > 0){
If(is_file($file)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file,"rb");
$data = @fread($fp,filesize($file));

@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;

// Send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);

// Email sending status
Echo $mail?"<h1>Email Sent Successfully!</h1>":"<h1>Email sending failed.</h1>";

?>

The example script allows you to send one attachment to the email, to send an email with multiple attachments follow this tutorial – Send Email with Multiple Attachments in PHP

Sending Email to Multiple Recipients:

  • You can send email to the multiple recipients directly with Cc and Bcc headers.
  • Use the Cc and Bcc headers for sending email with attachment to multiple recipients in PHP.
$headers .= "\ncc: mail@example.com"; 
$headers .= "\nbcc: mail@example.com";

Conclusion

  • Here we’ve provided the easiest thanks to send email with attachment in PHP.
  • You don’t have to include any library to send HTML email with attachment.
  • Using PHP default mail () function you’ll easily send email with attachment.