[long] Re: [Ilugc] Mail attachment in php

  • From: prsabitha@xxxxxxxxx (sabitha PR)
  • Date: Wed, 18 Dec 2002 21:38:45 -0800 (PST)

--- Suraj Kumar <suraj@xxxxxxxxxxx> wrote:

Hi,

We represent Altosys Software Technologies Limited,
which is part of the Rs. 2500 Crore Obul Reddy Group
of companies, whose flagship companies include Apollo
Hospitals, Indo-National Ltd, Indo-Matushita Carbon
Company Ltd, etc.


Altosys Software Technologies Ltd was formerly part of
the Interactive1 group, UK whose clients include
PricewaterhouseCoopers, Cisco, Shell, Prudential
Group, MTV, Canon etc.


Altosys was recently appointed the sole distributor of
TurboLinux and Realtime Products in India.
Dr Victor Yodaiken, founder of RT Linux recently
visited our company and launched the RT range of
products in a seminar held in Hotel Chola Sheraton on
25th November 2002.


Since we are going to be in this Linux line of
business for a long time to come, we request you to
kindly register us as members of your linux community.


I am the Head-HR of this company. I am looking for
people with 3 - 6 years of experience with strong 
Linux skills with excellent communication abilities to
take on our Linux marketing and support functions.


Looking forward to a positive response from you,
Regards,
Sabitha













S.Balasubramanian wrote on Wed, Dec 18, 2002 at
05:54:21PM +0530: 

,----
| How to send attachment through mail in php. 
Please explain.
`----

A Mail attachment isn't a seperate entity in
Internet mail.  The stock
mail functions in PHP donot have a way of adding
attachments as simply
as  passing a filename  parameter!  (I've  heard JSP
 has a  rich mail
class where you pass a filename as a parameter).

I'm  giving a  quick howto  on  sending an 
attachment.  Before  doing
anything,  send  a mail  with  attachments  to 
you@localhost and  cat
/var/spool/mail/you.   Try and  understand how  it
is  formatted.  All
Internet  mails have  a  Content-Type header  and  a
'boundary'.   The
boundary specifies the start and end of each section
within a mail.

The mail msg will use  the "randomstring" specified
in the boundary to
differentiate between various parts of  your mail.
For instance a mail
with a short message and a small attachment will be
like this:

Mail header:

 Content-Type: <content type>;
boundary="randomstring"

Mail Body:

 --randomstring
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 this is the mail's main message.  this is not an
attachment. your MUA
 will display this.
 
 --randomstring
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment;
filename="attachment.txt"
 
 this is the content of the attachment.
 --randomstring--

 
Here,   the   various  portions   of   your   mail  
starts  with   a
--<randomstring> and the entire  stuff ends with a
--<randomstring>--
(note the trailing '--'s).

To sum up all this, the following PHP code should
give a small idea of
how you can implement this:

<?

function random_string($len) {

       srand((double) microtime() * 1000000);
       for ($i = 0; $i < $len; $i++) {
           $ret = $ret . chr (rand (65, 65 + 25));
       }

       return $ret;
}

function add_attachment(&$body, $boundary,
$attachment_file) {
  
  static $init = false;
  
  $fp = fopen ($attachment_file, 'r');
  $file_data = fread ($fp, filesize
($attachment_file));
  $abs_filename = preg_replace ('/.*\//', '',
$attachment_file);
  $buffer = "\r\n" . '--' . $boundary . "\r\n";
  
  $buffer = $buffer . 'Content-Type: text/plain;
charset=us-ascii' . "\r\n";
  $buffer = $buffer . 'Content-Disposition:
attachment; filename="' 
    . $abs_filename . '"' 
    . "\r\n\r\n";
  
  $buffer = $buffer . $file_data;
  
  if (!$init) {
    $pre = '--' . $boundary . "\r\n"
      . 'Content-Type: text/plain; charset=us-ascii'
. "\r\n"
      . 'Content-Disposition: inline' . "\r\n\r\n";
    $init = true;
  }
  
  $body = $pre . $body . $buffer;
}

function end_attachments(&$body, $boundary) {
  $body = $body . "\r\n" . '--' . $boundary . '--' .
"\r\n";
}

function get_header($boundary) {
  return 'Content-Type: multipart/mixed; boundary="'
. $boundary . '"';
}

//main

$message = "hi,\nhow are you?\n";

$boundary = random_string(10);
add_attachment($message, $boundary,
"/con/con/foo.txt");
add_attachment($message, $boundary,
"/con/con/bar.txt");
end_attachments($message, $boundary);
$header = get_header($boundary);
$to = 'suraj@localhost';
$subject = 'attachment test';

mail ($to, $subject, $message, $header);

?>

I would recommend  you to find a full-blown 
attachment class first at
phpclasses.org.  What I've given above is just a
sample to demonstrate
how to do attachments in PHP.  For sending binary
data you may have to
handle a lot more different techniques (base64
encoding, etc.,).  Read
the appropriate RFCs (rfc822, rfc1521,  rfc1522), if
you plan to write
your  own ones.  (note that  most of  the code  at 
phpclasses.org are
GPLed,  so  it is  illegal  to  misuse  them for 
writing  proprietary
applications).
 
  -Suraj
-- 

+-------------------------------------------------<suraj@xxxxxxxxxxx>--+
| Rarer deeds are signs of greatness,               
                  |
| lesser mortals achieve no success                 
                  |
| (greatness of ascetics - 6), Thirukkural          
                  |

+--<http://symonds.net/~suraj/>----------------------------------------+
_______________________________________________
To unsubscribe, email
ilugc-request@xxxxxxxxxxxxxxxxxx with 
"unsubscribe <password> address"
in the subject or body of the message.  
http://www.aero.iitm.ernet.in/mailman/listinfo/ilugc


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

From Ravi Rao <mail@xxxxxxxxxxx>  Thu Dec 19 05:54:20 2002
From: Ravi Rao <mail@xxxxxxxxxxx> (Ravi Rao)
Date: Thu, 19 Dec 2002 00:54:20 -0500
Subject: NOT (was) Re: [long] Re: [Ilugc] Mail attachment in php
In-Reply-To: <20021219053845.28632.qmail@xxxxxxxxxxxxxxxxxxxxxxx>
References: <20021219050247.GA4166@xxxxxxxxxxxxxxx> 
<20021219053845.28632.qmail@xxxxxxxxxxxxxxxxxxxxxxx>
Message-ID: <20021219055420.GA5810@xxxxxxxxxxx>

* sabitha PR <prsabitha@xxxxxxxxx> [Wed, Dec 18, 2002 at 09:38:45PM -0800]:

Sabitha> --- Suraj Kumar <suraj@xxxxxxxxxxx> wrote:

        No he did not. 

Sabitha> I am the Head-HR of this company. I am looking for

        Those HR jokes were true ...!

Sabitha> people with 3 - 6 years of experience with strong 
Sabitha> Linux skills with excellent communication abilities to
Sabitha> take on our Linux marketing and support functions.

        Make sure you take in someone who teaches you email.

Sabitha> Looking forward to a positive response from you,

        You're a frigging idiot.

Sabitha> Regards,
Sabitha> Sabitha

        best,

                --ravi.
-- 
          "If life is a stage, I want some better lighting."
   ________________________________________________________________
 / Larval Geek       http://www.ravirao.net/       mail@xxxxxxxxxxx \
| GPG:0xF1E7DE29  CD17 7E7B 67A4 EEC1 FF0A  2757 358A 4A88 F1E7 DE29 |

Other related posts: