Attaching file in email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xLechugasx
Forum Newbie
Posts: 14
Joined: Wed Aug 28, 2002 9:56 am

Attaching file in email

Post by xLechugasx »

Hello to All!
I am using the mail function to send an email with php. I have a file in the server which I would like to attach to that email?
How can I use the mail function to do this?
Thanks a lot,
Nicolas
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

You can't do it just like a **click**. Try reading this article.
mlemos
Site Admin
Posts: 126
Joined: Mon Apr 29, 2002 4:14 pm
Contact:

Re: Attaching file in email

Post by mlemos »

xLechugasx wrote:Hello to All!
I am using the mail function to send an email with php. I have a file in the server which I would like to attach to that email?
How can I use the mail function to do this?
Nicolas
You may want to try this MIME message composing and sending class that makes it very simple to create messages with attachments from existing files or dynamically generated data with automatic content-type detection.

It can also send HTML messages with embedded images and even alternative text versions and attachements if you would like that.
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post by Phirus »

This will work:

Code: Select all

<?php 

	/*
	 * FileMailer - Attaches File to Email
	 *	Unknown origin of this code - Modified by Craig Donnelly (craig@evilwalrus.com) 
	*/

	$fileatt = "code.php"; // Path to the file                 
	$fileatt_type = "application/octet-stream"; // File Type 
	$fileatt_name = "code.php"; // Filename that will be used for the file as the attachment 

	$email_from = "craig@develop3r.net"; // Who the email is from 
	$email_subject = "Heres your file..."; // The Subject of the email 
	$email_message = "Regards,\n\nCraig"; // Message that the email has in it 

	$email_to = "craig@evilwalrus.com"; // Who the email is too 

		$headers = "From: ".$email_from; 

			$file = fopen($fileatt,'rb'); 
			$data = fread($file,filesize($fileatt)); 
			fclose($file); 

$semi_rand = md5(time()); 
$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"; 

$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok){ 
	echo "<font face=verdana size=2>The file was successfully sent!</font>"; 
}else{ 
	die("Sorry but the email could not be sent. Please go back and try again!"); 
} 

?>
Regards,

Craig
Post Reply