--- 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+-------------------------------------------------<suraj@xxxxxxxxxxx>--+
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
--
| Rarer deeds are signs of greatness,+--<http://symonds.net/~suraj/>----------------------------------------+
|
| lesser mortals achieve no success
|
| (greatness of ascetics - 6), Thirukkural
|
_______________________________________________
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