Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Attach file to email

the mail() function

Moderators: macek, egami, gesf

Attach file to email

Postby foot123 » Wed Nov 06, 2002 4:43 pm

how can i attach a file in the mail function??? :(
foot123
New php-forum User
New php-forum User
 
Posts: 14
Joined: Wed Nov 06, 2002 4:39 pm

Postby Dmitrit Plakhotnik » Thu Nov 07, 2002 1:50 am

Try this:

<?

$mail=new html_mime_mail();
$mail->add_html("Hello!");
$mail->add_attachment("filename");
$mail->build_message('win');
$mail->send('smtpserver',
'to',
'from',
'subject');

class html_mime_mail {
var $headers;
var $multipart;
var $mime;
var $html;
var $parts = array();

function html_mime_mail($headers="") {
$this->headers=$headers;
}

function add_html($html="") {
$this->html.=$html;
}

function build_html($orig_boundary,$kod) {
$this->multipart.="--$orig_boundary\n";
if ($kod=='w' || $kod=='win' || $kod=='windows-1251') $kod='windows-1251';
else $kod='koi8-r';
$this->multipart.="Content-Type: text/html; charset=$kod\n";
$this->multipart.="BCC: del@ipo.spb.ru\n";
$this->multipart.="Content-Transfer-Encoding: Quot-Printed\n\n";
$this->multipart.="$this->html\n\n";
}


function add_attachment($path="", $name = "", $c_type="application/octet-stream") {
if (!file_exists($path.$name)) {
print "File $path.$name dosn't exist.";
return;
}
$fp=fopen($path.$name,"r");
if (!$fp) {
print "File $path.$name coudn't be read.";
return;
}
$file=fread($fp, filesize($path.$name));
fclose($fp);
$this->parts[]=array("body"=>$file, "name"=>$name,"c_type"=>$c_type);
}


function build_part($i) {
$message_part="";
$message_part.="Content-Type: ".$this->parts[$i]["c_type"];
if ($this->parts[$i]["name"]!="")
$message_part.="; name = \"".$this->parts[$i]["name"]."\"\n";
else
$message_part.="\n";
$message_part.="Content-Transfer-Encoding: base64\n";
$message_part.="Content-Disposition: attachment; filename = \"".
$this->parts[$i]["name"]."\"\n\n";
$message_part.=chunk_split(base64_encode($this->parts[$i]["body"]))."\n";
return $message_part;
}


function build_message($kod) {
$boundary="=_".md5(uniqid(time()));
$this->headers.="MIME-Version: 1.0\n";
$this->headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
$this->multipart="";
$this->multipart.="This is a MIME encoded message.\n\n";
$this->build_html($boundary,$kod);
for ($i=(count($this->parts)-1); $i>=0; $i--)
$this->multipart.="--$boundary\n".$this->build_part($i);
$this->mime = "$this->multipart--$boundary--\n";
}


function send($server, $to, $from, $subject="", $headers="") {

$headers="To: $to\nFrom: $from\nSubject: $subject\nX-Mailer: The Mouse!\n$headers";
$fp = fsockopen($server, 25, &$errno, &$errstr, 30);
if (!$fp)
die("Server $server. Connection failed: $errno, $errstr");
fputs($fp,"HELO $server\n");
fputs($fp,"MAIL FROM: $from\n");
fputs($fp,"RCPT TO: $to\n");
fputs($fp,"DATA\n");
fputs($fp,$this->headers);
if (strlen($headers))
fputs($fp,"$headers\n");
fputs($fp,$this->mime);
fputs($fp,"\n.\nQUIT\n");
while(!feof($fp))
$resp.=fgets($fp,1024);
fclose($fp);
}
}

?>


--
Sincerely,
Dmitriy S. Plakhotnik
AlarIT programmer
http://www.AlarIT.com
Dmitrit Plakhotnik
New php-forum User
New php-forum User
 
Posts: 13
Joined: Fri Nov 01, 2002 6:56 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron