Jump to content

Email Attachment Help.


Demonic

Recommended Posts

I'm trying to make my email so it is able to actually download the attachment that you send.

Right now all it does is send plain text of encoded base64

<?php
//Copyrighted to Lamonte Harris
//2007 All rights reserved
//Copyright must stay in tact
//Internet Alias: Demonic/Lamonte/Scheols/Nor/TheInfernoSIn

Class Mail
{
	var $headers;
	var $message;
	var $F_DATA;
	var $F_NAME;
	var $F_TYPE;
	var $subject;
	var $from;
	var $to;
	function ContactForm()
	{
		$form = "
			<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
			<html>
			<head>
				<title>Contact Form</title>
				<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />
			</head>
			<body>
				<form method='post' action='' enctype='multipart/form-data'>
					<table>
						<tr>
							<td width='50%'>Name: </td>
							<td width='50%'><input type='text' name='name' /></td>
						</tr>
						<tr>
							<td width='50%'>Email: </td>
							<td width='50%'><input type='text' name='email' /></td>
						</tr>
						<tr>
							<td width='50%'>Attachment: </td>
							<td width='50%'><input type='file' name='attachment' /></td>
						</tr>
						<tr>
							<td width='50%'>Message Body</td>
							<td width='50%'><textarea cols='10' rows='10' name='body'></textarea></td>
						</tr>
						<tr>
							<td colspan='2' align='center'><input type='submit' value='Send Email' name='send' /></td>
						</tr>
					</table>
				</form>
			</body>
			</html>
		";
		return $form;
	}
	function EncodeAttachment($temp,$name,$type)
	{
		$destination = "test/";
		move_uploaded_file($temp,$destination . basename($name));
		$o = fopen($destination . $name,"r");
		$this->F_TYPE = $type;
		$this->F_NAME = $name;
		$this->F_DATA = fread($o,filesize($destination . $this->F_NAME));
		fclose($o);
		unlink($destination . $this->F_NAME);
		$this->F_DATA = chunk_split(base64_encode($this->F_DATA));

	}
	function headers()
	{
		$random = md5(rand(0,1999) . time());
		$boundary = "==Multipart_Boundary_x{$random}x";
		$multi_part = "This is a multi-part message in MIME format.\n\n";
		$multi_part .= "--{$boundary}\n";
		$multi_part .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
            $multi_part .= "Content-Transfer-Encoding: 7bit\n\n";

		$this->message =  $multi_part . $this->message;
		$this->message .= "--{$boundary}\n" . "Content-Type: {$this->F_TYPE};\n" . " name=\"{$this->F_NAME}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$this->F_NAME}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $this->F_DATA . "\n\n" . "--{$mime_boundary}--\n"; 

		$this->headers = "From: {$this->from}\r\nReply-To: {$this->from}";
		$this->headers .= "\r\nMIME-Version: 1.0\r\n";
		$this->headers .= "\r\nContent-Type:multipart/mixed; boundary=\"".$boundary."\"";
	}
	function Send()
	{
		mail($this->to,$this->subject,$this->message,$this->headers);
	}
	function contact()
	{
		if(!isset($_POST['send']))
		{
			echo $this->ContactForm();
		}
		else
		{
			$this->message = $_POST['body'];
			$this->EncodeAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'],$_FILES['attachment']['type']);
			$this->headers();
			$this->send();
		}
	}
}
?>

 

 

Index.php

<?php
//Copyrighted to Lamonte Harris
//2007 All rights reserved
//Copyright must stay in tact
include("Mailer.php");
$Mail = new Mail;
$Mail->to = "[email protected]";
$Mail->subject = "Attachment test.";
$Mail->from = "[email protected]";
$Mail->contact();
?>

Link to comment
https://forums.phpfreaks.com/topic/60008-email-attachment-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.