Page 1 of 1

simple way to add attachment to email

Posted: Thu Jan 26, 2006 8:02 pm
by xeno439
Hi, I am looking for an easy way to send an attachment to an email form in php. How do I specify the path and so forth. I tried AddAttachmnet ($path); but it didnt do the job and made the rest of the code unusable. Thanks

Posted: Thu Jan 26, 2006 8:10 pm
by raghavan20
I built this function long time ago similar to PHPMailer class...It is better to use PHPMailer class to send mails because they know the best way to help reach your email to other email servers...anyway...here is it...I hope you would know to replace the variables I have used.... :)

Code: Select all

//Build all attachments and add it to the message that has to be sent in the mail
	function buildAttachments(){
		foreach ($this->files as $file ){ 
			$message .= "\r\nContent-Type: {$file['MIME']}; name=\"{$file['NAME']}\"\r\n"; 
			$message .= "Content-Transfer-Encoding: base64\r\n"; 
			$message .= "Content-Disposition: attachment; filename=\"{$file['NAME']}\"\r\n\r\n"; 
			$base64 = base64_encode($file['FILE']); 
			for ( $i=0; $i < strlen($base64) / 70; $i++ ){ //line size is set to 70
				$message .= substr( $base64, $i*70, 70)."\r\n"; 
			}
		} 
		
		return $message;
	}

Posted: Thu Jan 26, 2006 8:14 pm
by xeno439
sorry, I am a little new here. Can you tell my exactly how to specify the path to a file on my server to include. For example, do I put quotations around the "http://mysite.com/images/sample.png" in the part where its says FILE. Thanks for quick response, I really appreciate it.

Posted: Thu Jan 26, 2006 8:36 pm
by John Cartwright
lets start off by asking you to post what you've done so far.. that way we can clearly identify the steps involved.

Posted: Thu Jan 26, 2006 8:44 pm
by xeno439

Code: Select all

<? 
$sendTo = "webmaster@flashmailforms.com"; 
$subject = "Information From Flash Site"; 
$ip = $_SERVER['REMOTE_ADDR']; 
$headers .= "From: " . $_POST['name'] . ">nl2br"; 
$headers .= "Reply-To: " . $_POST['email'] . ">nl2br"; 
$message = $_POST['message'] . "


 " . "Sender's ip address is " . 
 nl2br($ip); 
mail($sendTo, $subject, $message, $headers); 
?>
This does the job of sending the variables from my Flash Movie. What I want is to be able to send a file as an attachment that has recently been uploaded to the server. I know the code is sloppy, but all the validation is done in the Flash movie, and the line breaks are the result of a lot of trial and error. I just figured out how to get the user to upload a file to the server in Flash and would like the user to be able to send that file as an attachment to me (or whoever). I would ideally like to save the uploaded file to a temporary spot, but I am not trying to get ahead of myself. Thanks

Posted: Thu Jan 26, 2006 8:59 pm
by raghavan20
I am not sure this would work. By the way, why you are using nl2br at a few places???

Code: Select all

<? 
$sendTo = "webmaster@flashmailforms.com"; 
$subject = "Information From Flash Site"; 
$ip = $_SERVER['REMOTE_ADDR']; 
$headers .= "From: " . $_POST['name'] . ">nl2br"; 
$headers .= "Reply-To: " . $_POST['email'] . ">nl2br"; 
$message = $_POST['message'] . " 


" . "Sender's ip address is " . 
nl2br($ip); 
if (areAttachmentsAvailable === TRUE){// you have to set this variable if you want to attach files
	$message .= "--".md5( uniqid("myboundary")."\n"; 
	$message .= buildAttachments ('application/x-msword', "" , ""); //look out for available mimes	
	
}
mail($sendTo, $subject, $message, $headers); 


function buildAttachments($mime,$name, $filePath){ 
	$message = "\r\nContent-Type: {$mime}; name=\"{$name}\"\r\n"; 
	$message .= "Content-Transfer-Encoding: base64\r\n"; 
	$message .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n\r\n"; 
	$base64 = base64_encode($filePath); 
	for ( $i=0; $i < strlen($base64) / 70; $i++ ){ //line size is set to 70 
		$message .= substr( $base64, $i*70, 70)."\r\n"; 
	} 
	
	return $message; 
} 

?>

Posted: Thu Jan 26, 2006 9:10 pm
by xeno439
thanks I will give it a try. The line breaks come from lots of trial and error. Where do I specify the path. Should I do that in Flash?