Sambhashanam
तकनीकी संभाषणम्
Navigation
  • Microsoft Technologies
  • Java Technologies
  • LAMP Technologies
  • Opensource
    • Microsoft Opensource
    • LAMP Opensource
    • Java Opensource
You are here: Home › LAMP Opensource › How to send mail that supports multi-type attachment using php’s mail function
← Disable user from accessing their posts (member area) in wordpress
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.activiti.spring.SpringProcessEngineConfiguration] for bean with name ‘processEngineConfiguration’ nested exception is java.lang.ClassNotFoundException: org.activiti.spring.SpringProcessEngineConfiguration →

How to send mail that supports multi-type attachment using php’s mail function

July 24, 2013 | Filed under: LAMP Opensource, LAMP Technologies, Opensource and tagged with: php mail function, sending mail with attachment

To send an email with php mail() function is quite easy, but when it comes to send attachments with mail() function is a little bit complex. To send an email with mixed content requires to set Content-type header to multipart/mixed.

The message part is always specified in the Boundary section. A boundary always starts with two hyphens followed by unique number and also ends with two hyphens. This number is never a part of the mail body. Attached files are always encoded with the base64_encode() and chunked with the chunk_split() function.

Following is a sample code for sending pdf, doc, images files as an attachment with php’s mail() function.

$fileatt_path = $_SERVER['DOCUMENT_ROOT']."/documents/";  // Path to the file
$fileatt_type = "application/pdf"; // File Type
$attachement = 'example.pdf';
$fileatt = $fileatt_path.$attachement;
 
if(!empty($username) && !empty($email)){// check to send mail only if username and email is provided
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = $attachement; // Filename that will be used for the file as the attachment
$email_from = "prakashak <prakashak@sambhashanam.com/>"; // Who the email is from
$email_subject = "Thanks for your interest"; // The Subject of the email
$email_subject .= "<img src='http://www.sambhashanam.com/images/logo.png'/>";//To put logo in the mail body
$email_message .= "<b>Dear User,</b><br/>"; // Message that the email has in it
$email_message .= "Please find attached file.<br/><br/>"; // Message that the email has in it
$email_message .= "<b>Best,</b><br/>"; // Message that the email has in it
$email_message .= "<b>Sambhashanam.com</b>";
$email_to = $email; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());//to create a 32 digit hexadecimal number to create unique number
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$sent = mail($email_to, $email_subject, $email_message, $headers);

$fileatt_path = $_SERVER['DOCUMENT_ROOT']."/documents/";  // Path to the file $fileatt_type = "application/pdf"; // File Type $attachement = 'example.pdf'; $fileatt = $fileatt_path.$attachement; if(!empty($username) && !empty($email)){// check to send mail only if username and email is provided $fileatt_type = "application/pdf"; // File Type $fileatt_name = $attachement; // Filename that will be used for the file as the attachment $email_from = "prakashak <prakashak@sambhashanam.com/>"; // Who the email is from $email_subject = "Thanks for your interest"; // The Subject of the email $email_subject .= "<img src='http://www.sambhashanam.com/images/logo.png'/>";//To put logo in the mail body $email_message .= "<b>Dear User,</b><br/>"; // Message that the email has in it $email_message .= "Please find attached file.<br/><br/>"; // Message that the email has in it $email_message .= "<b>Best,</b><br/>"; // Message that the email has in it $email_message .= "<b>Sambhashanam.com</b>"; $email_to = $email; // Who the email is to $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $semi_rand = md5(time());//to create a 32 digit hexadecimal number to create unique number $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message .= "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data .= "\n\n" . "--{$mime_boundary}--\n"; $sent = mail($email_to, $email_subject, $email_message, $headers);

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Google+ (Opens in new window)

Related

Did you like this article? Share it with your friends!

Tweet

Written by Sushil Pandey

Visit my Website
← Disable user from accessing their posts (member area) in wordpress
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.activiti.spring.SpringProcessEngineConfiguration] for bean with name ‘processEngineConfiguration’ nested exception is java.lang.ClassNotFoundException: org.activiti.spring.SpringProcessEngineConfiguration →

2 Responses to "How to send mail that supports multi-type attachment using php’s mail function"

  1. Dhananjay Jha says:
    July 25, 2013 at 12:02 am

    This is really good Sushil.
    May you please suggest how this code can be enhanced to support template for mail contents?

    Regards
    DJ

    1. Sushil Pandey says:
      July 25, 2013 at 11:36 pm

      @DJ,

      There are lot of methods available to achieve the mail content in templated format.

      1. You can put the subject part of the mail inside various HTML tags as I used for “Dear User”. Also you can use a combination of strip_tags() and htmlentities() functions.

      2. By including the template file ‘template.html’ as
      ob_start();
      include(’email/template.html’);
      $email_message = ob_get_clean();
      Here the output buffering function will capture the output returned by include() function and will store into $email_message which you pass to the mail function.

      3. By using the file_get_contents() function instead of include function.

      Thanks,
      Sushil

Comments are closed.

Authors

  • Dhananjay Jha
  • Manish Kumar
  • Sushil Pandey

Recent Comments

  • muhammad on Mail merge in java for Microsoft Word document – Part I
  • muhammad on Mail merge in java for Microsoft Word document – Part I
  • Dhananjay Jha on Mail merge in java for Microsoft Word document and convert to PDF without iText – Part II
  • Clyde Symonette on Mail merge in java for Microsoft Word document and convert to PDF without iText – Part II
  • Dhananjay Kumar Jha on Mail merge in java for Microsoft Word document and convert to PDF without iText – Part II

Tag Cloud

.net activiti activiti-spring activiti bpmn C# change sender mail id in wordpress class classloader Conver MS Word to PDF disable user wordpress dox4j glassfish http response compression java Java Mail Merge MS Word javascript confirmation link on image jax-ws on tomcat jax-ws webservice Jee JTS junit junit-spring Link on Image with hidden URL link load spring configuration load spring configuration xml junit maven maven build Merge MS Word using Java Merge Word Document using XdocReport OOPS oracle weblogic PDF conversion using docx4j php mail function sample activiti process with human task and service task sending mail with attachment spring-orm spring framework spring hibernate spring hibernate integration spring jpa spring jpa hibernate integration tomcat jax-ws web service wordpress wordpress sender email

Looking for old post?

© 2021 Sambhashanam